user3608814 user3608814 - 1 year ago 221
Android Question

MPAndroidChart BarChart Grouped DataSets

I am using the MPAndroidChart library release v2. I'm trying to show 3 bars, with data taken from the database.
Unfortunately, instead of seeing 3 bars with their data, viewing them with the same result. See the screenshot. Thanks for your help.

int [] x = {1,2,3};
Cursor c = db.rawQuery(sql, null);
int count = c.getCount();

float value1 ;
float value2 ;
float value3 ;
String[] mesi = new String[count];

for(int n=0; n<count; n++) {
mesi[n]= c.getString(0);
value1 = c.getFloat(1);
value2 = c.getFloat(2);
value3 = c.getFloat(3);

ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i <x.length; i++) {
xVals.add(x.length + " " + mChart.getUnit());

ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();

for (int i = 0; i < x.length; i++) {
yVals1.add(new BarEntry(value1, i));

for (int i = 0; i < x.length; i++) {
yVals2.add(new BarEntry(value2, i));

for (int i = 0; i < x.length; i++) {
yVals3.add(new BarEntry(value3, i));

// create 3 datasets with different types
BarDataSet set1 = new BarDataSet(yVals1, "Company A");
set1.setColor(Color.rgb(104, 241, 175));
BarDataSet set2 = new BarDataSet(yVals2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
BarDataSet set3 = new BarDataSet(yVals3, "Company C");
set3.setColor(Color.rgb(242, 247, 158));

ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();

BarData data = new BarData(xVals, dataSets);

// add space between the dataset groups in percent of bar-width




the resultenter image description here

Answer Source

Did you make a logcat output of the values the chart should display? Maybe they are not stored correctly in the database.

Your code seems correct to me.

You could also test it by simply providing a predefined value like e.g. 50 for all bars and see if it is plotted correctly.

To change the color call:


UPDATE: There is now a very detailed tutorial on how to create grouped BarCharts available on the official GitHub page of the library, based on release v3.0.0: Grouped BarChart tutorial

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download