kotlin 中的 MPAndroid 饼图

MPAndroid PieChart in kotlin

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.utils.ColorTemplate
import java.util.*


class PieChartActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.piechart)
        val pieChart = findViewById<PieChart>(R.id.piechart)

        val NoOfEmp= ArrayList<GraphModels>()

        NoOfEmp.add(GraphModels(945f, 0))
        NoOfEmp.add(GraphModels(1040f, 1))
        NoOfEmp.add(GraphModels(1133f, 2))
        NoOfEmp.add(GraphModels(1240f, 3))
        NoOfEmp.add(GraphModels(1369f, 4))
        NoOfEmp.add(GraphModels(1487f, 5))
        NoOfEmp.add(GraphModels(1501f, 6))
        NoOfEmp.add(GraphModels(1645f, 7))
        NoOfEmp.add(GraphModels(1578f, 8))
        NoOfEmp.add(GraphModels(1695f, 9))
        val dataSet = PieDataSet(NoOfEmp, "Number Of Employees")

        val year= ArrayList<GraphModel>()
        year.add(GraphModel("2008"))
        year.add(GraphModel("2009"))
        year.add(GraphModel("2010"))
        year.add(GraphModel("2011"))
        year.add(GraphModel("2012"))
        year.add(GraphModel("2013"))
        year.add(GraphModel("2014"))
        year.add(GraphModel("2015"))
        year.add(GraphModel("2016"))
        year.add(GraphModel("2017"))

        val data = PieData(year, dataSet)
        pieChart.data = data
        dataSet?.setColors(*ColorTemplate.COLORFUL_COLORS)
        pieChart.animateXY(5000, 5000)
    }


}

**我想写库 MPAndroid 图的 kotlin 代码 link 在这里 https://javapapers.com/android/android-chart-example-app-using-mpandroidchart/ 我想在我的项目中实现这个帮助我解决问题。 **

您可以使用以下方法制作 MPAndroidChart PieChart。

private fun setPieChartData(view: View) {
    val listPie = ArrayList<PieEntry>()
    val listColors = ArrayList<Int>()
    listPie.add(PieEntry(20F, "Pass"))
    listColors.add(resources.getColor(R.color.color_result_pass))
    listPie.add(PieEntry(50F, "Fail"))
    listColors.add(resources.getColor(R.color.color_result_fail))
    listPie.add(PieEntry(30F, "Unanswered"))
    listColors.add(resources.getColor(R.color.colorPrimary))

    val pieDataSet = PieDataSet(listPie, "")
    pieDataSet.colors = listColors

    val pieData = PieData(pieDataSet)
    pieData.setValueTextSize(CommonUtils.convertDpToSp(14))
    view.pieChart.data = pieData

    view.pieChart.setUsePercentValues(true)
    view.pieChart.isDrawHoleEnabled = false
    view.pieChart.description.isEnabled = false
    view.pieChart.setEntryLabelColor(R.color.color_black)
    view.pieChart.animateY(1400, Easing.EaseInOutQuad)
}

您可以根据您的要求进行定制。希望对你有帮助,谢谢。

您可以从 PieChart Example, and full doc about MPAndroidChart 了解更多信息。这是一个用于图形表示的漂亮库。

希望这个回答对您有所帮助。

val NoOfEmp = ArrayList<PieEntry>()

NoOfEmp.add(PieEntry(945f, "2008"))
NoOfEmp.add(PieEntry(1040f, "2009"))
NoOfEmp.add(PieEntry(1133f, "2010"))
NoOfEmp.add(PieEntry(1240f, "2011"))
NoOfEmp.add(PieEntry(1369f, "2012"))
NoOfEmp.add(PieEntry(1487f, "2013"))
NoOfEmp.add(PieEntry(1501f, "2014"))
NoOfEmp.add(PieEntry(1645f, "2015"))
NoOfEmp.add(PieEntry(1578f, "2016"))
NoOfEmp.add(PieEntry(1695f, "2017"))
val dataSet = PieDataSet(NoOfEmp, "Number Of Employees")

dataSet.setDrawIcons(false)
dataSet.sliceSpace = 3f
dataSet.iconsOffset = MPPointF(0F, 40F)
dataSet.selectionShift = 5f
dataSet.setColors(*ColorTemplate.COLORFUL_COLORS)

val data = PieData(dataSet)
data.setValueTextSize(11f)
data.setValueTextColor(Color.WHITE)
pieChart.data = data
pieChart.highlightValues(null)
pieChart.invalidate()
pieChart.animateXY(5000, 5000)