Search code examples
androidchartsmpandroidchart

How do I move bar to center in chart in MPAndroidChart?


I'm using the MPAndroidChart.

It's nice library but I couldn't have solved a problem.

This is result be executed.

enter image description here

The axis are well to center align but bar groups are no center align.

I would like to move bar groups to center too.

And, This is entire code.

        float barWidth = 0.1f;
        float barSpace = 0f;
        float groupSpace = 0.1f;
        int groupCount = 3;

        distinctionGraph = (BarChart) findViewById(R.id.distinction_graph);
        verificationGraph = (BarChart) findViewById(R.id.verification_graph);
        sentenceGraph = (BarChart) findViewById(R.id.sentence_graph);

        ArrayList xVals = new ArrayList();

        xVals.add("3월");
        xVals.add("2월");
        xVals.add("1월");

        ArrayList yVals1 = new ArrayList();
        ArrayList yVals2 = new ArrayList();
        ArrayList yVals3 = new ArrayList();

        yVals1.add(new BarEntry(1, (float) 1));
        yVals2.add(new BarEntry(1, (float) 2));
        yVals3.add(new BarEntry(1, (float) 2));

        yVals1.add(new BarEntry(2, (float) 3));
        yVals2.add(new BarEntry(2, (float) 4));
        yVals3.add(new BarEntry(2, (float) 4));

        yVals1.add(new BarEntry(3, (float) 5));
        yVals2.add(new BarEntry(3, (float) 6));
        yVals3.add(new BarEntry(3, (float) 6));

        BarDataSet set1, set2, set3;
        set1 = new BarDataSet(yVals1, "쉬움");
        set1.setColor(Color.parseColor("#3EBB9B"));
        set2 = new BarDataSet(yVals2, "보통");
        set2.setColor(Color.parseColor("#3698DB"));
        set3 = new BarDataSet(yVals3, "어려움");
        set3.setColor(Color.parseColor("#2C80B9"));

        BarData data = new BarData(set1, set2, set3);
        data.setValueFormatter(new LargeValueFormatter());

        distinctionGraph.setData(data);
        distinctionGraph.getBarData().setBarWidth(barWidth);
        distinctionGraph.getXAxis().setAxisMinimum(0);
        distinctionGraph.getXAxis().setAxisMaximum(0 + distinctionGraph.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
        distinctionGraph.groupBars(0, groupSpace, barSpace);
        distinctionGraph.getData().setHighlightEnabled(false);
        distinctionGraph.invalidate();

        distinctionGraph.setDescription(null);
        distinctionGraph.setPinchZoom(false);
        distinctionGraph.setScaleEnabled(false);
        distinctionGraph.setDrawBarShadow(false);
        distinctionGraph.setDrawGridBackground(false);

        Legend l = distinctionGraph.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(true);
        l.setYOffset(20f);
        l.setXOffset(0f);
        l.setYEntrySpace(0f);
        l.setTextSize(8f);

        //X-axis
        XAxis xAxis = distinctionGraph.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setGranularityEnabled(true);
        xAxis.setCenterAxisLabels(true);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMaximum(3);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
        //Y-axis
        distinctionGraph.getAxisRight().setEnabled(false);
        YAxis leftAxis = distinctionGraph.getAxisLeft();
        leftAxis.setValueFormatter(new LargeValueFormatter());
        leftAxis.setDrawGridLines(true);
        leftAxis.setSpaceTop(35f);
        leftAxis.setAxisMinimum(0f);

I set 3 for groupCount but not working. Do I miss something?

For example, like this.

enter image description here

Needs help. Thank you :)


Solution

  • I solved it by myself.

    First, I changed variable like below.

        float barWidth = 0.3f;
        float barSpace = 0.0f;
        float groupSpace = 0.06f;
        int groupCount = 3;
    

    The point was barWidth. I Changed it 0.0f to 0.3f.

    0.3 * 3 = 0.9.

    it's close to 1.

    And, I modified groupBars like below.

    distinctionGraph.groupBars(groupSpace, groupSpace, barSpace);
    

    First Argument of groupBar is fromX. Before modification, groupSpace was 0.

    This is screenshot after I solved it.

    enter image description here