Show different datasets with an MPAndroidChart pie chart

淺唱寂寞╮ 提交于 2019-12-12 03:08:59

问题


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.

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.

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.

Question:

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?


回答1:


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.

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

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

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.



来源:https://stackoverflow.com/questions/41870505/show-different-datasets-with-an-mpandroidchart-pie-chart

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