1

I am using MPAndroidChart to create grouped Dataset, but it is showing an error BarData data = new BarData(labels,dataSets ); at this line that is Unable to resolve constructor,BarData(java.util.ArrayList)<java.lang.String>,java.util.ArrayList<com.github.mikephil.charting.data.BarDataSet;> I have also casted IBarDataSet then the Application is getting Unfortunately Stopped. I have used dependency compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'. Somebody Please help me!!

//create BarEntry for group 1
            ArrayList<BarEntry> group1 = new ArrayList<>();
            group1.add(new BarEntry(4f, 0));
            group1.add(new BarEntry(8f, 1));
            group1.add(new BarEntry(6f, 2));
            group1.add(new BarEntry(12f, 3));
            group1.add(new BarEntry(18f, 4));
            group1.add(new BarEntry(9f, 5));

            // create BarEntry for group 2
            ArrayList<BarEntry> group2 = new ArrayList<>();
            group2.add(new BarEntry(6f, 0));
            group2.add(new BarEntry(7f, 1));
            group2.add(new BarEntry(8f, 2));
            group2.add(new BarEntry(12f, 3));
            group2.add(new BarEntry(15f, 4));
            group2.add(new BarEntry(10f, 5));

            // creating dataset for group1
            BarDataSet barDataSet1 = new BarDataSet(group1, "Brand 1");
            barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);

            // creating dataset for group2
            BarDataSet barDataSet2 = new BarDataSet(group2, "Brand 2");
            barDataSet2.setColors(ColorTemplate.COLORFUL_COLORS);

            // combined all dataset into an arraylist
            ArrayList<BarDataSet> dataSets = new ArrayList<>();
            dataSets.add(barDataSet1);
            dataSets.add(barDataSet2);


            ArrayList<String> labels = new ArrayList<>();
            labels.add("JAN");
            labels.add("FEB");
            labels.add("MAR");
            labels.add("APR");
            labels.add("MAY");
            labels.add("JUN");
            BarData data = new BarData(labels,dataSets );// initialize the Bardata with argument labels and dataSet
            barChartGroup.setData(data);

I want to get the dataset like the following example : -

Grouped DataSet

Thanks!!

Philipp Jahoda
  • 49,738
  • 23
  • 176
  • 183
Sampad
  • 1,323
  • 9
  • 14

3 Answers3

4

Since release v3.0.0 brought a major change related to the drawing of grouped BarChart, you should modify your dependency to get the newest release.

Having said that, how to define the BarData for your data groups? According to the example for "Grouped BarChart" from the official Wiki, you do it like this:

BarData data = new BarData(barDataSet1, barDataSet2 );
barChartGroup.setData(data);

The example also illustrates how to set bar width and gaps between bars/ groups of bars.

By the way, MPandroidChart relies on your entries being sorted. Unsorted lists may or may not be drawn correctly. In addition to that, at least in the example the two groups of entries had the same set of x values, so it may be a good idea to fill in missing values.

About the labels: it seems that presently it's not possible to set labels like you do. For a workaround using a custom ValueFormatter for the x axis see this SO post by TR4Android.

Community
  • 1
  • 1
Bö macht Blau
  • 12,404
  • 4
  • 33
  • 57
1

here is a full code

 private BarData generateBarData() {

    ArrayList<BarEntry> entries1 = new ArrayList<>();
    ArrayList<BarEntry> entries2 = new ArrayList<>();

    for (int index = 0; index < count; index++) {
        entries1.add(new BarEntry(0, getRandom(25, 25)//your data));

        // stacked
        entries2.add(new BarEntry(0, new float[]{getRandom(13, 12)//your data, getRandom(13, 12)//your data}));
    }

    BarDataSet set1 = new BarDataSet(entries1, "Bar 1");
    set1.setColor(Color.rgb(60, 220, 78));
    set1.setValueTextColor(Color.rgb(60, 220, 78));
    set1.setValueTextSize(10f);
    set1.setAxisDependency(YAxis.AxisDependency.LEFT);

    BarDataSet set2 = new BarDataSet(entries2, "");
    set2.setStackLabels(new String[]{"Stack 1", "Stack 2"});
    set2.setColors(Color.rgb(61, 165, 255), Color.rgb(23, 197, 255));
    set2.setValueTextColor(Color.rgb(61, 165, 255));
    set2.setValueTextSize(10f);
    set2.setAxisDependency(YAxis.AxisDependency.LEFT);

    float groupSpace = 0.06f;
    float barSpace = 0.02f; // x2 dataset
    float barWidth = 0.45f; // x2 dataset
    // (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"

    BarData d = new BarData(set1, set2);
    d.setBarWidth(barWidth);

    // make this BarData object grouped
    d.groupBars(0, groupSpace, barSpace); // start at x = 0

    return d;
}

credit https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java

Energy
  • 752
  • 11
  • 17
0

There is a good example here, check it out.

Below is the resulting Grouped BarChart Group Bar Chart

corneliouz Bett
  • 141
  • 1
  • 10