得到以下代码:
Legend legend = mChart.getLegend();
legend.setLabels(new String[]{"aaaaa", "bbbbb", "ccccc"});
此设置无效?是否有另一种设置文本的方法?
答案 0 :(得分:9)
我在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);
答案 1 :(得分:5)
将标签名称作为第二个参数传递给构造函数PieEntry()
。
(对于版本&gt; 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);
答案 2 :(得分:4)
您可以使用颜色设置自定义标签:
首先确保Legend已启用。除非启用图例。
legend.setEnabled(true);
使用com.github.PhilJay:MPAndroidChart:v3.0.0: -
legend .setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "aaaaa", "bbbbb", "ccccc"});
setCustom(int [] colors,String [] labels):设置自定义图例的标签和颜色数组。颜色计数应与标签数量匹配。每种颜色都用于在相同索引处绘制的表单。
答案 3 :(得分:2)
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)在chartData()
onCreate()
答案 4 :(得分:1)
感谢vikas singh
和其他地方的其他提示,这里是我如何解决饼图和图例的着色问题,对于MPAndroidChart v3.0.3
:
private void visualizeAnalytics(ArrayList<Transaction> transactions) {
PieChart graph = rootView.findViewById(R.id.graph);
ArrayList<PieEntry> entries = new ArrayList<>();
HashMap<String, Integer> categoryFrequencyMap = new HashMap<>();
for(Transaction T: transactions) {
String category = T.getType().name();
if(categoryFrequencyMap.containsKey(category)){
categoryFrequencyMap.put(category, categoryFrequencyMap.get(category) + T.getAmount());
}else {
categoryFrequencyMap.put(category, T.getAmount());
}
}
ArrayList<String> categories = new ArrayList<>();
int e = 0;
for(String category: categoryFrequencyMap.keySet()){
categories.add(e, category);
entries.add(new PieEntry(categoryFrequencyMap.get(category), category));
e++;
}
PieDataSet categories_dataSet = new PieDataSet(entries, "Categories");
categories_dataSet.setSliceSpace(2f);
categories_dataSet.setValueTextSize(15f);
//categories_dataSet.setValueTextColor(Color.RED);/* this line not working */
graph.setEntryLabelColor(Color.RED);
categories_dataSet.setSelectionShift(10f);
categories_dataSet.setValueLinePart1OffsetPercentage(80.f);
categories_dataSet.setValueLinePart1Length(1f);
categories_dataSet.setValueLinePart2Length(0.9f);
categories_dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
PieData pieData = new PieData(categories_dataSet);
graph.setData(pieData);
Legend legend = graph.getLegend();
List<LegendEntry> legendEntries = new ArrayList<>();
RandomColor randomColor = new RandomColor();
int[] colors = randomColor.randomColor(categories.size());
for (int i = 0; i < categories.size(); i++) {
LegendEntry legendEntry = new LegendEntry();
legendEntry.formColor = colors[i];
legendEntry.label = categories.get(i);
legendEntries.add(legendEntry);
}
categories_dataSet.setColors(colors);
legend.setEnabled(false);
legend.setCustom(legendEntries);
Description description = new Description();
description.setText("Analyzing Transaction Vol by Category");
graph.setDescription(description);
graph.animateY(5000);
graph.invalidate(); // refresh
}
对于漂亮的随机颜色,我从这里得到:https://github.com/lzyzsd/AndroidRandomColor