fix: YAxis not putting value on first line (#19)

This commit is contained in:
Florian Bouillon 2023-01-11 23:20:23 +01:00 committed by GitHub
parent b1b209035c
commit 68c906e78e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View File

@ -104,14 +104,14 @@ class YAxis(
val bottom = space.bottom val bottom = space.bottom
var maxWidth = 0f var maxWidth = 0f
val increment = space.height() / labelCount val increment = space.height() / (labelCount - 1)
val valueIncrement = max / labelCount val valueIncrement = max / (labelCount - 1)
for (index in 0 until labelCount) { for (index in 0 until labelCount) {
val text = onValueFormat(min + (valueIncrement * (index + 1))) val text = onValueFormat(min + (valueIncrement * index))
textLabel.getTextBounds(text, 0, text.length, rect) textLabel.getTextBounds(text, 0, text.length, rect)
maxWidth = maxWidth.coerceAtLeast(rect.width().toFloat()) maxWidth = maxWidth.coerceAtLeast(rect.width().toFloat())
val posY = bottom - (index + 1) * increment val posY = bottom - index * increment
canvas.drawText( canvas.drawText(
text, text,
@ -121,7 +121,6 @@ class YAxis(
) )
// canvas.drawDottedLine(0f, posY, canvas.width.toFloat(), posY, 40f, linePaint) // canvas.drawDottedLine(0f, posY, canvas.width.toFloat(), posY, 40f, linePaint)
canvas.drawLine(space.left, posY, space.right - maxWidth - 32f, posY, linePaint) canvas.drawLine(space.left, posY, space.right - maxWidth - 32f, posY, linePaint)
} }
for ((y, settings) in lines) { for ((y, settings) in lines) {

View File

@ -114,7 +114,7 @@ class MainFragment : Fragment() {
scrollEnabled = true scrollEnabled = true
// change the number of labels // change the number of labels
labelCount = 10 labelCount = 11
// change how labels are displayed // change how labels are displayed
onValueFormat = { "${it.roundToInt()}g"} onValueFormat = { "${it.roundToInt()}g"}