Skip to content

Instantly share code, notes, and snippets.

@sudeshim3
Last active September 29, 2018 14:22
Show Gist options
  • Save sudeshim3/744ddb2a3502ae2effaf478153ecb90b to your computer and use it in GitHub Desktop.
Save sudeshim3/744ddb2a3502ae2effaf478153ecb90b to your computer and use it in GitHub Desktop.
Pie chart with minimal input required to create a poc.
<?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>
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment