Last active
August 24, 2024 15:59
-
-
Save ngengesenior/a8e731d34bc43d74e10e7e0331b7adc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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