Search code examples
androidgraphandroid-canvasachartengineandroid-graphview

Making Concentric Pie Chart in Android


Today I am come across a problem in Drawing out Graph in Android Pro-grammatically. I am Using Achartengine graph library for achieving this , I have done with simple pie chart , But I have no clue how to make Concentric pie chart using this .

Here is a demo Image of graph which I want to make.

Thanx for help in advance :) enter image description here


Solution

  • Here is the example, first create a LinearLayout in your view(xml) and get it in your activityto pass it SingleDonutGraph class to draw a donut graph on this layout.You also have to pass graphValues[]as double array(the value you have to set on donut graph).

    LayoutToDisplayChartLeftGraph = (LinearLayout) findViewById(R.id.right_graph_for_punch_count);
    Intent achartIntentLeft = new SingleDonutGraph().execute(TabletPunchCountActivity.this, LayoutToDisplayChartLeftGraph,graphValues);
    

    Then use this class SingleDonutGraph.java

    public class SingleDonutGraph {
    private GraphicalView mChartView2;
    static int count = 3;
    
    int[] Mycolors = new int[] { Color.parseColor("#F2846B"),
            Color.parseColor("#A01115"), Color.parseColor("#741E1E") };
    String[] labels = { "TODAY", "AVERAGE", "TOTAL" };
    
    
    public Intent execute(Context context, LinearLayout parent,double values[]) {
        parent.removeAllViews();
        int[] colors = new int[count];
        for (int i = 0; i < count; i++) {
            colors[i] = Mycolors[i];
        }
        DefaultRenderer renderer = buildCategoryRenderer(colors);
        renderer.setShowLabels(false);
        renderer.setBackgroundColor(Color.BLACK);
        renderer.setPanEnabled(false);// Disable User Interaction
        renderer.setScale((float) 1.4);
        renderer.setInScroll(true); //To avoid scroll Shrink        
        renderer.setStartAngle(90);
        renderer.setShowLegend(false);
    
    
        MultipleCategorySeries categorySeries = new MultipleCategorySeries(
                "Punch Graph");
        categorySeries.add(labels, values);
    
        mChartView2 = ChartFactory.getDoughnutChartView(context,
                categorySeries, renderer);
    
        parent.addView(mChartView2);
    
        return ChartFactory.getDoughnutChartIntent(context, categorySeries,
                renderer, null);
    }
    
    protected DefaultRenderer buildCategoryRenderer(int[] colors) {
        DefaultRenderer renderer = new DefaultRenderer();
        for (int color : colors) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(color);
            renderer.addSeriesRenderer(r);
    
        }
        return renderer;
    }
    }