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

时间:2014-05-06 10:24:56

标签: android graph achartengine

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

我对主要活动的创造

@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类,与我的活动在同一个.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)。

1 个答案:

答案 0 :(得分:1)

您可以使用下面的内容来完成。但要确保" arrProducts"在调用此方法之前填充Custom对象数组。自定义对象类具有" uniquecalls" "专业" 的获取和设置方法。我已将 uniqueCalls (它们是字符串)转换为布尔值。

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) {

            }
        }