MPAndroidChart条形图 - 如何在组之间对随机x轴间隔的条形图进行分组? [英] MPAndroidChart Bar Chart - how to group bars with random x-axis intervals in between groups?

查看:298
本文介绍了MPAndroidChart条形图 - 如何在组之间对随机x轴间隔的条形图进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个数据点制作一个包含3个不同数据集的条形图,如下所示:

I want to make a bar chart with 3 different datasets grouped together at each data point like so:

但是,我无法使用库提供的 groupBars 方法将条形图组合在一起,因为无论什么x-我为一个条目设置的值,它根据我在参数中指定的间隔对条形图进行分组。

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.

例如,如果我生成一个数据集,其中包含条目x值{0,5,13,​​17 ... 50}并调用groupBars,那么我的所有数据集都是条目被分开收集1 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值处可见。如果我只是删除 groupBars 调用,我会得到类似于我想要的东西但不完全,因为条形图都是重叠的,如下所示:

What I want is the bars to each be grouped and each be visible at their specified x-value. 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:

如何获得与上图类似的结果,但是每个数据集完全可见?这是我生成数据集和分组条形码的代码:

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);


推荐答案

我通过修改x值解决了这个问题每个条形条和条形宽度。

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

我用三个数据集创建一个新的BarData类并设置条形宽度(让我们称之为 BAR_WIDTH )为0.2(即三个条形在空间中占据0.6个单位,数据集后将有0.4个单位间距)。

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).

对于任何给定的条形码,我将第一个条形图放在我想要的x值上(让我们称之为 i ),我的第二个栏位于x值 i + BAR_WIDTH ,第三个栏位于 i + 2 * BAR_WIDTH 。结果是一组以我想要的任何x值为中心的3个条形条目,如下所示:

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);

这篇关于MPAndroidChart条形图 - 如何在组之间对随机x轴间隔的条形图进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆