Lynerd Lynerd - 4 months ago 258
Android Question

How to create a BarChart with grouped bars with MPAndroidChart?

How can i compare two set of data using

BarChart
of MPAndroidChart.

It should look like this image below:
enter image description here

I edited a code, I get from a sample project in github.
how can I put together
100f and 110f
value in one
Xaxis
label
Whole Number


Score score1 = new Score(100f, 0, "Whole Number");
mRealm.copyToRealm(score1);
Score score2 = new Score(110f, 0, "Whole Number");
mRealm.copyToRealm(score2);

Answer

Yes, that can be done quite easily.

What you need is a BarChart with multiple BarDataSets where each set (in your case) represents one sex (men or women).

Example code (without realm.io)

    List<String> xValues = ...; // "Denmark", "Finland", ...

    XAxis xAxis = chart.getXAxis();
    xAxis.setValueFormatter(new MyValueFormatter(xValues));

    // create 2 datasets 
    BarDataSet set1 = new BarDataSet(valuesMen, "Men");
    set1.setColor(Color.BLUE);
    BarDataSet set2 = new BarDataSet(valuesWomen, "Women");
    set2.setColor(Color.RED);

    BarData data = new BarData(set1, set2);
    chart.setData(data);
    chart.invalidate(); // refresh

If you want to "stack" values in a BarChart above each other, you need to create a stacked-barchart: Android Stacked Bars Chart