如何在MPAndroidChart的PieChart中设置颜色

时间:2018-07-23 12:00:21

标签: android colors pie-chart mpandroidchart

我尝试在MPAndroidChart的PieChart中设置颜色。但是它没有设置。我想在分区中添加自定义颜色。这是我的代码:-

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

}

1 个答案:

答案 0 :(得分:1)

代替这个

dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()

应该是

dataSet.setColors(Color.RED, Color.GREEN, Color.BLUE);  

因为第一个提供默认颜色,所以不能覆盖颜色。这是您的代码的结果:

enter image description here