Search code examples
androidbar-chartmpandroidchart

MPAndroidChart Weekly data


So I am trying to do a weekly chart with current weeks data vs previous weeks data using MPAndroidChart. I can do single bars just fine and everything lines up, but when I try to do multi bar groups it does the following.

enter image description here Is there something else I need to be doing? I only have 2 bars per group for each day. Here is my current code:

BarData barDataPayments = new BarData(barDataSetCurrentWeek, barDataSetPrevWeek);
        barChartPayments.setData(barDataPayments);

        XAxis xAxisPayments = barChartPayments.getXAxis();
        xAxisPayments.setValueFormatter(new IndexAxisValueFormatter(labelWeekdayNames));

        xAxisPayments.setYOffset(5);
        xAxisPayments.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxisPayments.setLabelCount(labelWeekdayNames.size());
        xAxisPayments.setGranularity(1f);
        xAxisPayments.setCenterAxisLabels(false);
        xAxisPayments.setDrawGridLines(false);

        float groupSpace = 0.08f;
        float barSpace = 0.03f;
        float barWidth = 0.2f;

        barDataPayments.setBarWidth(barWidth);
        barChartPayments.getXAxis().setAxisMinimum(0);
        barChartPayments.getXAxis().setAxisMaximum(0 + barChartPayments.getBarData().getGroupWidth(groupSpace, barSpace) * 14);
        barChartPayments.groupBars(0, groupSpace, barSpace);

        barChartPayments.invalidate();

Solution

  • So what I ended up having to do is manually set the data. It took a little tweaking to get it just how I wanted it, I am sure there is a more proper way, but so far this was the easiest way that I have currently found.

    xAxisMiles.setYOffset(0); // <---- CHANGED TO ZERO
    xAxisMiles.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxisMiles.setLabelCount(labelWeekdayNames.size());
    xAxisMiles.setGranularity(1f);
    xAxisMiles.setCenterAxisLabels(true); // <---- SET TO TRUE
    xAxisMiles.setDrawGridLines(false);
    
    //IMPORTANT *****
    barDataPayments.setBarWidth(0.40f);
    barDataPayments.getXAxis().setAxisMinimum(0);
    barDataPayments.getXAxis().setAxisMaximum(7);
    barDataPayments.groupBars(0, 0.10f, 0.05f);
    //***** IMPORTANT*/