scoop:scoop.ui.component:0.1.0
src/chart/geometry.scoop
Selected production source · src/chart/geometry.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal enum ChartShape {9
Point, Rectangle(val bounds: Rect), Sector(val arc: ChartSector)10
}12
internal class ChartSample(13
val selection: ChartSelection,14
val value: Double,15
val point: Point,16
val base: Point,17
val shape: ChartShape,18
val visible: Boolean19
)21
internal class ChartSeriesGeometry(22
val series: ChartPreparedSeries,23
val samples: Array<ChartSample?>24
)26
internal class ChartLabel(val text: String, val origin: Point)28
internal struct ChartGrid(val start: Point, val end: Point)30
internal class ChartGeometry(31
val size: Size,32
val plot: Rect,33
val series: Array<ChartSeriesGeometry>,34
val labels: Array<ChartLabel>,35
val grid: Array<ChartGrid>,36
val kind: ChartKind = ChartKind.Line,37
val tracks: Array<ChartSector> = [],38
val contentBounds: Rect = Rect(0.0, 0.0, 0.0, 0.0)39
) {40
val interactive: Boolean = chartHasSamples(series)41
fun find(selection: ChartSelection): ChartSample? {42
for (series in series)43
for (value in series.samples)44
when (value) {45
case Some(val sample) -> if (sample.selection == selection && sample.visible)46
return Some(sample);47
case None -> {}48
}49
return None50
}51
}53
private fun chartHasSamples(series: Array<ChartSeriesGeometry>): Boolean {54
for (line in series)55
for (value in line.samples)56
when (value) {57
case Some(val sample) -> if (sample.visible)58
return true;59
case None -> {}60
}61
return false62
}64
internal class ChartGeometryCache(private val plan: ChartPlan) {65
private var previous: ChartGeometry? = None66
fun resolve(size: Size): ChartGeometry {67
when (previous) {68
case Some(val value) -> if (value.size == size)69
return value;70
case None -> {}71
}72
val next = chartGeometry(plan, size);73
previous = Some(next);74
return next75
}76
}78
internal fun chartInside(bounds: Rect, point: Point): Boolean =79
point.x >=80
bounds.x &&81
point.x <=82
bounds.x +83
bounds.width &&84
point.y >=85
bounds.y &&86
point.y <=87
bounds.y +88
bounds.height90
internal fun chartOverlap(a: Rect, b: Rect): Boolean =91
a.x + a.width >= b.x && b.x + b.width >= a.x && a.y + a.height >= b.y && b.y + b.height >= a.y93
internal fun chartGeometry(plan: ChartPlan, size: Size): ChartGeometry =94
when (plan.input.kind) {95
case Pie -> chartPie(plan, size);96
case Radar -> chartRadar(plan, size);97
case Radial -> chartRadial(plan, size);98
else -> chartCartesian(plan, size)99
}