feat: Add Grouped Charts support (#17)

This commit is contained in:
2023-01-11 16:24:49 +01:00
committed by GitHub
parent e3d82026db
commit b1b209035c
7 changed files with 90 additions and 21 deletions

View File

@ -0,0 +1,21 @@
package com.dzeio.charts
enum class ChartType {
/**
* Basic Chart where items go over the other
*/
BASIC,
/**
* each series are next to each other nicely
*/
GROUPED,
/**
* WILL NOT DO ANYTHING CURRENTLY
*
* Each series are stacked over the other one
*/
STACKED
}

View File

@ -21,6 +21,8 @@ class ChartView @JvmOverloads constructor(context: Context?, attrs: AttributeSet
const val TAG = "Charts/ChartView"
}
override var type: ChartType = ChartType.BASIC
override var debug: Boolean = false
override val xAxis = XAxis(this)

View File

@ -6,6 +6,11 @@ import com.dzeio.charts.series.SerieInterface
interface ChartViewInterface {
/**
* Chart Type
*/
var type: ChartType
/**
* Make the whole view in debug mode
*

View File

@ -5,6 +5,7 @@ import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import com.dzeio.charts.ChartType
import com.dzeio.charts.ChartViewInterface
import com.dzeio.charts.Entry
import kotlin.math.roundToInt
@ -51,11 +52,13 @@ class XAxis(
}
override fun getPositionOnRect(entry: Entry, drawableSpace: RectF): Double {
return translatePositionToRect(entry.x, drawableSpace)
}
fun translatePositionToRect(value: Double, drawableSpace: RectF): Double {
return drawableSpace.width() * (value - x) / getDataWidth()
val result = drawableSpace.width() * (entry.x - x) / getDataWidth()
if (view.type == ChartType.GROUPED) {
val serie = view.series.find { it.entries.contains(entry) }
val index = view.series.indexOf(serie)
return result + getEntryWidth(drawableSpace) * index + spacing / 2 * index
}
return result
}
override fun getXMax(): Double {
@ -123,8 +126,15 @@ class XAxis(
}
}
return (drawableSpace.width() * smallest / getDataWidth() - spacing)
val result = (drawableSpace.width() * smallest / getDataWidth() - spacing)
.coerceIn(1.0, drawableSpace.width().toDouble())
// handle grouped series
if (view.type == ChartType.GROUPED) {
return result / view.series.size - spacing / 2 * (view.series.size - 1)
}
return result
}
override fun getDataWidth(): Double {