From 4612fd71896e26d2692b8329970c947413048574 Mon Sep 17 00:00:00 2001 From: Avior Date: Thu, 12 Jan 2023 23:12:28 +0100 Subject: [PATCH] fix: Negative numbers not being displayed correctly (#26) --- .../main/java/com/dzeio/charts/axis/YAxis.kt | 12 ++++++++++ .../java/com/dzeio/charts/series/BarSerie.kt | 23 ++++++++----------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/com/dzeio/charts/axis/YAxis.kt b/library/src/main/java/com/dzeio/charts/axis/YAxis.kt index 97441ac..d60fef1 100644 --- a/library/src/main/java/com/dzeio/charts/axis/YAxis.kt +++ b/library/src/main/java/com/dzeio/charts/axis/YAxis.kt @@ -81,6 +81,12 @@ class YAxis( } for (index in 0 until serie.entries.size) { 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 } } @@ -114,6 +120,12 @@ class YAxis( } for (index in 0 until serie.entries.size) { 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 } } diff --git a/library/src/main/java/com/dzeio/charts/series/BarSerie.kt b/library/src/main/java/com/dzeio/charts/series/BarSerie.kt index 2700d86..209b92a 100644 --- a/library/src/main/java/com/dzeio/charts/series/BarSerie.kt +++ b/library/src/main/java/com/dzeio/charts/series/BarSerie.kt @@ -64,6 +64,7 @@ class BarSerie( // calculated height in percent from 0 to 100 var top = view.yAxis.getPositionOnRect(entry, drawableSpace) + .coerceIn(drawableSpace.top, drawableSpace.bottom) var posX = drawableSpace.left + view.xAxis.getPositionOnRect( entry, drawableSpace @@ -99,7 +100,9 @@ class BarSerie( paint.color = entry.color!! } + var height: Float if (entry.y < 0) { + height = top - zero canvas.drawRoundRect( posX, zero, @@ -112,6 +115,7 @@ class BarSerie( paint ) } else { + height = zero - top canvas.drawRoundRect( posX, top, @@ -130,31 +134,22 @@ class BarSerie( textPaint.getTextBounds(text, 0, text.length, rect) - val textLeft = (posX + barWidth / 2) - - if ( - // handle right side - textLeft + rect.width() / 2 > right || - // handle left sie - textLeft - rect.width() / 2 < drawableSpace.left - ) { - continue - } + // text center X + val textX = (posX + barWidth / 2) val doDisplayIn = - rect.height() < drawableSpace.bottom - top && - rect.width() < barWidth + rect.height() + 32f < height 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()) { textY = drawableSpace.top + rect.height() } - canvas.drawText( text, - textLeft, + textX, textY, if (doDisplayIn) textPaint else textExternalPaint )