简体   繁体   English

MPAndroidChart条形图 - 如何在组之间对随机x轴间隔的条形图进行分组?

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

I want to make a bar chart with 3 different datasets grouped together at each data point like so: 我想制作一个条形图,其中3个不同的数据集在每个数据点组合在一起,如下所示:

在此输入图像描述

However, I am unable to group the bars together using the library's provided groupBars method because no matter what x-value I set for an entry, it groups the bars according to the interval I specify in its parameters. 但是,我无法使用库提供的groupBars方法将条形组合在一起,因为无论我为条目设置了什么x值,它都会根据我在参数中指定的间隔对条形图进行分组。

For example, if I generate a dataset with entry x-values {0, 5, 13, 17...50} and call `groupBars', all of my entries are gathered 1 x-value apart like so: 例如,如果我生成一个带有条目x值{0,5,13,​​17 ... 50}并调用`groupBars'的数据集,那么我的所有条目都会被收集1 x值,如下所示:

在此输入图像描述

What I want is the bars to each be grouped and each be visible at their specified x-value. 我想要的是每个被分组的条形图,每个条形图在其指定的x值处可见。 If I simply remove the groupBars call, I get something similar to what I want but not quite since the bars are all overlapping, like so: 如果我只是删除groupBars调用,我会得到类似于我想要的东西但不完全,因为条形图都是重叠的,如下所示:

在此输入图像描述

How do I achieve a result similar to the above image but with the bar of each dataset completely visible? 如何获得与上述图像类似的结果,但每个数据集的条形图是否完全可见? Here is my code for generating the dataset and grouping the bars: 这是我生成数据集和分组条形码的代码:

        ArrayList<BarEntry> happinessValues = new ArrayList<>();
        ArrayList<BarEntry> stressValues = new ArrayList<>();
        ArrayList<BarEntry> painValues = new ArrayList<>();

        for (int i = 0; i < 50; ++i) {
            happinessValues.add(new BarEntry(
                    i,
                    datapoint.getHappiness()));
            stressValues.add(new BarEntry(
                    i,
                    datapoint.getStress()));
            painValues.add(new BarEntry(
                    i,
                    datapoint.getPain()));
        }

        HappinessDataset happyDataset;

        BarDataSet stressDataset, painDataset;

            happyDataset = new HappinessDataset(happinessValues, "Happiness");

            stressDataset = new BarDataSet(stressValues, "Stress");

            painDataset = new BarDataSet(painValues, "Pain");

            BarData data = new BarData(happyDataset, stressDataset, painDataset);

            mChart.setData(data);

        mChart.getXAxis().setAxisMinimum(0);
        mChart.getXAxis().setAxisMaximum(50);


      float groupSpace = 0.4f;
        float barSpace = 0f; // x3 DataSet
        float barWidth = 0.2f; // x3 DataSet
        // (0.2 + 0) * 3 + 0.4 = 1.00 -> interval per "group"
        mChart.groupBars(startTime, groupSpace, barSpace);

I have solved the problem by modifying the x-values of each bar-entry and the bar width. 我通过修改每个条形条的x值和条宽来解决了这个问题。

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 (ie the three bars together will take up 0.6 units in space, and there will be 0.4 unit of spacing after the dataset). 我创建了一个包含三个数据集的新BarData类,并将条宽度(我们称之为BAR_WIDTH )设置为0.2(即三个条在一起将占用空间中的0.6个单位,并且在数据集之后将有0.4个单位的间距) 。

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 . 对于任何给定的条形条目,我将第一个条形图放在我想要的x值(让我们称之为i ),第二个条形图位于x-value i+BAR_WIDTH ,第三个条形图位于i+2*BAR_WIDTH The result is a group of 3 bar entries centered at any x-value I want, like so: 结果是一组以我想要的任何x值为中心的3个条形条目,如下所示:

http://i.imgur.com/KrKTE45l.jpg

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 在使用setCenterAxisLabels之前

在此输入图像描述

AFTER setting setCenterAxisLabels to true 将setCenterAxisLabels设置为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 如果你不确定分组条形图的数量(这里是4)那么

    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 (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. 所有这些空格的总和应该等于1,以使标签与分组条形图的中心对齐。

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. 为了将X轴标签设置为条形组的中心。

Please refer to GroupedBarChart section of this for further details. 请参考的GroupedBarChart节对于进一步的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM