使用achartengine的每个酒吧用不同颜色的条形图

时间:2014-08-14 08:13:55

标签: android achartengine

我是使用achartengine库的新手。显然我想要一个带有两个值但每个颜色不同的条形图。我已经完成了我所做的截图。问题是我似乎找不到如何改变条形图中每个条形的颜色的方法。

以下是截图: enter image description here

到目前为止,这是我的代码:

public class BarGraph {

    public Intent getIntent(Context context) {

        int y[] = { 8000, 4000 };

        CategorySeries series = new CategorySeries("Bar1");
        for (int i = 0; i < y.length; i++) {
            series.add("Bar" + (i + 1), y[i]);

        }

        XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset(); 
        dataSet.addSeries(series.toXYSeries()); // number of series

        // customization of the chart
        XYSeriesRenderer renderer = new XYSeriesRenderer(); // one renderer for
                                                            // one series
        renderer.setColor(Color.RED);

        renderer.setDisplayChartValues(true);
        renderer.setChartValuesSpacing((float) 5.5d);

        renderer.setLineWidth((float) 10.5d);

        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.setChartTitle("Demo Graph");
        // mRenderer.setXTitle("xValues");

        mRenderer.setShowLegend(false);
        mRenderer.setShowGridX(false); // this will show the grid in graph
        mRenderer.setShowGridY(false);
        // mRenderer.setAntialiasing(true);
        mRenderer.setBarSpacing(.5); // adding spacing between the line or
                                        // stacks
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setXAxisMin(0);
        mRenderer.setXAxisMax(3);

        mRenderer.setYAxisMax(10000);
        mRenderer.setYAxisMin(0);
        //
        mRenderer.setOrientation(Orientation.VERTICAL);
        mRenderer.setXLabels(0);
        mRenderer.addXTextLabel(1, "Quota");
        mRenderer.addXTextLabel(2, "Current Sales");

        mRenderer.setPanEnabled(true, true); // will fix the chart position
        Intent intent = ChartFactory.getBarChartIntent(context, dataSet,
                mRenderer, Type.DEFAULT);

        return intent;
    }
}

1 个答案:

答案 0 :(得分:2)

    int x=8000, y=4000;

    CategorySeries series1 = new CategorySeries("Bar1");
    CategorySeries series2 = new CategorySeries("Bar2");

    series1.add(x);
    series2.add(y);

    XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset(); 
            dataSet.addSeries(series1.toXYSeries());
            dataSet.addSeries(series2.toXYSeries());

    XYSeriesRenderer renderer1 = new XYSeriesRenderer(); 
            renderer.setColor(Color.RED);

            renderer.setDisplayChartValues(true);
            renderer.setChartValuesSpacing((float) 5.5d);

            renderer.setLineWidth((float) 10.5d);

    XYSeriesRenderer renderer2 = new XYSeriesRenderer(); 
            renderer.setColor(Color.BLUE);

            renderer.setDisplayChartValues(true);
            renderer.setChartValuesSpacing((float) 5.5d);

            renderer.setLineWidth((float) 10.5d);

    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 
            mRenderer.addSeriesRenderer(renderer1);
            mRenderer.addSeriesRenderer(renderer2);