Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/component.scoop

Selected production source · src/chart/component.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.ui.style.*
5import scoop.ui.component.column as chartColumn
7public class Chart public constructor(
8 private val props: Props<ChartProps>,
9 scope: ComponentScope
10): Component(scope) {
11 private val themes = ThemeAccess(scope)
12 private val localActive = state<ChartSelection?>(None)
13 private val active =
14 binding(
15 {
16 when (props.value.active) {
17 case Some(val value) -> value.value;
18 case None -> localActive.value
19 }
20 },
21 {
22 selection -> when (props.value.active) {
23 case Some(val value) -> value.value = selection;
24 case None -> localActive.value = selection
25 }
26 }
27 )
28 private var previous: ChartPlan? = None
29 public override fun onMount() {
30 themes.mount(props.value.theme)
31 }
32 public override fun render(): View {
33 val input = props.value
34 val theme = themes.read(input.theme)
35 val fonts = input.fonts ?: scope.defaultFonts
36 val snapshot = input.model.snapshot
37 val plan =
38 when (previous) {
39 case Some(val value) -> if (
40 chartPlanMatches(value, input, snapshot, theme, fonts, scope.layoutDirection)
41 )
42 value
43 else
44 ChartPlan(input, snapshot, ChartAppearance(theme, fonts, scope.layoutDirection))
45 case None -> ChartPlan(
46 input,
47 snapshot,
48 ChartAppearance(theme, fonts, scope.layoutDirection)
49 )
50 }
51 previous = Some(plan)
52 val payload =
53 if (input.tooltip && input.enabled)
54 chartPayload(plan, active.value)
55 else
56 None
57 val tooltip =
58 when (payload) {
59 case None -> empty()
60 case Some(val value) -> when (input.tooltipContent) {
61 case Some(val content) -> content(value);
62 case None -> chartTooltip(value, input.tooltipOptions, plan.appearance)
63 }
64 }
65 val plot =
66 element(
67 ChartInkProps(input, plan, active),
68 { props, scope -> ChartInk(props, scope) },
69 [tooltip, input.center ?: empty()],
70 ViewKey.Text("plot")
71 )
72 return chartColumn {
73 this.alignment = CrossAlignment.Stretch
74 this.add(plot)
75 if (input.legend)
76 this.add(chartLegend(input, plan))
77 }
78 }