Reputation: 10788
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);
Upvotes: 8
Views: 5459
Reputation:
You just missed replacing the i * spaceForBar
by (count - i) * spaceForBar
.
So the coordinates sequence when rendering becomes (assuming count = 10
):
(10 * 2, 0 * 4) => (9 * 2, 1 * 4) => (8 * 2, 2 * 4) => ... => (0 * 2, 10 * 4)
So your code becomes:
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry> ();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random () * range);
yVals1.add (new BarEntry ((count - i) * spaceForBar, i * 4));
}
BarDataSet set1 = new BarDataSet (yVals1, "DataSet 1");
Upvotes: 2
Reputation: 213
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.
Upvotes: 4