android - MPAndroidChart 饼图:未显示所有标签
问题描述
我正在使用 compile 'com.github.PhilJay:MPAndroidChart:v3.0.0' 作为 android 中的饼图,但无法查看所有标签
还会显示一个标签,但颜色不匹配。
private void drawMap()
{
ArrayList<PieEntry> yvalues = new ArrayList<PieEntry>();
yvalues.add(new PieEntry(8f, "JAN"));
yvalues.add(new PieEntry(15f, "FEB"));
yvalues.add(new PieEntry(12f, "MAR"));
yvalues.add(new PieEntry(25f, "APR"));
yvalues.add(new PieEntry(23f, "MAY"));
yvalues.add(new PieEntry(17f, "JUNE"));
PieDataSet dataSet = new PieDataSet(yvalues, "Election Results");
PieData data = new PieData();
data.addDataSet(dataSet);
data.setValueFormatter(new PercentFormatter());
pcVehicle.setData(data);
dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
}<com.github.mikephil.charting.charts.PieChart
android:padding="@dimen/padding_12"
android:layout_margin="@dimen/margin_08"
android:id="@+id/pcVehicle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
解决方案
您使用的颜色模板只有五种颜色。如果要绘制多于五个的数据值,则需要提供更多的颜色,否则它将使用一些相同的颜色来绘制超过五个饼图。
解决这个问题的一种方法是像这样组合来自两个不同模板的颜色。
int[] colors = new int[10];
int counter = 0;
for (int color : ColorTemplate.JOYFUL_COLORS
) {
colors[counter] = color;
counter++;
}
for (int color : ColorTemplate.MATERIAL_COLORS
) {
colors[counter] = color;
counter++;
}
dataSet.setColors(colors);
推荐阅读
- java - 实例化对象抛出空指针异常
- regex - 自动化无聊的东西:电话和电子邮件地址提取器(错误,从示例中复制)
- c++ - (c++) GetWindowText 为除控制台本身之外的所有内容返回空值
- azure - Azure Api Manager 将多个服务结构终结点配置到后端
- hadoop - 无法在谷歌云的端口 22 上连接虚拟机
- java - 自定义注解 - Spring Boot
- python - Python:使用模块或正则表达式从列表中提取列表
- javascript - Javascript:仍然显示以前的结果
- javascript - 如何使用 JavaScript 获取元素的网格坐标?
- sql - 有没有办法将相同的数据插入多列是SQL?