scoop:scoop.ui.component:0.1.0
src/chart/radar.scoop
Selected production source · src/chart/radar.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*6
internal fun chartRadar(plan: ChartPlan, size: Size): ChartGeometry {7
val layout = ChartPolarLayout(plan, size, true)8
val a = plan.appearance9
val input = plan.input10
val count = plan.snapshot.data.datums.size11
val grid = ArrayList<ChartGrid>()12
val labels = ArrayList<ChartLabel>()13
val series = ArrayList<ChartSeriesGeometry>()14
fun angle(index: Long): Double =15
-PI / 2.0 + TAU * index.toDouble() / lowerBound(count.toDouble(), 1.0)16
for (at in 0L ..< count) {17
val outer = chartPolarPoint(layout.center, layout.radius, angle(at))18
if (input.yAxis.grid)19
grid.add(ChartGrid(layout.center, outer))20
val label = plan.snapshot.data.datums[at].label21
val text = a.measure(label)22
val point = chartPolarPoint(layout.center, layout.radius + 8.0 * a.density, angle(at))23
val x =24
if (cos(angle(at)) > 0.1)25
point.x26
else27
if (cos(angle(at)) < -0.1)28
point.x - text.width29
else30
point.x - text.width / 2.031
labels.add(32
ChartLabel(33
label,34
Point(35
chartClamp(x, 0.0, lowerBound(size.width - text.width, 0.0)),36
chartClamp(37
point.y - text.height / 2.0,38
0.0,39
lowerBound(size.height - text.height, 0.0)40
)41
)42
)43
)44
}45
for (tick in chartTicks(plan.prepared.yRange, input.yAxis.tickCount)) {46
val unit = plan.prepared.yRange.unit(tick)47
if (unit >= 0.0 && unit <= 1.0) {48
val radius = unit * layout.radius49
if (input.yAxis.grid && count > 1L)50
for (at in 0L ..< count)51
grid.add(52
ChartGrid(53
chartPolarPoint(layout.center, radius, angle(at)),54
chartPolarPoint(layout.center, radius, angle((at + 1L) % count))55
)56
)57
if (input.yAxis.shown) {58
val text = chartFormat(input.yAxis, tick)59
val measured = a.measure(text)60
val halfStep =61
if (count > 2L)62
PI / count.toDouble()63
else64
0.065
val point =66
chartPolarPoint(layout.center, radius * cos(halfStep), -PI / 2.0 + halfStep)67
labels.add(68
ChartLabel(69
text,70
Point(point.x + 4.0 * a.density, point.y - measured.height / 2.0)71
)72
)73
}74
}75
}76
for (item in plan.prepared.series) {77
val samples =78
Array<ChartSample?>(count) {79
at -> when (item.samples[at]) {80
case None -> None81
case Some(val value) -> {82
val unit = plan.prepared.yRange.unit(value.value)83
Some(84
ChartSample(85
chartPolarSelection(plan, item, at),86
value.value,87
chartPolarPoint(layout.center, layout.radius * unit, angle(at)),88
layout.center,89
ChartShape.Point,90
unit >= 0.0 && unit <= 1.0 && layout.radius > 0.091
)92
)93
}94
}95
}96
series.add(ChartSeriesGeometry(item, samples))97
}98
return ChartGeometry(99
size,100
layout.plot,101
series.toArray(),102
labels.toArray(),103
grid.toArray(),104
ChartKind.Radar105
)106
}108
/** Rotate an incomplete polygon to its first gap, so the last/first edge remains an ordinary edge. */109
internal fun chartRadarRuns(samples: Array<ChartSample?>): Array<Array<ChartSample>> {110
var gap = -1L111
for (at in 0L ..< samples.size)112
when (samples[at]) {113
case None -> gap = at;114
case Some(_) -> {}115
}116
if (gap < 0L)117
return chartRuns(samples, false)118
val start = gap + 1L119
val rotated = Array<ChartSample?>(samples.size) { at -> samples[(start + at) % samples.size] }120
return chartRuns(rotated, false)121
}