How to use AChartEngine to display two pie graphs in one activity AND displaying my own data in the charts

StackOverflow https://stackoverflow.com/questions/23492089

  •  16-07-2023
  •  | 
  •  

Question

I am very much new to achartengine (to programming in Java/Android in general), but I need it to display two pie graphs in one activity. I've got it to work by calling the following code, but now I'd like to know how to display the data I have set previously in the activity. Each chart has to hold its own data (its own array of strings)

My onCreate of the main activity

@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

Here is my PieGraph class which is in the same .java file as my activity

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);
    }
}

I now need a method which will get the given 'test' values, put them into the graph and output both graphs in the layout (I've already added two LinearLayouts for the graphs in my .xml).

Was it helpful?

Solution

You can do it by using something like below. But make sure "arrProducts" which is array of Custom objects is filled up before this method is called. Custom object class has get and set methods for "uniquecalls" and "specialty". I've converted uniqueCalls which were string, into the boolean values.

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

            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top