Reputation: 93
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)
}
}
**I want to write the kotlin code of library MPAndroid graphs link is here https://javapapers.com/android/android-chart-example-app-using-mpandroidchart/ i want to implement this in my project help me to resolve issues . **
Upvotes: 2
Views: 7098
Reputation: 575
You can learn more from PieChart Example, and full doc about MPAndroidChart. It's a beautiful lib for graph representation.
I hope this answer will help you.
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)
Upvotes: 3
Reputation: 376
You can use below method for 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)
}
You can customise as per your requirement. Hope this helps, thank you.
Upvotes: 0