gpt4 book ai didi

android - 如何使用 AChartEngine 在一个 Activity 中显示两个饼图并在图表中显示我自己的数据

转载 作者:行者123 更新时间:2023-11-30 02:54:40 27 4
gpt4 key购买 nike

我对 achartengine 非常陌生(通常对 Java/Android 编程),但我需要它在一个 Activity 中显示两个饼图。我已经通过调用以下代码让它工作,但现在我想知道如何显示我之前在 Activity 中设置的数据。每个图表都必须保存自己的数据(自己的字符串数组)

主要 Activity 的我的onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rezultati);

...


db.open();
//here I get the values, which are then stored in test1, test2 and test3 for graph 1
//I also get the values for graph 2, which are called test4, test5 and test6
db.close();

//Now I'd like to assign those 'test' values to the pie chart

这是我的 PieGraph 类,它与我的 Activity 位于同一个 .java 文件中

public class PieGraph{
public GraphicalView getView(Context context) {

int[] values; //I'd like to place my values for the first graph here, display the 1st graph and then do the same with the second

CategorySeries series = new CategorySeries("Pie Graph");
//I also need to replace my 'series' to "test1", "test2" and "test3" for the 1st graph
//and "test4", "test5" and "test6 for the 2nd graph

int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.RED };

DefaultRenderer renderer = new DefaultRenderer();
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}

renderer.setZoomButtonsVisible(false);
renderer.setPanEnabled(false);
renderer.setDisplayValues(true);
renderer.setLabelsTextSize(35);
renderer.setShowLegend(false);

return ChartFactory.getPieChartView(context, series, renderer);
}
}

我现在需要一种方法来获取给定的“测试”值,将它们放入图表中并在布局中输出两个图表(我已经为我的 .xml 中的图表添加了两个 LinearLayouts)。

最佳答案

您可以使用如下所示的方法来完成。但是请确保在调用此方法之前填充“arrProducts”,即自定义对象数组。自定义对象类具有"uniquecalls""specialty" 的get 和set 方法。我已将字符串形式的 uniqueCalls 转换为 bool 值。

ArrayList<CustomObjectClass> arrProducts;

public void PieChart() {
try {
String uniqueCalls;
String specialtyName;

CategorySeries series = new CategorySeries(" Pie Chart");
for (int i = 0; i < arrProducts.size(); i++) {
uniqueCalls = arrProducts.get(i).getUniqueCalls();
specialtyName = arrProducts.get(i).getSpecialtyName();
double db = Double.parseDouble(uniqueCalls);
Log.v("******", "String : " + uniqueCalls + " Double: " + db);
series.add(specialtyName, db);
}
// Color of each Pie Chart Sections
int[] colors = { Color.rgb(169, 169, 169), Color.rgb(255, 165, 0),
Color.rgb(128, 128, 128), Color.rgb(255, 160, 122),
R.color.BabyBlue,
R.color.BashfulPink, R.color.BasketBallOrange,
R.color.Chocolate,R.color.Khaki, R.color.LimeGreen, R.color.Maroon, };

// Instantiating a renderer for the Pie Chart
DefaultRenderer defaultRenderer = new DefaultRenderer();

for (int i = 0; i < arrProducts.size(); i++) {
specialtyName = arrProducts.get(i).getSpecialtyName();
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
seriesRenderer.setColor(colors[i]);

seriesRenderer.setDisplayChartValues(true);
// Adding a renderer for a slice
defaultRenderer.addSeriesRenderer(seriesRenderer);
}

// defaultRenderer.setChartTitle("Product Wise Calling");
defaultRenderer.setChartTitleTextSize(20);
defaultRenderer.setZoomButtonsVisible(true);
// defaultRenderer.setBackgroundColor(Color.GRAY);
defaultRenderer.setLabelsColor(Color.BLACK);
defaultRenderer.setDisplayValues(true);
defaultRenderer.setClickEnabled(true);
defaultRenderer.setSelectableBuffer(10);

LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);

// Creating a Line Chart
mChartView = ChartFactory.getPieChartView(getBaseContext(), series,
defaultRenderer);

// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChartView);

mChartView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SeriesSelection seriesSelection = mChartView
.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(
ChartProductCallingSpecialtyWiseActivity.this,
"Please Select the Pie to see the value",
Toast.LENGTH_SHORT).show();
} else {
// display information of the clicked point
Toast.makeText(
ChartProductCallingSpecialtyWiseActivity.this,
" Pie Value : " + seriesSelection.getXValue(),
Toast.LENGTH_SHORT).show();
}

}
});
// ANOTHER INSTANCE EXAMPLE OF PLOTTING THE PIE CHART
// Creating an intent to plot Pie chart using dataset and
// multipleRenderer
// Intent intent = ChartFactory.getPieChartIntent(getBaseContext(),
// series, defaultRenderer, "Pie Chart");
//
// // Start Activity
// startActivity(intent);
} catch (Exception e) {

}
}

关于android - 如何使用 AChartEngine 在一个 Activity 中显示两个饼图并在图表中显示我自己的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23492089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com