From e3d82026db111cdba4d40c7c13de6461c046922a Mon Sep 17 00:00:00 2001 From: Avior Date: Tue, 10 Jan 2023 17:49:51 +0100 Subject: [PATCH] feat: Add global line adding --- .../main/java/com/dzeio/charts/axis/Line.kt | 15 +++++ .../main/java/com/dzeio/charts/axis/YAxis.kt | 64 ++++++++++++------- .../com/dzeio/charts/axis/YAxisInterface.kt | 18 ++++++ 3 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 library/src/main/java/com/dzeio/charts/axis/Line.kt diff --git a/library/src/main/java/com/dzeio/charts/axis/Line.kt b/library/src/main/java/com/dzeio/charts/axis/Line.kt new file mode 100644 index 0000000..f315dd9 --- /dev/null +++ b/library/src/main/java/com/dzeio/charts/axis/Line.kt @@ -0,0 +1,15 @@ +package com.dzeio.charts.axis + +import android.graphics.Paint + +data class Line ( + /** + * is the bar dotted + */ + var dotted: Boolean = false, + + /** + * Custom Paint + */ + var paint: Paint? = null +) \ No newline at end of file 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 bca6296..971ef00 100644 --- a/library/src/main/java/com/dzeio/charts/axis/YAxis.kt +++ b/library/src/main/java/com/dzeio/charts/axis/YAxis.kt @@ -40,7 +40,12 @@ class YAxis( private var min: Float? = 0f private var max: Float? = null - override var drawZeroLine: Boolean = true + @Deprecated("use the new global function", replaceWith = ReplaceWith("YAxisInterface.addLine")) + override var drawZeroLine: Boolean = false + set(value) { + addLine(0f, Line()) + field = value + } override var scrollEnabled: Boolean = false @@ -61,7 +66,7 @@ class YAxis( return max!! } if (view.series.isEmpty()) { - return (this.goalLine ?: 90f) + 10f + return this.lines.keys.maxOrNull() ?: 0f } val seriesMax = view.series .maxOf { serie -> @@ -70,9 +75,6 @@ class YAxis( } return@maxOf serie.getDisplayedEntries().maxOf { entry -> entry.y } } - if (this.goalLine != null) { - return if (seriesMax > this.goalLine!!) seriesMax else this.goalLine!! + 1000f - } return seriesMax } @@ -81,7 +83,7 @@ class YAxis( return min!! } if (view.series.isEmpty()) { - return this.goalLine ?: 0f + return this.lines.keys.minOrNull() ?: 0f } return view.series .minOf { serie -> @@ -122,21 +124,26 @@ class YAxis( } - if (this.goalLine != null) { - val pos = (1 - this.goalLine!! / max) * space.height() + space.top - canvas.drawDottedLine( - 0f, - pos, - space.right - maxWidth - 32f, - pos, - space.right / 20, - goalLinePaint - ) - } - - if (this.drawZeroLine) { - val pos = ((1 - -min / (getYMax() - min)) * space.height() + space.top) - canvas.drawLine(0f, pos, space.right - maxWidth - 32f, pos, linePaint) + for ((y, settings) in lines) { + val pos = ((1 - y - min / (getYMax() - min)) * space.height() + space.top) + if (settings.dotted) { + canvas.drawDottedLine( + 0f, + pos, + space.right - maxWidth - 32f, + pos, + space.right / 20, + settings.paint ?: linePaint + ) + } else { + canvas.drawLine( + 0f, + pos, + space.right - maxWidth - 32f, + pos, + settings.paint ?: linePaint + ) + } } return maxWidth + 32f @@ -146,9 +153,20 @@ class YAxis( // TODO("Not yet implemented") } - private var goalLine: Float? = null + private val lines: HashMap = hashMapOf() + override fun addLine(y: Float, line: Line) { + lines[y] = line + } + + override fun removeLine(y: Float) { + lines.remove(y) + } + + @Deprecated("use the new global function", ReplaceWith("YAxisInterface.addLine")) override fun setGoalLine(height: Float?) { - goalLine = height + if (height != null) { + addLine(height, Line(true)) + } } } diff --git a/library/src/main/java/com/dzeio/charts/axis/YAxisInterface.kt b/library/src/main/java/com/dzeio/charts/axis/YAxisInterface.kt index 8d1f6a0..9a7f469 100644 --- a/library/src/main/java/com/dzeio/charts/axis/YAxisInterface.kt +++ b/library/src/main/java/com/dzeio/charts/axis/YAxisInterface.kt @@ -41,6 +41,7 @@ sealed interface YAxisInterface { /** * do the Zero line gets drawn? */ + @Deprecated("use the new global function", ReplaceWith("YAxisInterface.addLine")) var drawZeroLine: Boolean /** @@ -90,6 +91,23 @@ sealed interface YAxisInterface { /** * Add a Goal line + * */ + @Deprecated("use the new global function", ReplaceWith("YAxisInterface.addLine")) fun setGoalLine(height: Float?) + + /** + * add a line on the Chart + * + * @param y the Y position of the line + * @param paint the Paint of the line if you want to have a custom one + */ + fun addLine(y: Float, line: Line) + + /** + * remove a line one the specified position + * + * @param y the Y position of the line + */ + fun removeLine(y: Float) }