sudeshim3
9/29/2018 - 2:22 PM

Pie chart with minimal input required to create a poc.

Pie chart with minimal input required to create a poc.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val values = ArrayList<PieEntry>()
        var colorList = listOf(Color.RED, Color.GREEN, Color.YELLOW);

//    1. Create an array list of pie chart values.
        values.add(PieEntry(10f, 1))
        values.add(PieEntry(10f, 2))
        values.add(PieEntry(10f, 3))

//    2. pass values to PieDataset as parameter along with chart name.
        val dataset = PieDataSet(values, "Pie Chart Demo")

//        if color is not provided the default color is set to white
        dataset.setColors(colorList)

//    3. pass your dataset to piechart.
        pie_chart.data = PieData(dataset)
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#9f9f9f"
    android:padding="10dp"
    tools:context=".MainActivity">

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/pie_chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>