如何使用 MPAndroidChart 创建带有分组条的条形图? [英] How to create a BarChart with grouped bars with MPAndroidChart?

查看:41
本文介绍了如何使用 MPAndroidChart 创建带有分组条的条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用

我编辑了一个代码,我从 github 中的一个示例项目中获取.如何将 100f 和 110f 值放在一个 Xaxis 标签 Whole Number

 Score score1 = new Score(100f, 0, "Whole Number");mRealm.copyToRealm(score1);Score score2 = new Score(110f, 0, "整数");mRealm.copyToRealm(score2);

解决方案

是的,这很容易做到.

您需要的是一个带有多个 BarDataSetsBarChart,其中每个集合(在您的情况下)代表一种性别(男性或女性).

示例代码(不含realm.io)

 ListxValues = ...;//"丹麦", "芬兰", ...XAxis xAxis = chart.getXAxis();xAxis.setValueFormatter(new MyValueFormatter(xValues));//创建 2 个数据集BarDataSet set1 = new BarDataSet(valuesMen, "Men");set1.setColor(Color.BLUE);BarDataSet set2 = new BarDataSet(valuesWomen, "Women");set2.setColor(Color.RED);BarData 数据 = new BarData(set1, set2);图表.setData(数据);chart.groupBars(...);//自发布 v3.0.0 起可用chart.invalidate();//刷新

如果您需要进一步的帮助,这里是关于分组详细教程code>BarChart 在 wiki 上可用.

如果您想将 BarChart 中的值相互堆叠",则需要创建一个堆叠条形图:Android 堆叠条形图

How can i compare two set of data using BarChart of MPAndroidChart.

It should look like this image below:

I edited a code, I get from a sample project in github. how can I put together 100f and 110f value in one Xaxis label Whole Number

        Score score1 = new Score(100f, 0, "Whole Number");
        mRealm.copyToRealm(score1);
        Score score2 = new Score(110f, 0, "Whole Number");
        mRealm.copyToRealm(score2);

解决方案

Yes, that can be done quite easily.

What you need is a BarChart with multiple BarDataSets where each set (in your case) represents one sex (men or women).

Example code (without realm.io)

    List<String> xValues = ...; // "Denmark", "Finland", ...

    XAxis xAxis = chart.getXAxis();
    xAxis.setValueFormatter(new MyValueFormatter(xValues));

    // create 2 datasets 
    BarDataSet set1 = new BarDataSet(valuesMen, "Men");
    set1.setColor(Color.BLUE);
    BarDataSet set2 = new BarDataSet(valuesWomen, "Women");
    set2.setColor(Color.RED);

    BarData data = new BarData(set1, set2);
    chart.setData(data);
    chart.groupBars(...); // available since release v3.0.0
    chart.invalidate(); // refresh

If you need further assistance, here is a detailed tutorial on grouped BarChart available on the wiki.

If you want to "stack" values in a BarChart above each other, you need to create a stacked-barchart: Android Stacked Bars Chart

这篇关于如何使用 MPAndroidChart 创建带有分组条的条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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