manage text in some Situation PieChart of MPAndroidChart

眉间皱痕 提交于 2019-12-25 03:12:09

问题


I try to manage text in PieChart of MPAndroidChart. But when my text is difficult situation like True = 2, False = 3 , Not = 95, in this situation my pie chart layout is very bad. Here is my code :-

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.setColors(Color.GREEN, Color.RED, Color.GRAY);
    dataSet.setDrawValues(true)

    val pieData = PieData(dataSet)
    pieData.setValueFormatter(PercentFormatter())
    pieData.setValueTextSize(10f)
    pieData.setValueTextColor(Color.WHITE)

    mPie?.data = pieData
}

Result is like that :-

here in my Pie-Chart data is not set in good format . Help me for this


回答1:


You need this line, so your labels will be outside graph:

dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

And value will be outside . You can also control position, line length and coloring like this:

    pieData.setValueTextColor(Color.BLACK);
    dataSet.setValueLinePart1OffsetPercentage(10.f);
    dataSet.setValueLinePart1Length(0.43f);
    dataSet.setValueLinePart2Length(.1f);
    dataSet.setValueTextColor(Color.BLACK);
    dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
    mPie.setEntryLabelColor(Color.BLUE);

Here is result:



来源:https://stackoverflow.com/questions/51493521/manage-text-in-some-situation-piechart-of-mpandroidchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!