2016-11-17 141 views

回答

2

您可以通过颜色设置自定义标签:

首先确保传说是启用。除非启用图例。

legend.setEnabled(true); 

随着com.github.PhilJay:MPAndroidChart:V3.0.0: -

legend .setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "aaaaa", "bbbbb", "ccccc"}); 

setCustom(INT []颜色,字符串[]标记):设置一个自定义的图例的标签和颜色阵列。颜色数量应与标签数量相匹配。每种颜色都用于在相同索引处绘制的表单。

+0

谢谢,但我没有找到'传说.setCustom()'方法 – Fomove

+0

的build.gradle:'编译“COM .github.PhilJay:MPAndroidChart:v2.1.0'' – Fomove

+0

检查此编译'com.github.PhilJay:MPAndroidChart:v3.0.0' –

4

我在v3.0.0中找不到方法setCustom(int [] color,String [] labels)。只有您必须传递LegendEntry对象的setCustom(LegendEntry [])。

List<LegendEntry> entries = new ArrayList<>(); 

for (int i = 0; i < titleList.size(); i++) { 
    LegendEntry entry = new LegendEntry(); 
    entry.formColor = colorList.get(i); 
    entry.label = titleList.get(i); 
    entries.add(entry); 
} 

legend.setCustom(entries); 
0

1)添加依赖于build.gradle应用水平 编译'com.github.PhilJay:MPAndroidChart:v2.1.0' 2)使功能chartData

private void chartData() { 

     ArrayList<Entry> entries = new ArrayList<>(); 
     entries.add(new Entry(50, 0)); 
     entries.add(new Entry(60, 1)); 


     final int[] piecolors = new int[]{ 
       Color.rgb(183, 28, 28), 
       Color.rgb(27, 94, 32)}; 

     PieDataSet dataset = new PieDataSet(entries, ""); 

     ArrayList<String> labels = new ArrayList<String>(); 
     labels.add("Borrowing"); 
     labels.add("Pending"); 


     PieData data = new PieData(labels, dataset); 
     dataset.setColors(ColorTemplate.createColors(piecolors)); // 
     data.setValueTextColor(Color.WHITE); 
     pieChart.setDescription("Description"); 
     pieChart.setData(data); 

    } 

3)onCreate()

0

呼叫chartData()传递标签名称作为第二个参数的构造函数PieEntry()。 (对于版本> 3.0.0)

例子:

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()); 
pieChart.setData(data); 
相关问题