Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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++) {
            c.moveToNext();     
            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>();
            dataSets.add(set1);
            dataSets.add(set2);
            dataSets.add(set3);

            BarData data = new BarData(xVals, dataSets);

            // add space between the dataset groups in percent of bar-width
            data.setGroupSpace(0);

            mChart.setData(data);
            mChart.invalidate();

}               
        c.close();  
    db.close();

}

the resultenter image description here

like image 881
user3608814 Avatar asked Nov 11 '14 23:11

user3608814


1 Answers

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:

BarDataSet.setColor(...);

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

like image 161
Philipp Jahoda Avatar answered Nov 11 '22 10:11

Philipp Jahoda