feat: Add global line adding

This commit is contained in:
Florian Bouillon 2023-01-10 17:49:51 +01:00
parent 55948edc83
commit e3d82026db
3 changed files with 74 additions and 23 deletions

View File

@ -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
)

View File

@ -40,7 +40,12 @@ class YAxis(
private var min: Float? = 0f private var min: Float? = 0f
private var max: Float? = null 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 override var scrollEnabled: Boolean = false
@ -61,7 +66,7 @@ class YAxis(
return max!! return max!!
} }
if (view.series.isEmpty()) { if (view.series.isEmpty()) {
return (this.goalLine ?: 90f) + 10f return this.lines.keys.maxOrNull() ?: 0f
} }
val seriesMax = view.series val seriesMax = view.series
.maxOf { serie -> .maxOf { serie ->
@ -70,9 +75,6 @@ class YAxis(
} }
return@maxOf serie.getDisplayedEntries().maxOf { entry -> entry.y } return@maxOf serie.getDisplayedEntries().maxOf { entry -> entry.y }
} }
if (this.goalLine != null) {
return if (seriesMax > this.goalLine!!) seriesMax else this.goalLine!! + 1000f
}
return seriesMax return seriesMax
} }
@ -81,7 +83,7 @@ class YAxis(
return min!! return min!!
} }
if (view.series.isEmpty()) { if (view.series.isEmpty()) {
return this.goalLine ?: 0f return this.lines.keys.minOrNull() ?: 0f
} }
return view.series return view.series
.minOf { serie -> .minOf { serie ->
@ -122,21 +124,26 @@ class YAxis(
} }
if (this.goalLine != null) { for ((y, settings) in lines) {
val pos = (1 - this.goalLine!! / max) * space.height() + space.top val pos = ((1 - y - min / (getYMax() - min)) * space.height() + space.top)
canvas.drawDottedLine( if (settings.dotted) {
0f, canvas.drawDottedLine(
pos, 0f,
space.right - maxWidth - 32f, pos,
pos, space.right - maxWidth - 32f,
space.right / 20, pos,
goalLinePaint space.right / 20,
) settings.paint ?: linePaint
} )
} else {
if (this.drawZeroLine) { canvas.drawLine(
val pos = ((1 - -min / (getYMax() - min)) * space.height() + space.top) 0f,
canvas.drawLine(0f, pos, space.right - maxWidth - 32f, pos, linePaint) pos,
space.right - maxWidth - 32f,
pos,
settings.paint ?: linePaint
)
}
} }
return maxWidth + 32f return maxWidth + 32f
@ -146,9 +153,20 @@ class YAxis(
// TODO("Not yet implemented") // TODO("Not yet implemented")
} }
private var goalLine: Float? = null private val lines: HashMap<Float, Line> = 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?) { override fun setGoalLine(height: Float?) {
goalLine = height if (height != null) {
addLine(height, Line(true))
}
} }
} }

View File

@ -41,6 +41,7 @@ sealed interface YAxisInterface {
/** /**
* do the Zero line gets drawn? * do the Zero line gets drawn?
*/ */
@Deprecated("use the new global function", ReplaceWith("YAxisInterface.addLine"))
var drawZeroLine: Boolean var drawZeroLine: Boolean
/** /**
@ -90,6 +91,23 @@ sealed interface YAxisInterface {
/** /**
* Add a Goal line * Add a Goal line
*
*/ */
@Deprecated("use the new global function", ReplaceWith("YAxisInterface.addLine"))
fun setGoalLine(height: Float?) 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)
} }