1
votes

I am using the MPAndroidChart library. I have some questions about BarChart.

Here is my chart data.

ArrayList<String> xVals = new ArrayList<String>();
xVals.add("AAA");
xVals.add("BBB");
xVals.add("Z1");
xVals.add("CCC");
xVals.add("DDD");
xVals.add("Z2");
xVals.add("EEE");
xVals.add("FFF");
xVals.add("Z3");

The BarChart data sets.

ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
ArrayList<BarEntry> yValsSet = new ArrayList<BarEntry>();
yVals.add(new BarEntry(42, 0));
yVals.add(new BarEntry(15, 1));
yValsSet.add(new BarEntry(57, 2));
yVals.add(new BarEntry(35, 3));
yVals.add(new BarEntry(14, 4));
yValsSet.add(new BarEntry(49, 5));
yVals.add(new BarEntry(7, 6));
yVals.add(new BarEntry(21, 7));
yValsSet.add(new BarEntry(28, 8));

And the chart. enter image description here

The "Z3" on x-axis didn't show.

And chart isn't at correct position.

The dark gray one is CCC's value, but it shows at Z1.

How to fix it?


UPDATE: If I use only one data set, it works great.

1

1 Answers

0
votes

The values that belong to each other (in terms of their x-axis label) must be on the same x-index.

Which means:

If you want to have the values 30 and 25 on the x-index with the description "AAA", you need to do the following:

ArrayList<String> xVals = new ArrayList<String>();
xVals.add("AAA");
xVals.add("BBB");
//... and so on

ArrayList<BarEntry> vals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> vals2 = new ArrayList<BarEntry>();

// this will be above "AAA"
vals1.add(new BarEntry(30, 0)); // use x-index 0 for both
vals2.add(new BarEntry(25, 0));

// this will be above "BBB"
vals1.add(new BarEntry(50, 1)); // use x-index 1 for both
vals2.add(new BarEntry(60, 1));