Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/decorations.scoop

Selected production source · src/chart/decorations.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.component.button as chartLegendButton
8import scoop.ui.component.row as chartDecorationRow
9import scoop.ui.component.column as chartDecorationColumn
11public class ChartTooltipEntry public constructor(
12 public val series: ChartSeries,
13 public val value: Double,
14 public val color: Color
17public class ChartTooltipPayload public constructor(
18 public val datum: ChartDatum,
19 public val entries: Array<ChartTooltipEntry>
22internal fun chartPayload(plan: ChartPlan, selection: ChartSelection?): ChartTooltipPayload? {
23 val key = selection ?: return None
24 val data = plan.snapshot.data
25 val at = data.indexOfDatum(key.datum) ?: return None
26 val selected = data.indexOfSeries(key.series) ?: return None
27 var visible = false
28 for (index in plan.snapshot.visible)
29 if (index == selected)
30 visible = true
31 if (!visible || data.series[selected].values[at] == None)
32 return None
33 val entries = ArrayList<ChartTooltipEntry>()
34 for (series in plan.prepared.series)
35 when (data.series[series.source].values[at]) {
36 case Some(val value) -> entries.add(
37 ChartTooltipEntry(
38 data.series[series.source],
39 value,
40 if (plan.input.kind == ChartKind.Pie || plan.input.kind == ChartKind.Radial)
41 data.datums[at].color ?: series.color
42 else
43 series.color
44 )
45 );
46 case None -> {}
47 }
48 return Some(ChartTooltipPayload(data.datums[at], entries.toArray()))
51internal fun chartViewText(
52 text: String,
53 appearance: ChartAppearance,
54 strong: Boolean = false
55): View =
56 component(
57 LabelProps(
58 LabelText.Plain(text, None),
59 Some(appearance.fonts),
60 Some(appearance.paragraph),
61 baseStyle =
62 Some(
63 if (strong)
64 appearance.foreground
65 else
66 appearance.text
67 )
68 ),
69 { props, scope -> Label(props, scope) }
70 )
72private class ChartSwatchProps(val color: Color, val size: Double, val kind: ChartIndicator)
74private class ChartSwatch(
75 private val props: Props<ChartSwatchProps>,
76 scope: ComponentScope
77): Element<Unit>(scope) {
78 public override fun update(): UiInvalidation = UiInvalidation.Layout
79 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
80 Unit
81 > =
82 LayoutResult(
83 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
84 Unit
85 )
86 public override fun scene(context: SceneContext, value: Unit) {
87 val input = props.value
88 val size = context.size
89 context.draw {
90 paint -> when (input.kind) {
91 case Dot -> paint.canvas.drawRoundRect(
92 Rect(0.0, 0.0, size.width, size.height),
93 2.0,
94 2.0,
95 Paint(input.color)
96 )
97 case Line -> paint.canvas.drawRect(
98 Rect(size.width * 0.3, 0.0, size.width * 0.4, size.height),
99 Paint(input.color)
100 )
101 case Dashed -> {
102 paint.canvas.drawRect(
103 Rect(0.0, size.height * 0.3, size.width * 0.4, size.height * 0.4),
104 Paint(input.color)
105 )
106 paint.canvas.drawRect(
107 Rect(
108 size.width * 0.6,
109 size.height * 0.3,
110 size.width * 0.4,
111 size.height * 0.4
112 ),
113 Paint(input.color)
114 )
115 }
116 }
117 }
118 }
121internal fun chartSwatch(
122 color: Color,
123 size: Double,
124 kind: ChartIndicator = ChartIndicator.Dot
125): View =
126 element(ChartSwatchProps(color, size, kind), { props, scope -> ChartSwatch(props, scope) })
128internal fun chartLegend(input: ChartProps, plan: ChartPlan): View {
129 val data = plan.snapshot.data
130 val a = plan.appearance
131 val d = a.density
132 val options = input.legendOptions
133 val model = input.model
134 return wrap {
135 this.spacing = 8.0 * d;
136 this.lineSpacing = 4.0 * d
137 this.justify = MainAlignment.Center;
138 this.alignment = CrossAlignment.Center
139 this.padding = Insets(4.0 * d, 8.0 * d, 4.0 * d, 4.0 * d)
140 for (index in 0L ..< data.series.size) {
141 val series = data.series[index]
142 val visible = model.isVisible(series.key)
143 val color = a.color(plan.snapshot, index)
144 val text =
145 when (options.formatter) {
146 case Some(val format) -> format(series);
147 case None -> series.label
148 }
149 val content =
150 chartDecorationRow {
151 this.spacing = 6.0 * d;
152 this.alignment = CrossAlignment.Center
153 if (!options.hideIcon)
154 this.add(
155 series.icon ?:
156 chartSwatch(
157 color.withAlpha(
158 if (visible)
159 color.alpha
160 else
161 color.alpha * 0.35
162 ),
163 8.0 * d
164 )
165 )
166 this.add(chartViewText(text, a, visible))
167 }
168 this.add(
169 chartLegendButton(
170 content,
171 { model.showSeries(series.key, !model.isVisible(series.key)) }
172 ) {
173 this.variant = ButtonVariant.Ghost;
174 this.size = ButtonSize.Sm;
175 this.enabled = input.enabled;
176 this.theme = input.theme
177 this.key = ViewKey.Text(series.key.value)
178 this.style {
179 this.surface {
180 this.padding = Some(Insets(4.0 * d, 2.0 * d, 4.0 * d, 2.0 * d))
181 }
182 }
183 }
184 )
185 }
186 }
189internal fun chartTooltip(
190 payload: ChartTooltipPayload,
191 options: ChartTooltipOptions,
192 a: ChartAppearance
193): View {
194 val d = a.density
195 val title =
196 when (options.labelFormatter) {
197 case Some(val format) -> format(payload.datum);
198 case None -> payload.datum.label
199 }
200 val values =
201 Array<String>(payload.entries.size) {
202 at ->
203 val entry = payload.entries[at];
204 when (options.formatter) {
205 case Some(val format) -> format(entry);
206 case None -> chartNumber(entry.value)
207 }
208 }
209 var width = 128.0 * d
210 if (!options.hideLabel)
211 width = lowerBound(width, a.measure(title).width + 20.0 * d)
212 for (at in 0L ..< payload.entries.size)
213 width =
214 lowerBound(
215 width,
216 a.measure(payload.entries[at].series.label).width +
217 a.measure(values[at]).width +
218 (
219 if (options.hideIndicator)
220 36.0
221 else
222 54.0
223 ) *
224 d
225 )
226 val content =
227 chartDecorationColumn {
228 this.spacing = 6.0 * d;
229 this.alignment = CrossAlignment.Stretch
230 if (!options.hideLabel)
231 this.add(chartViewText(title, a, true))
232 for (at in 0L ..< payload.entries.size) {
233 val entry = payload.entries[at]
234 this.add(
235 chartDecorationRow {
236 this.spacing = 8.0 * d;
237 this.alignment = CrossAlignment.Center
238 if (!options.hideIndicator)
239 this.item(
240 entry.series.icon ?:
241 chartSwatch(entry.color, 10.0 * d, options.indicator)
242 ) { this.shrink = 0.0 }
243 this.item(chartViewText(entry.series.label, a)) { this.grow = 1.0 }
244 this.item(chartViewText(values[at], a, true)) { this.shrink = 0.0 }
245 }
246 )
247 }
248 }
249 return panelFrame(
250 content,
251 Insets(10.0 * d, 6.0 * d, 10.0 * d, 6.0 * d),
252 styleOverride {
253 background(Some(Paint(a.theme.tokens.palette.background.background)))
254 border(Some(Border.all(d, a.theme.tokens.palette.border.withAlpha(0.5))))
255 corners(Corners.all(8.0 * d));
256 boxShadows(a.theme.tokens.effects.floating)
257 },
258 Some(upperBound(width, 320.0 * d))
259 )