如何在 MPAndroidChart 的 PieChart 中设置颜色
How can i set color in PieChart of MPAndroidChart
我尝试在饼图中设置颜色 MPAndroidChart.But 未设置 it.I 想在我的分区中添加自定义颜色。这是我的代码:-
fun setupPieChartView() {
mPie = findViewById(R.id.piechart)
mPie?.setUsePercentValues(true)
val desc: Description = Description()
desc.text = "PieChart"
mPie?.description = desc
val legend: Legend? = mPie?.legend
legend?.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
val value = Arrays.asList(trueAns.toFloat(), wrongAns.toFloat(), noAns.toFloat())
val label = Arrays.asList("True", "false", "Not")
val entry = ArrayList<PieEntry>()
for (i in value.indices) {
entry.add(PieEntry(value.get(i), label.get(i)))
}
val dataSet = PieDataSet(entry, "Result")
dataSet.setDrawValues(true)
val pieData = PieData(dataSet)
pieData.setValueFormatter(PercentFormatter())
pieData.setValueTextSize(10f)
pieData.setValueTextColor(Color.WHITE)
mPie?.data = pieData
}
而不是这个
dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()
应该是
dataSet.setColors(Color.RED, Color.GREEN, Color.BLUE);
因为第一个给出了默认颜色,所以你不能覆盖颜色。这是您的代码的结果:
我尝试在饼图中设置颜色 MPAndroidChart.But 未设置 it.I 想在我的分区中添加自定义颜色。这是我的代码:-
fun setupPieChartView() {
mPie = findViewById(R.id.piechart)
mPie?.setUsePercentValues(true)
val desc: Description = Description()
desc.text = "PieChart"
mPie?.description = desc
val legend: Legend? = mPie?.legend
legend?.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
val value = Arrays.asList(trueAns.toFloat(), wrongAns.toFloat(), noAns.toFloat())
val label = Arrays.asList("True", "false", "Not")
val entry = ArrayList<PieEntry>()
for (i in value.indices) {
entry.add(PieEntry(value.get(i), label.get(i)))
}
val dataSet = PieDataSet(entry, "Result")
dataSet.setDrawValues(true)
val pieData = PieData(dataSet)
pieData.setValueFormatter(PercentFormatter())
pieData.setValueTextSize(10f)
pieData.setValueTextColor(Color.WHITE)
mPie?.data = pieData
}
而不是这个
dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()
应该是
dataSet.setColors(Color.RED, Color.GREEN, Color.BLUE);
因为第一个给出了默认颜色,所以你不能覆盖颜色。这是您的代码的结果: