
commonMain.render.components.BarRenderer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chart-js Show documentation
Show all versions of chart-js Show documentation
A collection of drawing/charting utilities
package com.juul.krayon.chart.render.components
import com.juul.krayon.chart.data.ClusteredDataSet
import com.juul.krayon.chart.data.maxValue
import com.juul.krayon.chart.render.Orientation
import com.juul.krayon.chart.render.Renderer
import com.juul.krayon.color.Color
import com.juul.krayon.kanvas.Clip
import com.juul.krayon.kanvas.Kanvas
import com.juul.krayon.kanvas.Paint
import com.juul.krayon.kanvas.withClip
public class BarRenderer(
private val style: Style,
) : Renderer {
public class Style(
public val orientation: Orientation,
public val colors: Sequence,
public val clusterBehavior: ClusterBehavior,
)
/** How to represent multiple series within the same cluster. */
public enum class ClusterBehavior {
/** Each series should be its own bar, grouped next to each other. */
Grouped,
/** Each series should stacked to make a larger bar. */
Stacked
}
public data class Specification(
public val left: Float,
public val top: Float,
public val right: Float,
public val bottom: Float,
public val bars: ClusteredDataSet,
)
override fun render(data: Specification, canvas: Kanvas) {
val strokeWidth = 8f
val paints = style.colors.take(data.bars.seriesData.size)
.map { color -> canvas.buildPaint(Paint.Stroke(color, strokeWidth, Paint.Stroke.Cap.Round)) }
.toList()
canvas.withClip(Clip.Rect(data.left, data.top, data.right, data.bottom)) {
val scale = -1 * ((data.bottom - data.top - strokeWidth / 2) / data.bars.maxValue())
var offset = data.left + 16f
for (cluster in data.bars.clusterData) {
for ((index, value) in cluster.withIndex()) {
canvas.drawLine(offset, data.bottom, offset, data.bottom + value * scale, paints[index])
offset += 8f
}
offset += 16f
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy