введите код здесьЯ хочу нарисовать диаграмму, но не могу выровнять метки оси Y по правому краю.
@Composable
fun Chart(
min: Double,
max: Double,
) {
val textMeasurer = rememberTextMeasurer()
val padding = 40f
val yAxisLabelMaxWidth = textMeasurer.measure(text = "${max.toInt()}").size.width.toFloat()
val yAxisLabelMinWidth = textMeasurer.measure(text = "${min.toInt()}").size.width.toFloat()
Canvas(
modifier = Modifier
) {
drawText(
textMeasurer = textMeasurer,
text = "${max.toInt()}",
topLeft = Offset(
x = 0f,
y = padding - textMeasurer.measure(text = "$max").size.height / 2
),
style = TextStyle(
fontSize = 10.sp, background = Color.Green
),
)
/*
2 more texts
*/
drawText(
textMeasurer = textMeasurer,
text = "${min.toInt()}",
topLeft = Offset(
x = yAxisLabelMaxWidth - yAxisLabelMinWidth,
y = size.height - padding - textMeasurer.measure(text = "$min").size.height / 2
),
style = TextStyle(
fontSize = 10.sp, background = Color.Green
),
)
}
}
[![Screenshot][1]][1]
Всегда происходит сдвиг, когда в min меньше цифр, чем в max. Однако yAxisLabelMaxWidth и yAxisLabelMinWidth имеют хорошее значение. Например, если max имеет 4 цифры, а min — только 1, ширина max равна 100 пикселям, а ширина min равна 25 пикселям.
Есть ли у вас решение?
[1]: https://i.sstatic.net/51L3cOYH.png
Подробнее здесь: https://stackoverflow.com/questions/798 ... ck-compose