Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/geometry.scoop

Selected production source · src/chart/geometry.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.ui.*
6import scoop.ui.style.*
8internal enum ChartShape {
9 Point, Rectangle(val bounds: Rect), Sector(val arc: ChartSector)
12internal 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: Boolean
21internal class ChartSeriesGeometry(
22 val series: ChartPreparedSeries,
23 val samples: Array<ChartSample?>
26internal class ChartLabel(val text: String, val origin: Point)
28internal struct ChartGrid(val start: Point, val end: Point)
30internal 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 None
50 }
53private 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 false
64internal class ChartGeometryCache(private val plan: ChartPlan) {
65 private var previous: ChartGeometry? = None
66 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 next
75 }
78internal 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.height
90internal 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.y
93internal 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 }