首页 > 解决方案 > 如何在 MPAndroidChart 的 PieChart 中设置颜色

问题描述

我尝试在 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

}

标签: androidcolorspie-chartmpandroidchart

解决方案


而不是这个

dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()

它应该是

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

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

在此处输入图像描述


推荐阅读