Skip to content

mahozad/android-pie-chart

Repository files navigation

Codecov Latest Maven Central release Kotlin

A Pie/Donut*/Ring chart for Android, customizable to the most extent possible.
For tutorial and examples refer to the website.

Screenshot 1 Screenshot 2 Screenshot 3 Screenshot 4 Screenshot 5
Screenshot 2 Screenshot 1 Screenshot 3 Screenshot 4 Screenshot 5

build.gradle[.kts]

implementation("ir.mahozad.android:pie-chart:0.7.0")

XML layout

<ir.mahozad.android.PieChart
    android:id="@+id/pieChart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Kotlin Activity

val pieChart = findViewById<PieChart>(R.id.pieChart)
pieChart.slices = listOf(
    PieChart.Slice(0.2f, Color.BLUE),
    PieChart.Slice(0.4f, Color.MAGENTA),
    PieChart.Slice(0.3f, Color.YELLOW),
    PieChart.Slice(0.1f, Color.CYAN)
)

Jetpack Compose

@Composable
fun PieChartView() {
    AndroidView(
        modifier = Modifier.fillMaxSize(),
        factory = { context ->
            PieChart(context).apply {
                slices = listOf(
                    PieChart.Slice(0.2f, Color.BLUE),
                    PieChart.Slice(0.4f, Color.MAGENTA),
                    PieChart.Slice(0.3f, Color.YELLOW),
                    PieChart.Slice(0.1f, Color.CYAN)
                )
            }
        },
        update = { view ->
            // View's been inflated or state read in this block has been updated
            // Add logic here if necessary
        }
    )
}

Contributing

Please help improve the library by fixing the issues that I couldn't tackle myself.
Any other contributions are also welcome.


* Or Doughnut