如何在图表引擎android中正确显示条形图

时间:2013-07-15 19:44:24

标签: android achartengine

我使用AChart Engine显示条形图,并希望显示如下条形图:

enter image description here

但最终结果如下: enter image description here

这是非常令人讨厌的,我正在尝试最后4个小时给它一个正确的格式但失败了。我在互联网上搜索了一个合适的教程但却无法管理。你能告诉我我应该包括什么片段吗?

这是我尝试的代码:

public class BarGraph{

    public GraphicalView getView(Context context) 
    {   
        // Bar 1

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        CategorySeries ac = new CategorySeries("AC");
        CategorySeries pe = new CategorySeries("PE");
        CategorySeries wa = new CategorySeries("WA");
        CategorySeries tl = new CategorySeries("TL");
        CategorySeries ml = new CategorySeries("ML");
        CategorySeries ce = new CategorySeries("CE");
        CategorySeries re = new CategorySeries("RE");
        CategorySeries ot = new CategorySeries("OT");
        ac.add(291);
        dataset.addSeries(ac.toXYSeries());
        pe.add(11);
        dataset.addSeries(pe.toXYSeries());
        wa.add(204);
        dataset.addSeries(ac.toXYSeries());
        tl.add(28);
        dataset.addSeries(pe.toXYSeries());
        ml.add(0);
        dataset.addSeries(ac.toXYSeries());
        ce.add(21);
        dataset.addSeries(pe.toXYSeries());
        re.add(51);
        dataset.addSeries(ac.toXYSeries());
        ot.add(4);
        dataset.addSeries(pe.toXYSeries());


        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        mRenderer.setChartTitle("Submission Statistics");
        mRenderer.setXTitle("Verdict Code");
        mRenderer.setYTitle("No. of Submissions");
        mRenderer.setAxesColor(Color.BLACK);
        mRenderer.setLabelsColor(Color.BLACK);
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.WHITE);
        mRenderer.setMarginsColor(Color.WHITE);
        mRenderer.setZoomEnabled(true);
        mRenderer.setZoomButtonsVisible(true);

        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setColor(Color.parseColor("#00AA00"));
        renderer.setDisplayChartValues(true);
        renderer.setChartValuesSpacing((float) 0.5);

        XYSeriesRenderer renderer2 = new XYSeriesRenderer();
        renderer2.setColor(Color.parseColor("#666600"));
        renderer2.setDisplayChartValues(true);
        renderer2.setChartValuesSpacing((float) 0.5);

        XYSeriesRenderer renderer3 = new XYSeriesRenderer();
        renderer3.setColor(Color.parseColor("#FF0000"));
        renderer3.setDisplayChartValues(true);
        renderer3.setChartValuesSpacing((float) 0.5);

        XYSeriesRenderer renderer4 = new XYSeriesRenderer();
        renderer4.setColor(Color.parseColor("#0000FF"));
        renderer4.setDisplayChartValues(true);
        renderer4.setChartValuesSpacing((float) 0.5);

        XYSeriesRenderer renderer5 = new XYSeriesRenderer();
        renderer5.setColor(Color.parseColor("#6767D0"));
        renderer5.setDisplayChartValues(true);
        renderer5.setChartValuesSpacing((float) 0.5);

        XYSeriesRenderer renderer6 = new XYSeriesRenderer();
        renderer6.setColor(Color.parseColor("#AAAA00"));
        renderer6.setDisplayChartValues(true);
        renderer6.setChartValuesSpacing((float) 0.5);

        XYSeriesRenderer renderer7 = new XYSeriesRenderer();
        renderer7.setColor(Color.parseColor("#00AAAA"));
        renderer7.setDisplayChartValues(true);
        renderer7.setChartValuesSpacing((float) 0.5);

        XYSeriesRenderer renderer8 = new XYSeriesRenderer();
        renderer8.setColor(Color.parseColor("#000000"));
        renderer8.setDisplayChartValues(true);
        renderer8.setChartValuesSpacing((float) 0.5);


        mRenderer.addSeriesRenderer(renderer);
        mRenderer.addSeriesRenderer(renderer2);
        mRenderer.addSeriesRenderer(renderer3);
        mRenderer.addSeriesRenderer(renderer4);
        mRenderer.addSeriesRenderer(renderer5);
        mRenderer.addSeriesRenderer(renderer6);
        mRenderer.addSeriesRenderer(renderer7);
        mRenderer.addSeriesRenderer(renderer8);

        return ChartFactory.getBarChartView(context, dataset,mRenderer, Type.DEFAULT);
    }

}

1 个答案:

答案 0 :(得分:1)

我建议您构建CategorySeries而不是构建XYSeries。然后,在第一个系列中执行:series.add(1, 291);第二个将是series.add(2, 11);,依此类推。