scoop:scoop.ui.component:0.1.0
src/chart/component.scoop
Selected production source · src/chart/component.scoop
1
package scoop.ui.component3
import scoop.ui.*4
import scoop.ui.style.*5
import scoop.ui.component.column as chartColumn7
public class Chart public constructor(8
private val props: Props<ChartProps>,9
scope: ComponentScope10
): 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.value19
}20
},21
{22
selection -> when (props.value.active) {23
case Some(val value) -> value.value = selection;24
case None -> localActive.value = selection25
}26
}27
)28
private var previous: ChartPlan? = None29
public override fun onMount() {30
themes.mount(props.value.theme)31
}32
public override fun render(): View {33
val input = props.value34
val theme = themes.read(input.theme)35
val fonts = input.fonts ?: scope.defaultFonts36
val snapshot = input.model.snapshot37
val plan =38
when (previous) {39
case Some(val value) -> if (40
chartPlanMatches(value, input, snapshot, theme, fonts, scope.layoutDirection)41
)42
value43
else44
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
else56
None57
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.Stretch74
this.add(plot)75
if (input.legend)76
this.add(chartLegend(input, plan))77
}78
}79
}