MPAndroidChart Bar Chart - how to group bars with random x-axis intervals in between groups?

我怕爱的太早我们不能终老 提交于 2019-12-05 19:40:51

I have solved the problem by modifying the x-values of each bar-entry and the bar width.

I create a new BarData class with the three datasets and set the bar width (let's call it BAR_WIDTH) to be 0.2 (i.e. the three bars together will take up 0.6 units in space, and there will be 0.4 unit of spacing after the dataset).

For any given bar entry, I place my first bar at the x-value I want (lets call it i), my second bar at x-value i+BAR_WIDTH, and third bar at i+2*BAR_WIDTH. The result is a group of 3 bar entries centered at any x-value I want, like so:

So in my above code, modify the bar-entry creation code to be as follows:

final float BAR_WIDTH = 0.2f;

     happinessValues.add(new BarEntry(
                        i,
                        datapoint.getHappiness()));
                stressValues.add(new BarEntry(
                        i + BAR_WIDTH,
                        datapoint.getStress()));
                painValues.add(new BarEntry(
                        i + 2 * BAR_WIDTH,
                        datapoint.getPain()));

mChart.getBarData().setBarWidth(BAR_WIDTH);

Two things that you have missed out is:

  1. mChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);

  2. mChart.getXAxis().setCenterAxisLabels(true);

BEFORE using setCenterAxisLabels

AFTER setting setCenterAxisLabels to true

this is the code snippet I am using to align labels to center of each set of group

float barSpace = 0.02f;
    float groupSpace = 0.3f;
    int groupCount = 4;

    data.setBarWidth(0.15f);
    barChart.getXAxis().setAxisMinimum(0);
    barChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
    barChart.groupBars(0, groupSpace, barSpace); // perform the "explicit" grouping

If you are not sure about the number of grouped bar graph (here it's 4) then

    float defaultBarWidth = -1;
    int groupCount = xAxisValues.size();

    defaultBarWidth = (1 - groupSpace)/barDataSets.size()  - barSpace;
        if(defaultBarWidth >=0) {
            barData.setBarWidth(defaultBarWidth);
        }
   if(groupCount != -1) {
            mChart.getXAxis().setAxisMinimum(0);
            mChart.getXAxis().setAxisMaximum(0 + mChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
            mChart.getXAxis().setCenterAxisLabels(true);
        }

Here the barwidth is calculated as follows

(barwidth + barspace) * no of bars + groupspace = 1

the sum of all this spaces should be equal to 1 for label to be aligned to center of grouped bar graph.

I think the solution is to use

barChart.groupBars(fromX, groupSpace, barSpace);
barChart.notifyDataSetChanged()

along with

XAxis xAxis = chart.getXAxis();
xAxis.setCenterAxisLabels(true);

in order to set X-Axis label into the centre of the bar group.

Please refer to GroupedBarChart section of this for further details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!