使用MPAndroidChart饼图显示不同的数据集

问题描述:

在我的应用程序中,我想在饼图上显示一些数据.我正在使用MPAndroidChart库,并按照文档操作,编写了一个外观漂亮的图表,正确显示了所有数据.

In my application I want to display some data on a pie chart. I'm using the MPAndroidChart library and following the documentation I managed to program a nice looking chart with all my data correctly displayed.

现在,我想改善图表,但遇到了一些麻烦.我的数据指的是一天,但有两类:收入和收入.到目前为止,我将它们作为单个PieDataSet处理(它们具有标签,因此很容易区分它们).现在,我想区分收入和收入,在同一个饼图中以不同的颜色显示它们.

Now, I'd like to improve my chart, but I'm having some trouble. My data refers to a single day, but there are two categories: incomes and revenues. Until now I've handled them as a single PieDataSet (they have labels, so it is quite easy to distinguish among them). Now I'd like to differentiate among incomes and revenues, to show them with different colors in the same pie chart.

我尝试按照链接(折线图部分)进行调整饼图,但Android Studio告诉我不能将List<IPieDataSet>用作PieData对象的构造函数的参数.这是代码:

I tried following this link (the line chart part) adapting it to pie charts, but Android Studio tells me that I can't use a List<IPieDataSet> as parameter for a constructor of a PieData object. Here is the code:

public static void drawPie( List<PieEntry> entriesU, List<PieEntry>entriesE, PieChart chart){
    PieDataSet set = new PieDataSet(entriesU,"uscite");
    PieDataSet set1 = new PieDataSet(entriesE,"entrate");
    List<IPieDataSet> dataSets = new ArrayList<>();
    dataSets.add(set);
    dataSets.add(set1);
    set.setSliceSpace(5);
    set1.setSliceSpace(5);
    PieData data = new PieData(dataSets);
    chart.setData(data);
}

我进行了很多搜索,但仍未找到此问题的答案.

I've searched a lot but I still haven't found an answer to this problem.

问题:

是否可以在同一饼图上显示多个数据集?如果可能的话,我该怎么办?

It is possible to display multiple data sets on the same pie chart or not? And if it is possible, how can I do it?

饼图是一个360度的圆圈,其中切片代表给定数据集的总数的百分比.这仅在单个IDataSet的上下文中才有意义.因此,与BarChartLineChart不同,PieChart不支持多个IDataSet.

A pie-chart is a single 360-degree circle where slices represent percentages of a total of a given dataset. This only makes sense in the context of a single IDataSet. Therefore, unlike BarChart and LineChart, a PieChart doesn't support multiple IDataSet.

我认为您真正想要的是为收入"和收入"使用不同的颜色.为此,您需要做的是创建一个解析颜色(不是颜色资源)的List<Integer>,该颜色在列表中的位置与将PieEntry添加到图表中的顺序相匹配.

I think what you really want is to use different colors for your "incomes" and "revenues". To do this, all you need to do is create a List<Integer> of resolved colors (not color resources) where the position in the list matches the order the PieEntry is added to the chart.

为了说明,假设我们有这个:

To illustrate, let's say we have this:

entries.add(new PieEntry(15f, "entrate1")); //revenue1
entries.add(new PieEntry(20f, "uscite1")); //expense1
entries.add(new PieEntry(10f, "entrate2")); //revenue2
entries.add(new PieEntry(50f, "uscite2")); //expense2

您只需创建一个与该颜色匹配的解析颜色List<Integer>:

You just create a List<Integer> of resolved colors that matches that:

List<Integer> colors = new ArrayList<Integer>();
colors.add(ContextCompat.getColor(context, R.color.red));
colors.add(ContextCompat.getColor(context, R.color.blue));
colors.add(ContextCompat.getColor(context, R.color.red));
colors.add(ContextCompat.getColor(context, R.color.blue));
dataSet.setColors(colors);

现在,您所有的收入均为红色,支出为蓝色.您甚至可以变得聪明,编写一个将PieEntry映射到颜色而不是手动完成的功能.

Now all your revenues are red and your expenses are blue. You can even be clever and write a function that maps PieEntry to colors rather than do it manually.

无论如何,此处的答案对于设置饼图切片的颜色是正确的.

In any case, this answer here is correct for setting colors for the pie chart slices.