Skip to content

Instantly share code, notes, and snippets.

@ngengesenior
Last active August 24, 2024 15:59
Show Gist options
  • Save ngengesenior/a8e731d34bc43d74e10e7e0331b7adc6 to your computer and use it in GitHub Desktop.
Save ngengesenior/a8e731d34bc43d74e10e7e0331b7adc6 to your computer and use it in GitHub Desktop.
@Composable
@Preview(showBackground = true, showSystemUi = true)
fun PieChart(
items: List<Item> = sampleItems
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
val total = items.sumOf { it.value.toDouble() }.toFloat()
val sweepAngles = remember {
items.map {
Animatable(0f)
}
}
LaunchedEffect(Unit) {
for (i in items.indices) {
sweepAngles[i].animateTo(
targetValue = ((items[i].value / total) * 360f),
animationSpec = tween(
easing = FastOutSlowInEasing
)
)
}
}
Canvas(
modifier = Modifier
.size(300.dp)
) {
var startAngle = -90f
for (i in items.indices) {
//val sweepAngle = ((items[currentItem].value / total) * 360f).toFloat()
val sweepAngle = sweepAngles[i].value
drawArc(
startAngle = startAngle,
sweepAngle = sweepAngle,
color = items[i].color,
useCenter = true
)
startAngle += sweepAngle
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment