OPunktSchmidt
OPunktSchmidt

Reputation: 721

MPAndroidChart grouped Barchart: Group title not showed above group

I have a Barchart with 4 bars which are grouped into 2 groups using MPAndroidChart library. This works so far. Now i want to display a title above each group. This is my current output (as you can see, the titles are not showed above each group).

enter image description here

My code:

   _overviewBarChart.setPinchZoom(false);
    _overviewBarChart.getDescription().setEnabled(false);
    _overviewBarChart.setDrawValueAboveBar(true);
    _overviewBarChart.getXAxis().setCenterAxisLabels(true);
    _overviewBarChart.getXAxis().setAxisMinimum(0);
    _overviewBarChart.getXAxis().setDrawGridLines(false);

private void updateOverviewBarChart() {

    //Init calorie needs bars

    BarEntry todayCalorieNeedsBarEntry = new BarEntry(0, _calorieEntry.getTarget());
    BarEntry yesterdayCalorieNeedsBarEntry = new BarEntry(0, 0);
    if (_yesterdayCalorieEntry != null)
        yesterdayCalorieNeedsBarEntry = new BarEntry(0, _yesterdayCalorieEntry.getTarget());

    ArrayList<BarEntry> calorieNeedsBarEntries = new ArrayList<>();
    calorieNeedsBarEntries.add(todayCalorieNeedsBarEntry);
    calorieNeedsBarEntries.add(yesterdayCalorieNeedsBarEntry);

    //Init consumed calories bars

    BarEntry todayConsumedCaloriesBarEntry = new BarEntry(1, _calorieEntry.getConsumed());
    BarEntry yesterdayConsumedCaloriesBarEntry = new BarEntry(1, 0);
    if (_yesterdayCalorieEntry != null)
        yesterdayConsumedCaloriesBarEntry = new BarEntry(1, _yesterdayCalorieEntry.getConsumed());

    ArrayList<BarEntry> consumedCaloriesBarEntries = new ArrayList<>();
    consumedCaloriesBarEntries.add(todayConsumedCaloriesBarEntry);
    consumedCaloriesBarEntries.add(yesterdayConsumedCaloriesBarEntry);

    //Init BarDataSets

    BarDataSet calorieNeedsBarDataSet = new BarDataSet(calorieNeedsBarEntries, getString(R.string.fragment_main_calorieneeds));
    calorieNeedsBarDataSet.setColor(Color.parseColor("#26A69A"));
    BarDataSet consumedCaloriesBarDataSet = new BarDataSet(consumedCaloriesBarEntries, getString(R.string.fragment_main_consumed));
    consumedCaloriesBarDataSet.setColor(Color.parseColor("#E53935"));

    //Init BarData, group BarEntrys, set group titles

    BarData barData = new BarData(calorieNeedsBarDataSet, consumedCaloriesBarDataSet);
    barData.setValueTextSize(14);
    barData.setBarWidth(0.2f);
    barData.groupBars(0, 0.15f, 0.1f);

    ArrayList<String> groupTitles = new ArrayList<String>();
    groupTitles.add(getString(R.string.fragment_main_today));
    groupTitles.add(getString(R.string.fragment_main_yesterday));

    _overviewBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(groupTitles));// new BarChartStringFormatter(groupTitles));

    //Set data, redraw BarChart

    _overviewBarChart.setData(barData);
    _overviewBarChart.invalidate();
}

Can you see what i'm doing wrong?

Upvotes: 0

Views: 980

Answers (1)

OPunktSchmidt
OPunktSchmidt

Reputation: 721

Finally i found the solution for my problem.

First you have to add this two lines of code (note: The parameters can vary for your requirements):

_overviewBarChart.getXAxis().setGranularity(1f);
_overviewBarChart.getXAxis().setAxisMaximum(2);

My mistake was that the default XAxis is to finely granulated (0.1, 0.2, 0.3 ...). So IndexAxisValueFormatter applies the group title to each XAxis step. setGranularity resolves this. Finally setAxisMaximum is used to restrict the amount of steps on the XAxis (previous was 3 so the title showed 3 times).

And then change...

barData.groupBars(0, 0.15f, 0.1f);

to this (note: The parameters can vary for your requirements)..

barData.groupBars(0, 0.45f, 0.1f);

Result:

enter image description here

Upvotes: 1

Related Questions