Search code examples
javaandroidbar-chartachartenginestacked

Are multiple stacked Bar charts possible using Achartengine?


I tried adding another set of values to the demo example of stacked bar charts with achartengine, but the new values introduced by me don't appear on the chart. Is Stacking bars limited to two bars?

public Intent getIntent(Context context) {

    String[] titles = new String[] { "Good", "Defect", "Repair" };
    List<double[]> values = new ArrayList<double[]>();

    values.add(new double[] { 14230, 12300, 1424, 15240, 15900, 19200,
    22030, 21200, 19500, 15500, 12060, 14000 });
    values.add(new double[] { 14230, 12300, 14240, 15244, 15900, 19200,
            22030, 21200, 19500, 15500, 12600, 14000 });
    values.add(new double[] { 5230, 7300, 9240, 10540, 7900, 9200, 12030,
            11200, 9500, 10500, 11600, 13500 });

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

    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);

    setChartSettings(renderer, "Machine Efficiency Rates",
            "Machine", "NET produce", 0.5, 12.5, 0, 24000, Color.GRAY,
            Color.LTGRAY);

    renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
    renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
    renderer.getSeriesRendererAt(2).setDisplayChartValues(true);
    renderer.setXLabels(12);
    renderer.setYLabels(5);
    renderer.setXLabelsAlign(Align.LEFT);
    renderer.setYLabelsAlign(Align.LEFT);
    renderer.setPanEnabled(true, false);
    renderer.setZoomEnabled(true);
    renderer.setZoomRate(1.1f);
    renderer.setBarSpacing(0.5f);
    return ChartFactory.getBarChartIntent(context,
            buildBarDataset(titles, values), renderer, Type.STACKED);

}

Solution

  • There is a misunderstanding in the way AChartEngine displays stacked bar charts. It doesn't really stack them, but displays them one over the other. This means that you will want to always add the bigger values first then the smaller and so on as it renders the first series, the second one above the first and so on.

    UPDATE: As of version 1.2.0, there is the new HEAP stacked bar charts, which are displayed in the manner you need.