scoop:scoop.ui.component:0.1.0
src/chart/polar_draw.scoop
Selected production source · src/chart/polar_draw.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*6
internal fun chartDrawPolar(canvas: Canvas, plan: ChartPlan, geometry: ChartGeometry) {7
val a = plan.appearance8
val d = a.density9
for (line in geometry.grid)10
canvas.drawLine(11
line.start,12
line.end,13
Paint(a.theme.tokens.palette.border.withAlpha(0.5), strokeWidth = d)14
)15
for (track in geometry.tracks)16
if (track.outer > track.inner)17
track.outline().use { outline -> canvas.drawPath(outline, Paint(track.color)) }18
if (geometry.kind == ChartKind.Radar)19
chartDrawRadar(canvas, plan, geometry)20
else {21
for (series in geometry.series)22
for (sample in series.samples)23
when (sample) {24
case Some(val value) -> when (value.shape) {25
case Sector(val arc) -> {26
if (value.visible) {27
arc.outline().use {28
outline ->29
canvas.drawPath(outline, Paint(arc.color))30
if (geometry.kind == ChartKind.Pie)31
canvas.drawPath(32
outline,33
Paint(34
a.theme.tokens.palette.background.background,35
PaintingStyle.Stroke,36
d37
)38
)39
return@use Unit40
}41
}42
}43
else -> {}44
}45
case None -> {}46
}47
}48
for (label in geometry.labels)49
a.draw(canvas, label.text, label.origin)50
if (plan.input.labels)51
for (series in geometry.series)52
for (sample in series.samples)53
when (sample) {54
case Some(val value) -> if (value.visible) {55
val text = chartNumber(value.value)56
val measured = a.measure(text)57
a.draw(58
canvas,59
text,60
Point(61
chartClamp(62
value.point.x - measured.width / 2.0,63
0.0,64
lowerBound(geometry.size.width - measured.width, 0.0)65
),66
chartClamp(67
value.point.y - measured.height / 2.0,68
0.0,69
lowerBound(geometry.size.height - measured.height, 0.0)70
)71
),72
true73
)74
}75
case None -> {}76
}77
}79
private fun chartDrawRadar(canvas: Canvas, plan: ChartPlan, geometry: ChartGeometry) {80
path { this.addOval(geometry.plot) }.use {81
clip -> canvas.withState {82
canvas ->83
canvas.clipPath(clip)84
for (series in geometry.series) {85
val runs = chartRadarRuns(series.samples)86
val complete = runs.size == 1L && runs[0L].size == series.samples.size87
for (run in runs) {88
path {89
chartTrace(90
this,91
Array<Point>(run.size) { at -> run[at].point },92
ChartCurve.Linear,93
false94
)95
if (complete && run.size > 2L)96
this.closeContour()97
}.use {98
polygon ->99
if (complete && run.size > 2L)100
canvas.drawPath(polygon, Paint(series.series.color.withAlpha(0.2)))101
canvas.drawPath(102
polygon,103
Paint(104
series.series.color,105
PaintingStyle.Stroke,106
2.0 * plan.appearance.density107
)108
)109
}110
if (plan.input.pointRadius > 0.0)111
for (value in run)112
canvas.drawCircle(113
value.point,114
plan.input.pointRadius * plan.appearance.density,115
Paint(series.series.color)116
)117
}118
}119
}120
}121
}123
internal fun chartDrawPolarActive(124
canvas: Canvas,125
plan: ChartPlan,126
geometry: ChartGeometry,127
selection: ChartSelection?128
) {129
val key = selection ?: return130
val sample = geometry.find(key) ?: return131
val a = plan.appearance132
when (sample.shape) {133
case Sector(val arc) -> {134
val active =135
if (geometry.kind == ChartKind.Pie)136
arc.expanded()137
else138
arc139
active.outline().use {140
outline ->141
if (geometry.kind == ChartKind.Pie)142
canvas.drawPath(outline, Paint(active.color))143
canvas.drawPath(144
outline,145
Paint(a.theme.tokens.palette.ring, PaintingStyle.Stroke, 2.0 * a.density)146
)147
}148
}149
else -> {150
canvas.drawCircle(151
sample.point,152
lowerBound(plan.input.pointRadius, 4.0) * a.density,153
Paint(a.theme.tokens.palette.background.background)154
)155
canvas.drawCircle(156
sample.point,157
lowerBound(plan.input.pointRadius, 4.0) * a.density,158
Paint(a.theme.tokens.palette.ring, PaintingStyle.Stroke, 2.0 * a.density)159
)160
}161
}162
}