scoop:scoop.ui.component:0.1.0
src/chart/sector.scoop
Selected production source · src/chart/sector.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*6
internal fun chartAngle(value: Double): Double {7
val angle = value % TAU8
return if (angle < 0.0)9
angle + TAU10
else11
angle12
}14
internal fun chartPolarPoint(center: Point, radius: Double, angle: Double): Point =15
Point(center.x + cos(angle) * radius, center.y + sin(angle) * radius)17
/** Drawing and picking share the complete annular sector, including its active outset. */18
internal class ChartSector(19
val center: Point,20
val inner: Double,21
val outer: Double,22
val start: Double,23
val sweep: Double,24
val color: Color,25
val outset: Double26
) {27
fun expanded(): ChartSector =28
ChartSector(center, inner, outer + outset, start, sweep, color, outset)29
fun contains(point: Point): Boolean {30
if (outer <= inner || sweep == 0.0)31
return false32
val dx = point.x - center.x;33
val dy = point.y - center.y34
val distance = dx * dx + dy * dy35
if (distance < inner * inner || distance > outer * outer)36
return false37
if (abs(sweep) >= TAU - 0.0000000001)38
return true39
val angle = atan2(dy, dx)40
val offset =41
if (sweep > 0.0)42
chartAngle(angle - start)43
else44
chartAngle(start - angle)45
return offset <= abs(sweep) + 0.000000000146
}47
fun outline(): Path {48
val arc = this49
return path {50
this.arcTo(51
Rect(52
arc.center.x - arc.outer,53
arc.center.y - arc.outer,54
arc.outer * 2.0,55
arc.outer * 2.056
),57
arc.start,58
arc.sweep,59
true60
)61
if (arc.inner > 0.0)62
this.arcTo(63
Rect(64
arc.center.x - arc.inner,65
arc.center.y - arc.inner,66
arc.inner * 2.0,67
arc.inner * 2.068
),69
arc.start + arc.sweep,70
-arc.sweep71
)72
else73
this.lineTo(arc.center)74
this.closeContour()75
}76
}77
}79
internal class ChartPolarLayout(plan: ChartPlan, size: Size, radar: Boolean = false) {80
private val a = plan.appearance81
private val line = a.measure("0").height82
private val labelWidth =83
if (radar)84
chartPolarLabelWidth(plan)85
else86
0.087
private val horizontal =88
if (radar)89
upperBound(labelWidth + 8.0 * a.density, size.width / 4.0)90
else91
12.0 * a.density92
private val vertical =93
if (radar)94
line + 10.0 * a.density95
else96
12.0 * a.density97
val center = Point(size.width / 2.0, size.height / 2.0)98
val radius =99
lowerBound(100
upperBound(size.width / 2.0 - horizontal, size.height / 2.0 - vertical) -101
4.0 *102
a.density,103
0.0104
)105
val plot = Rect(center.x - radius, center.y - radius, radius * 2.0, radius * 2.0)106
val hole = radius * plan.input.innerRadius107
private val side = lowerBound(sqrt(2.0) * hole - 8.0 * a.density, 0.0)108
val content = Rect(center.x - side / 2.0, center.y - side / 2.0, side, side)109
}111
private fun chartPolarLabelWidth(plan: ChartPlan): Double {112
var width = 0.0113
for (datum in plan.snapshot.data.datums)114
width = lowerBound(width, plan.appearance.measure(datum.label).width)115
return width116
}118
internal fun chartPolarSelection(119
plan: ChartPlan,120
series: ChartPreparedSeries,121
index: Long122
): ChartSelection =123
ChartSelection(124
plan.snapshot.data.series[series.source].key,125
plan.snapshot.data.datums[index].key126
)128
internal fun chartSectorColor(plan: ChartPlan, series: ChartPreparedSeries, index: Long): Color =129
plan.snapshot.data.datums[index].color ?: series.color