How do i add X-axis lables in Mpcharts for barcharts

喜欢而已 提交于 2019-12-12 04:19:30

问题


I was using an older version of MPCharts and in that it was working fine,but in 3.0.2,it seems to be a bit diferent,their wiki page isn't helping much.

this is what i have

 ArrayList<BarEntry> entries = new ArrayList();
    entries.add(new BarEntry(0,125f));
    entries.add(new BarEntry(1,233f));
    entries.add(new BarEntry(2,318f));
    entries.add(new BarEntry(3,12f));
    entries.add(new BarEntry(4,92f));
    entries.add(new BarEntry(5,72f));
    entries.add(new BarEntry(6,11f));
    entries.add(new BarEntry(7,456f));
    entries.add(new BarEntry(8,1567f));
    entries.add(new BarEntry(9,367f));
    entries.add(new BarEntry(10,753f));
    entries.add(new BarEntry(11, 11f));
    //entries.add(new BarEntry(233f, 12));
    //entries.add(new BarEntry(555f, 6));

    BarDataSet dataset = new BarDataSet(entries,"Rupees(₹)");
    int colors1[] = {Color.rgb(178,34,34)};
    dataset.setColors(colors1);

    ArrayList<String> labels = new ArrayList();
    labels.add("January");
    labels.add("February");
    labels.add("March");
    labels.add("April");
    labels.add("May");
    labels.add("June");
    labels.add("July");
    labels.add("August");
    labels.add("September");
    labels.add("October");
    labels.add("November");
    labels.add("December");

    BarData data = new BarData(dataset);
    barChart.setData(data);


    barChart.invalidate();

Now i'm not sure how should i add Jan,Feb,March to X axis,any help would be appreciated


回答1:


For the BarChart, u can use this to set the labels in xAxis

XAxis xAxis = barChart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));

For more information, look on its description: IndexAxisValueFormatter




回答2:


you have to use the value formatter.

Add entries like this

ArrayList<BarEntry> entryList = new ArrayList<BarEntry>();
for (int i = 0; i < yourValues.length; i++) {
     entryList.add(new BarEntry(i, yourValues[i]));
}

and set the value formatter for your chart

IAxisValueFormatter xAxisFormatter = new LabelFormatter(mChart);
XAxis xAxis = mChart.getXAxis();
xAxis.setValueFormatter(xAxisFormatter);

here is the LabelFormatter class:

public class LabelFormatter implements IAxisValueFormatter {

   private labels[] = {"Jan", "Feb", ..... }
   public LabelFormatter(BarLineChartBase<?> chart) {
        this.chart = chart;
    }
     @Override
    public String getFormattedValue(float value, AxisBase axis) {
        return label[(int)value];
    }
}

you can check this class for more information.



来源:https://stackoverflow.com/questions/44194550/how-do-i-add-x-axis-lables-in-mpcharts-for-barcharts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!