fix: Negative numbers not being displayed correctly (#26)

This commit is contained in:
Florian Bouillon 2023-01-12 23:12:28 +01:00 committed by GitHub
parent c9fee7ca24
commit 4612fd7189
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 14 deletions

View File

@ -81,6 +81,12 @@ class YAxis(
} }
for (index in 0 until serie.entries.size) { for (index in 0 until serie.entries.size) {
val entry = serie.entries[index] val entry = serie.entries[index]
if (sign(entry.y) <= 0f && nList[index] > 0f) {
continue
} else if (nList[index] < 0f && entry.y > 0f) {
nList[index] = entry.y
continue
}
nList[index] += entry.y nList[index] += entry.y
} }
} }
@ -114,6 +120,12 @@ class YAxis(
} }
for (index in 0 until serie.entries.size) { for (index in 0 until serie.entries.size) {
val entry = serie.entries[index] val entry = serie.entries[index]
if (sign(entry.y) >= 0f && nList[index] < 0f) {
continue
} else if (nList[index] > 0f && entry.y < 0f) {
nList[index] = entry.y
continue
}
nList[index] += entry.y nList[index] += entry.y
} }
} }

View File

@ -64,6 +64,7 @@ class BarSerie(
// calculated height in percent from 0 to 100 // calculated height in percent from 0 to 100
var top = view.yAxis.getPositionOnRect(entry, drawableSpace) var top = view.yAxis.getPositionOnRect(entry, drawableSpace)
.coerceIn(drawableSpace.top, drawableSpace.bottom)
var posX = drawableSpace.left + view.xAxis.getPositionOnRect( var posX = drawableSpace.left + view.xAxis.getPositionOnRect(
entry, entry,
drawableSpace drawableSpace
@ -99,7 +100,9 @@ class BarSerie(
paint.color = entry.color!! paint.color = entry.color!!
} }
var height: Float
if (entry.y < 0) { if (entry.y < 0) {
height = top - zero
canvas.drawRoundRect( canvas.drawRoundRect(
posX, posX,
zero, zero,
@ -112,6 +115,7 @@ class BarSerie(
paint paint
) )
} else { } else {
height = zero - top
canvas.drawRoundRect( canvas.drawRoundRect(
posX, posX,
top, top,
@ -130,31 +134,22 @@ class BarSerie(
textPaint.getTextBounds(text, 0, text.length, rect) textPaint.getTextBounds(text, 0, text.length, rect)
val textLeft = (posX + barWidth / 2) // text center X
val textX = (posX + barWidth / 2)
if (
// handle right side
textLeft + rect.width() / 2 > right ||
// handle left sie
textLeft - rect.width() / 2 < drawableSpace.left
) {
continue
}
val doDisplayIn = val doDisplayIn =
rect.height() < drawableSpace.bottom - top && rect.height() + 32f < height
rect.width() < barWidth
var textY = if (doDisplayIn) top + rect.height() + 16f else top - 16f var textY = if (doDisplayIn) top + rect.height() + 16f else top - 16f
if (entry.y < 0f) textY = if (doDisplayIn) top - 16f else top + rect.height() + 16f
if (textY < drawableSpace.top + rect.height()) { if (textY < drawableSpace.top + rect.height()) {
textY = drawableSpace.top + rect.height() textY = drawableSpace.top + rect.height()
} }
canvas.drawText( canvas.drawText(
text, text,
textLeft, textX,
textY, textY,
if (doDisplayIn) textPaint else textExternalPaint if (doDisplayIn) textPaint else textExternalPaint
) )