MPAndroidChart - 饼图的所有部分都是相同的颜色 [英] MPAndroidChart - all pieces of the pie chart are the same colour
问题描述
我使用 MPAndroidChart (
文档说:
<块引用>当添加一些额外的样式时,生成的 PieChart 带有上面使用的数据可能与此类似
这些不是真正的颜色,而是颜色的标签:
pieChartEntries.add(new PieEntry(18.5f, "Green"));pieChartEntries.add(new PieEntry(26.7f, "Yellow"));pieChartEntries.add(new PieEntry(24.0f, "Red"));pieChartEntries.add(new PieEntry(30.8f, "Blue"));
要为您使用的饼图添加颜色:
set.setColors(new int[]{Color.parseColor("#FF32DA64"),Color.parseColor("#FF32DAD4"),Color.parseColor("#FFB853F2"),Color.parseColor("#FFF2ED53")});
或者您可以使用其中一种模板:
set.setColors(ColorTemplate.COLORFUL_COLORS);
我刚刚看到您正在使用资源中的颜色,如果您检查 setColors
方法,您应该看到:
如果您使用资源中的颜色,请确保颜色已经准备好了(通过调用 getResources().getColor(...))
因此,在您的情况下,您需要将资源解析为 Color
对象,然后再将它们添加到 set
.
I am using MPAndroidChart (https://github.com/PhilJay/MPAndroidChart) library to generate pie chart. I followed multiple tutorials including the wiki page, but when I create my pie chart all of the pieces are the same color. Any idea how can I solve this ?
Code:
PieChart mChart = (PieChart) findViewById(R.id.piechart);
List<PieEntry> pieChartEntries = new ArrayList<>();
pieChartEntries.add(new PieEntry(18.5f, "Green"));
pieChartEntries.add(new PieEntry(26.7f, "Yellow"));
pieChartEntries.add(new PieEntry(24.0f, "Red"));
pieChartEntries.add(new PieEntry(30.8f, "Blue"));
PieDataSet set = new PieDataSet(pieChartEntries, "Emotion Results");
PieData data = new PieData(set);
mChart.setData(data);
set.setColors(R.color.pieColour1,R.color.pieColour2,R.color.pieColour3,R.color.pieColour4,R.color.pieColour5,R.color.pieColour6,R.color.pieColour7,R.color.pieColour8);
mChart.invalidate();
The documentation says:
When adding some additional styling, the resulting PieChart with the data used above could look similar to this
These are not the real colors, but oly the labels for the colors:
pieChartEntries.add(new PieEntry(18.5f, "Green"));
pieChartEntries.add(new PieEntry(26.7f, "Yellow"));
pieChartEntries.add(new PieEntry(24.0f, "Red"));
pieChartEntries.add(new PieEntry(30.8f, "Blue"));
To add colors to the PieChart you use:
set.setColors(new int[]{Color.parseColor("#FF32DA64"),
Color.parseColor("#FF32DAD4"),
Color.parseColor("#FFB853F2"),
Color.parseColor("#FFF2ED53")});
or you can use one of the templates:
set.setColors(ColorTemplate.COLORFUL_COLORS);
EDIT:
I just saw you're using the colors from the resources, if you inspect the setColors
method you should see this:
If you are using colors from the resources,make sure that the colors are already prepared (by calling getResources().getColor(...))
So in your case you need to resolve the resources into Color
objects before adding them to the set
.
这篇关于MPAndroidChart - 饼图的所有部分都是相同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!