Last active
September 11, 2023 20:00
-
-
Save zurche/52cfccf54e340dddc6680714acd92d6e to your computer and use it in GitHub Desktop.
This file contains 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 | |
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