Skip to content

Instantly share code, notes, and snippets.

@zurche
Last active September 11, 2023 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zurche/52cfccf54e340dddc6680714acd92d6e to your computer and use it in GitHub Desktop.
Save zurche/52cfccf54e340dddc6680714acd92d6e to your computer and use it in GitHub Desktop.
@Composable
fun PerformanceChart(modifier: Modifier = Modifier, list: List<Float> = listOf(10f, 20f, 3f, 1f)) {
val zipList: List<Pair<Float, Float>> = list.zipWithNext()
Row(modifier = modifier) {
val max = list.max()
val min = list.min()
val lineColor =
if (list.last() > list.first()) LightOlive else LightCarmin // <-- Line color is Green if its going up and Red otherwise
for (pair in zipList) {
val fromValuePercentage = getValuePercentageForRange(pair.first, max, min)
val toValuePercentage = getValuePercentageForRange(pair.second, max, min)
Canvas(
modifier = Modifier
.fillMaxHeight()
.weight(1f),
onDraw = {
val fromPoint = Offset(x = 0f, y = size.height.times(1 - fromValuePercentage)) // <-- Use times so it works for any available space
val toPoint =
Offset(x = size.width, y = size.height.times(1 - toValuePercentage)) // <-- Also here!
drawLine(
color = lineColor,
start = fromPoint,
end = toPoint,
strokeWidth = 3f
)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment