Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart: How to reverse the order of a HorizontalBarChart?

Just hoping someone here could explain how to reverse the order of the HorizontalBarChart which is shown in the screenshot of MPAndroidChart (so instead of 44.0 being at the top it'd be at the bottom).

The code below shows how I create the BarDataSet which is used to produce the HorizontalBarChart.

ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range);
            yVals1.add(new BarEntry(i * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1");

I have tried to reverse the for loop so the dataset would be added the reverse way but it seems that the same HorizontalBarChart is produced.

ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = count-1; i >= 0; i--) {
            float val = (float) (Math.random() * range);
            yVals1.add(new BarEntry(i * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1");

Hopefully someone will be able to show me a way to reverse the order of this chart.

I already have tried the code below which is what one of the answers suggested. I had previously found the exact same link they shared. This line of code flips the graph so it's on the other side (see second screenshot)

mChart.getAxisLeft().setInverted(true);

Screenshot of HorizontalBarChart

Screenshot of HorizontalBarChart with inverted left axis

like image 321
Mark O'Sullivan Avatar asked Jan 14 '17 21:01

Mark O'Sullivan


1 Answers

chart.getAxisLeft().setInverted(true);

Taken from GitHub PhilJay/MPAndroidChart.

Yes. You can invert y-axes if you want to: chart.getAxisLeft().setInverted(true); This will let high values appear on the bottom of the chart, low values on top. The x-axis cannot be manipulated in order. Be aware that this feature is not included in the latest pre-release, only in the commits ahead.

like image 77
user0815 Avatar answered Nov 03 '22 00:11

user0815