9

I'm using the MPAndroidChart library, trying to customize the Stacked Bars. The objective is to display the differences between 2 currencies. So suppose this example for January. Currency #1 = 8; Currency #2 = 12

The idea is to show in the same bar both values, but instead of sum the 2 values ( 8 + 12) to have the value 8 "in front" and the value 12 "behind". So the total high of the bar is the max value.

I tried to do that, extending the BarEntry class, and instead of calculate the sum, just set the max value. But I'm seeing only one bar now (the one with value = 12)

Do you know if this kind of logic is supported? Any advice?

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
psabbate
  • 767
  • 1
  • 5
  • 22

1 Answers1

12

Check out the guide on how to create stacked-bar-charts, as well as the example.

Create an entry in the stacked-bar-chart in the following way:

BarEntry entry = new BarEntry(xValue, new float[] { 8f, 12f });

This will create an entry with a total height of 20, consisting of two different values (8 and 12). The "xValue" is the position this bar will show up on the x-axis.

You can use as many entries for the stack (float array) as you want. If you intend to do only single entries (no stacks), do not use the BarEntry constructor that takes a float array, use the one that only takes a single value.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • 3
    I was able to do that. Following your example, what I need to do is to show yval2 "behind" yval1, so instead of a bar with height = 20, the bar I'm expecting is height = 12. And in front of that bar, yval1 with height = 8. I was thinking that maybe I could create a yval1 = 8 and yval2 = 12 - yval1, but the problem are the labels with the amounts. – psabbate Feb 07 '15 at 21:05
  • @Philipp Can I add Third and fourth values like yval3 , yval4 will it work that way – Vinod Kumar Mar 22 '16 at 12:41
  • Any update on this issue? @psabbate were you able to solve this? – Gurupad Mamadapur Mar 21 '17 at 18:37
  • @Jahoda how to get the single stack – kartheeki j Oct 27 '18 at 12:03
  • @PhilippJahoda is there a sample for RealmBarDataSet ? I can see there is a constructor – PedroAGSantos Oct 07 '19 at 13:32
  • Yes, have a look [here](https://github.com/PhilJay/MPAndroidChart-Realm/blob/master/MPChartLib-Realm/src/main/java/com/github/mikephil/charting/data/realm/implementation/RealmBarDataSet.java#L66) – Philipp Jahoda Oct 08 '19 at 11:00