Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/sector.scoop

Selected production source · src/chart/sector.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
6internal fun chartAngle(value: Double): Double {
7 val angle = value % TAU
8 return if (angle < 0.0)
9 angle + TAU
10 else
11 angle
14internal 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. */
18internal 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: Double
26) {
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 false
32 val dx = point.x - center.x;
33 val dy = point.y - center.y
34 val distance = dx * dx + dy * dy
35 if (distance < inner * inner || distance > outer * outer)
36 return false
37 if (abs(sweep) >= TAU - 0.0000000001)
38 return true
39 val angle = atan2(dy, dx)
40 val offset =
41 if (sweep > 0.0)
42 chartAngle(angle - start)
43 else
44 chartAngle(start - angle)
45 return offset <= abs(sweep) + 0.0000000001
46 }
47 fun outline(): Path {
48 val arc = this
49 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.0
56 ),
57 arc.start,
58 arc.sweep,
59 true
60 )
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.0
68 ),
69 arc.start + arc.sweep,
70 -arc.sweep
71 )
72 else
73 this.lineTo(arc.center)
74 this.closeContour()
75 }
76 }
79internal class ChartPolarLayout(plan: ChartPlan, size: Size, radar: Boolean = false) {
80 private val a = plan.appearance
81 private val line = a.measure("0").height
82 private val labelWidth =
83 if (radar)
84 chartPolarLabelWidth(plan)
85 else
86 0.0
87 private val horizontal =
88 if (radar)
89 upperBound(labelWidth + 8.0 * a.density, size.width / 4.0)
90 else
91 12.0 * a.density
92 private val vertical =
93 if (radar)
94 line + 10.0 * a.density
95 else
96 12.0 * a.density
97 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.0
104 )
105 val plot = Rect(center.x - radius, center.y - radius, radius * 2.0, radius * 2.0)
106 val hole = radius * plan.input.innerRadius
107 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)
111private fun chartPolarLabelWidth(plan: ChartPlan): Double {
112 var width = 0.0
113 for (datum in plan.snapshot.data.datums)
114 width = lowerBound(width, plan.appearance.measure(datum.label).width)
115 return width
118internal fun chartPolarSelection(
119 plan: ChartPlan,
120 series: ChartPreparedSeries,
121 index: Long
122): ChartSelection =
123 ChartSelection(
124 plan.snapshot.data.series[series.source].key,
125 plan.snapshot.data.datums[index].key
126 )
128internal fun chartSectorColor(plan: ChartPlan, series: ChartPreparedSeries, index: Long): Color =
129 plan.snapshot.data.datums[index].color ?: series.color