8

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

2
  • I see two weird behaviours of mpandroidchart... 1. You tried to invert the YAxis but obviously the XAxis changed and 2. Once inverted, the XAxis labels at the top changed (to descending) but at the bottom the order is still normal (ascending)... I don't know if this is a bug and this should be changed to descending too
    – user0815
    Jan 15, 2017 at 13:10
  • 1
    @user0815 see after trying this for a while I became more and more convinced this was an issue with the library but thought I'd post this question in case I had missed something. If you want to follow the issue, it can be found here: github.com/PhilJay/MPAndroidChart/issues/2715 Jan 16, 2017 at 14:46

2 Answers 2

4

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.

3
  • Doesn't work. I've updated the question with a screenshot of what happens when you include the code needed for inverting the left axis. Jan 15, 2017 at 9:49
  • This is weird... According to the docs this should work setInverted(boolean enabled): If set to true, this axis will be inverted which means the highest value will be on the bottom, the lowest value on top.
    – user0815
    Jan 15, 2017 at 12:41
  • Note that "axis left" in a HorizontalBarChart is actually the top axis and likewise "axis right" is the bottom axis. So this is not what the questioner wants. The questioner wants to invert the "x axis". Feb 22, 2019 at 11:06
2
+100

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");
4
  • 1
    This fixed me problem! Thank you! Instead of using count - i, I just created a new int x before the start of the for loop and set it to the value of count and then just deducted 1 at the end of each loop with x--;. Jan 27, 2017 at 15:18
  • For some reason, this trick doesn't work on horizontal ground bar chart. from some reason!
    – Ofcourse
    Feb 27, 2020 at 23:29
  • Actually, I'm not sure why this trick should works. Maybe it doesn't work in the latest version as this question was asked few years ago.
    – Ofcourse
    Feb 27, 2020 at 23:45
  • I tried this. The order does change but along with that the X-Axis labels (maybe because the BarEntry is being modified, which changes the axis label --> value mapping). Had to add some changes on XAxis for getting the right labels. xAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float value) { return String.valueOf((int) (count - value)); } });
    – Mihir Shah
    Feb 1, 2022 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.