其他分享
首页 > 其他分享> > android – 使用AchartEngine的条形图

android – 使用AchartEngine的条形图

作者:互联网

我使用aChartEngine库创建了条形图.任何人都可以帮助我如何增加条的宽度和将条位置在X轴的中心?

这是我的BarGraph类:

public class BarGraph {
public Intent getIntent(Context context){
     XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
     double[] range = {0,5,0,5};

        int[] y = {24,30,12};
        CategorySeries series1 = new CategorySeries("Today");
        CategorySeries series2 = new CategorySeries("Next day");
        CategorySeries series3 = new CategorySeries("2 days later");

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

     for (int i=0;i<3;i++){
         switch(i){
         case 0:
                series1.add(y[0]);
                dataset.addSeries(series1.toXYSeries());
                break;
         case 1:
                series2.add(y[1]);
                dataset.addSeries(series2.toXYSeries());
                break;
         case 2:
                series3.add(y[2]);
                dataset.addSeries(series3.toXYSeries());
                break;
         }
     }

    XYSeriesRenderer renderer1 = new XYSeriesRenderer();
    renderer1.setColor(Color.GREEN);
    XYSeriesRenderer renderer2 = new XYSeriesRenderer();
    renderer2.setColor(Color.BLUE);
    XYSeriesRenderer renderer3 = new XYSeriesRenderer();
    renderer3.setColor(Color.RED);
        mrenderer.setAxisTitleTextSize(16);
        mrenderer.setChartTitleTextSize(20);
        mrenderer.setLabelsTextSize(15);
        mrenderer.setLegendTextSize(15);
        mrenderer.setBarSpacing(0);
        mrenderer.setXLabels(1);
        mrenderer.addXTextLabel(2.5, "Outstanding jobs");
        mrenderer.setMargins(new int[] {20, 30, 15, 0});
        mrenderer.setAxesColor(Color.WHITE);
        mrenderer.setChartTitle("Work per day");
        mrenderer.setXTitle("Days");
        mrenderer.setInitialRange(range, 1);
        mrenderer.setBarSpacing(1);
        mrenderer.setXAxisMin(0);
        mrenderer.setXAxisMax(5);
        mrenderer.setYAxisMin(0);
        mrenderer.setPanEnabled(false, false);
        mrenderer.setZoomEnabled(false, false);
        mrenderer.addSeriesRenderer(renderer1);
        mrenderer.addSeriesRenderer(renderer2);
        mrenderer.addSeriesRenderer(renderer3);
    Intent intent = ChartFactory.getBarChartIntent(context, dataset, mrenderer, Type.DEFAULT);
    return intent;  
}}

这是我得到的条形图的截图,

我需要这样的条形图,

我怎样才能实现这一目标?请帮我.

解决方法:

如果你想设置条的宽度 –

试试这个…

    renderer.setBarSpacing(-0.7);

标签:achartengine,android,bar-chart
来源: https://codeday.me/bug/20190826/1728800.html