1
0
mirror of https://github.com/dzeiocom/OpenHealth.git synced 2025-07-20 01:09:50 +00:00

feat: Add daily Steps goal

This commit is contained in:
2022-12-17 21:15:20 +01:00
parent 9d3585c42c
commit 1e4267731b
21 changed files with 336 additions and 64 deletions

View File

@ -4,6 +4,7 @@ package com.dzeio.charts
* A Base entry for any charts
*/
data class Entry(
val x: Double,
val y: Float
var x: Double,
var y: Float,
var color: Int? = null
)

View File

@ -14,12 +14,12 @@ class XAxis(
override var x: Double = 0.0
set(value) {
val max = getXMax() - increment * displayCount
if (value > max) {
val min = getXMin()
if (value > max && min <= max) {
field = max
return
}
val min = getXMin()
if (value < min) {
field = min
return
@ -103,4 +103,4 @@ class XAxis(
override fun refresh() {
// TODO("Not yet implemented")
}
}
}

View File

@ -6,6 +6,7 @@ import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import com.dzeio.charts.ChartViewInterface
import com.dzeio.charts.utils.drawDottedLine
class YAxis(
private val view: ChartViewInterface
@ -25,6 +26,12 @@ class YAxis(
color = Color.BLUE
}
override val goalLinePaint = Paint().apply {
isAntiAlias = true
color = Color.RED
strokeWidth = 4f
}
var onValueFormat: (value: Float) -> String = { it -> it.toString() }
override var labelCount = 5
@ -49,15 +56,19 @@ class YAxis(
return max!!
}
if (view.series.isEmpty()) {
return 100f
return (this.goalLine ?: 90f) + 10f
}
return view.series
val seriesMax = view.series
.maxOf { serie ->
if (serie.getDisplayedEntries().isEmpty()) {
return@maxOf 0f
}
return@maxOf serie.getDisplayedEntries().maxOf { entry -> entry.y }
}
if (this.goalLine != null) {
return if (seriesMax > this.goalLine!!) seriesMax else this.goalLine!! + 1000f
}
return seriesMax
}
override fun getYMin(): Float {
@ -65,7 +76,7 @@ class YAxis(
return min!!
}
if (view.series.isEmpty()) {
return 0f
return this.goalLine ?: 0f
}
return view.series
.minOf { serie ->
@ -80,6 +91,7 @@ class YAxis(
if (!enabled) {
return 0f
}
val min = getYMin()
val max = getYMax() - min
val top = space.top
@ -89,7 +101,7 @@ class YAxis(
val increment = (bottom - top) / labelCount
val valueIncrement = (max - min) / labelCount
for (index in 0 until labelCount) {
val text = onValueFormat((valueIncrement * (index + 1))).toString()
val text = onValueFormat((valueIncrement * (index + 1)))
textLabel.getTextBounds(text, 0, text.length, rect)
maxWidth = maxWidth.coerceAtLeast(rect.width().toFloat())
@ -105,10 +117,29 @@ class YAxis(
canvas.drawLine(space.left, posY, space.right - maxWidth - 32f, posY, linePaint)
}
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
)
}
return maxWidth + 32f
}
override fun refresh() {
// TODO("Not yet implemented")
}
private var goalLine: Float? = null
override fun setGoalLine(height: Float?) {
goalLine = height
}
}

View File

@ -28,6 +28,11 @@ sealed interface YAxisInterface {
*/
val linePaint: Paint
/**
* Goal line paint
*/
val goalLinePaint: Paint
/**
* run when manually refreshing the system
*
@ -72,4 +77,9 @@ sealed interface YAxisInterface {
* @return the width of the sidebar
*/
fun onDraw(canvas: Canvas, space: RectF): Float
}
/**
* Add a Goal line
*/
fun setGoalLine(height: Float?)
}

View File

@ -5,7 +5,6 @@ import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.Log
import com.dzeio.charts.ChartView
import com.dzeio.charts.utils.drawRoundRect
@ -47,18 +46,21 @@ class BarSerie(
for (entry in displayedEntries) {
// calculated height in percent from 0 to 100
val top = (1 - entry.y / max) * drawableSpace.height() + drawableSpace.top
var posX = drawableSpace.left + (view.xAxis.getPositionOnRect(entry, drawableSpace) - view.xAxis.getXOffset(drawableSpace)).toFloat()
var posX = drawableSpace.left + (view.xAxis.getPositionOnRect(
entry,
drawableSpace
) - view.xAxis.getXOffset(drawableSpace)).toFloat()
// Log.d(TAG, "gpor = ${view.xAxis.getPositionOnRect(entry, space)}, gxo = ${view.xAxis.getXOffset(space)}")
// Log.d(TAG, "max = $max, y = ${entry.y}, height = $height")
// Log.d(TAG, "posX: ${posX / 60 / 60 / 1000}, offsetX = ${view.xAxis.x / (1000 * 60 * 60)}, x = ${entry.x / (1000 * 60 * 60)}, pouet: ${(view.xAxis.x + view.xAxis.displayCount * view.xAxis.increment) / (1000 * 60 * 60)}")
Log.d(
TAG, """
${posX},
$top,
${(posX + barWidth)},
${drawableSpace.bottom}""".trimIndent()
)
// Log.d(
// TAG, """
// ${posX},
// $top,
// ${(posX + barWidth)},
// ${drawableSpace.bottom}""".trimIndent()
// )
val right = (posX + barWidth).coerceAtMost(drawableSpace.right)
@ -72,6 +74,13 @@ class BarSerie(
continue
}
// handle color recoloration
val paint = Paint(barPaint)
if (entry.color != null) {
paint.color = entry.color!!
}
canvas.drawRoundRect(
posX,
top,
@ -82,7 +91,7 @@ class BarSerie(
32f,
0f,
0f,
barPaint
paint
)
// handle text display

View File

@ -55,7 +55,17 @@ fun Canvas.drawDottedLine(
/**
* A more customizable drawRoundRect function
*/
fun Canvas.drawRoundRect(left: Float, top: Float, right: Float, bottom: Float, topLeft: Float, topRight: Float, bottomLeft: Float, bottomRight: Float, paint: Paint) {
fun Canvas.drawRoundRect(
left: Float,
top: Float,
right: Float,
bottom: Float,
topLeft: Float,
topRight: Float,
bottomLeft: Float,
bottomRight: Float,
paint: Paint
) {
val maxRound = arrayOf(topLeft, topRight, bottomLeft, bottomRight).maxOf { it }
val width = right - left
val height = bottom - top
@ -81,14 +91,30 @@ fun Canvas.drawRoundRect(left: Float, top: Float, right: Float, bottom: Float, t
if (bottomLeft == 0f) {
drawRect(left, bottom - height / 2, left + width / 2, bottom, paint)
} else {
drawRoundRect(left, bottom - height / 2, left + width / 2, bottom, bottomLeft, bottomLeft, paint)
drawRoundRect(
left,
bottom - height / 2,
left + width / 2,
bottom,
bottomLeft,
bottomLeft,
paint
)
}
// bottom right border
if (bottomRight == 0f) {
drawRect(right - width / 2, bottom - height / 2, right, bottom, paint)
} else {
drawRoundRect(right - width / 2, bottom - height / 2, right, bottom, bottomRight, bottomRight, paint)
drawRoundRect(
right - width / 2,
bottom - height / 2,
right,
bottom,
bottomRight,
bottomRight,
paint
)
}
}
@ -96,6 +122,23 @@ fun Canvas.drawRoundRect(left: Float, top: Float, right: Float, bottom: Float, t
/**
* A more customizable drawRoundRect function
*/
fun Canvas.drawRoundRect(rect: RectF, topLeft: Float, topRight: Float, bottomLeft: Float, bottomRight: Float, paint: Paint) {
drawRoundRect(rect.left, rect.top, rect.right, rect.bottom, topLeft, topRight, bottomLeft, bottomRight, paint)
}
fun Canvas.drawRoundRect(
rect: RectF,
topLeft: Float,
topRight: Float,
bottomLeft: Float,
bottomRight: Float,
paint: Paint
) {
drawRoundRect(
rect.left,
rect.top,
rect.right,
rect.bottom,
topLeft,
topRight,
bottomLeft,
bottomRight,
paint
)
}