MPAndroidChart
MPAndroidChart copied to clipboard
I want a pie chart that has lines instead of having the labels inside of the slices
I made a pie chart using MP Chart. The values are inside of the pie pieces, but I want to have lines pointing to each piece of the pie indicating the value. Here is the code that I used to build my pie chart.
The issue I'm having is that there isn't a ton of stuff online about MP Chart and a lot of the stuff that is online is in Java. My code is in Kotlin. Trying to find something in Kotlin. Beginner to developing.
Image of my current Pie Chart What I want my pie chart to look like
//***Start Pie Chart Code***//
val pieChart = binding.pieChart
// on below line we are setting user percent value,
// setting description as enabled and offset for pie chart
pieChart.setUsePercentValues(false)
pieChart.getDescription().setEnabled(false)
pieChart.setExtraOffsets(5f, 10f, 5f, 5f)
// on below line we are setting drag for our pie chart
pieChart.setDragDecelerationFrictionCoef(0.95f)
// on below line we are setting hole
// and hole color for pie chart
pieChart.setDrawHoleEnabled(true)
pieChart.setHoleColor(Color.WHITE)
// on below line we are setting circle color and alpha
pieChart.setTransparentCircleColor(Color.WHITE)
pieChart.setTransparentCircleAlpha(110)
// on below line we are setting hole radius
pieChart.setHoleRadius(20f)
pieChart.setTransparentCircleRadius(5f)
// on below line we are setting center text
pieChart.setDrawCenterText(true)
// on below line we are setting
// rotation for our pie chart
pieChart.setRotationAngle(0f)
// enable rotation of the pieChart by touch
pieChart.setRotationEnabled(true)
pieChart.setHighlightPerTapEnabled(true)
// on below line we are setting animation for our pie chart
pieChart.animateY(1400, Easing.EaseInOutQuad)
// on below line we are disabling our legend for pie chart
pieChart.legend.isEnabled = false
pieChart.setEntryLabelColor(Color.WHITE)
pieChart.setEntryLabelTextSize(12f)
// on below line we are creating array list and
// adding data to it to display in pie chart
val entries: ArrayList<PieEntry> = ArrayList()
entries.add(PieEntry(proteinFloat,"g"))
entries.add(PieEntry(carbFloat,"g"))
entries.add(PieEntry(fatFloat,"g"))
// on below line we are setting pie data set
val dataSet = PieDataSet(entries, "Mobile OS")
// on below line we are setting icons.
dataSet.setDrawIcons(false)
// on below line we are setting slice for pie
dataSet.sliceSpace = 3f
dataSet.iconsOffset = MPPointF(0f, 40f)
dataSet.selectionShift = 5f
// add a lot of colors to list
val colors: ArrayList<Int> = ArrayList()
colors.add(resources.getColor(R.color.purple_200))
colors.add(resources.getColor(R.color.blue))
colors.add(resources.getColor(R.color.red))
// on below line we are setting colors.
dataSet.colors = colors
// on below line we are setting pie data set
val data = PieData(dataSet)
data.setValueFormatter(PercentFormatter())
data.setValueTextSize(15f)
data.setValueTypeface(Typeface.DEFAULT_BOLD)
data.setValueTextColor(Color.WHITE)
pieChart.setData(data)
// undo all highlights
pieChart.highlightValues(null)
// loading chart
pieChart.invalidate()
//***End Pie Chart Code***//