Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/options.scoop

Selected production source · src/chart/options.scoop

1package scoop.ui.component
3import scoop.graphics.text.FontCollection
4import scoop.math.*
5import scoop.ui.*
6import scoop.ui.style.*
8public enum ChartAxisKind {
9 Category, Linear
12public enum ChartDomain {
13 Auto, Fixed(val minimum: Double, val maximum: Double)
16public class ChartAxis public constructor(
17 public val kind: ChartAxisKind = ChartAxisKind.Linear,
18 public val domain: ChartDomain = ChartDomain.Auto,
19 public val includeZero: Boolean = false,
20 public val nice: Boolean = true,
21 public val tickCount: Int = 5,
22 public val shown: Boolean = true,
23 public val grid: Boolean = true,
24 public val formatter: ((Double) -> String)? = None
25) {
26 init {
27 requireComponent(tickCount > 0, "chart tick count must be positive")
28 when (domain) {
29 case Fixed(val low, val high) -> requireComponent(
30 low.isFinite() && high.isFinite() && high > low,
31 "chart domain must be finite and increasing"
32 );
33 case Auto -> {}
34 }
35 }
38internal val chartCategoryAxis = ChartAxis(ChartAxisKind.Category, grid = false)
40internal val chartValueAxis = ChartAxis()
42public class ChartTooltipOptions public constructor(
43 public val hideLabel: Boolean = false,
44 public val hideIndicator: Boolean = false,
45 public val indicator: ChartIndicator = ChartIndicator.Dot,
46 public val formatter: ((ChartTooltipEntry) -> String)? = None,
47 public val labelFormatter: ((ChartDatum) -> String)? = None
50public class ChartLegendOptions public constructor(
51 public val hideIcon: Boolean = false,
52 public val formatter: ((ChartSeries) -> String)? = None
55public class ChartProps public constructor(
56 public val model: ChartModel,
57 public val kind: ChartKind = ChartKind.Line,
58 public val orientation: Orientation = Orientation.Vertical,
59 public val curve: ChartCurve = ChartCurve.Linear,
60 public val stack: ChartStack = ChartStack.None,
61 public val connectMissing: Boolean = false,
62 public val xAxis: ChartAxis = ChartAxis(ChartAxisKind.Category, grid = false),
63 public val yAxis: ChartAxis = ChartAxis(),
64 public val pointRadius: Double = 3.0,
65 public val barRadius: Double = 4.0,
66 public val barGap: Double = 0.2,
67 public val labels: Boolean = false,
68 public val startAngle: Double = -PI / 2.0,
69 public val endAngle: Double = 3.0 * PI / 2.0,
70 public val innerRadius: Double = 0.0,
71 public val width: Double? = None,
72 public val height: Double? = None,
73 public val legend: Boolean = true,
74 public val tooltip: Boolean = true,
75 public val legendOptions: ChartLegendOptions = ChartLegendOptions(),
76 public val tooltipOptions: ChartTooltipOptions = ChartTooltipOptions(),
77 public val tooltipContent: ((ChartTooltipPayload) -> View)? = None,
78 public val center: View? = None,
79 public val active: Binding<ChartSelection?>? = None,
80 public val focus: FocusOptions = FocusOptions(),
81 public val enabled: Boolean = true,
82 public val fonts: FontCollection? = None,
83 public val theme: ThemeSource? = None,
84 public val semantics: ComponentSemantics = ComponentSemantics()
85) {
86 init {
87 nonnegative(pointRadius, "invalid chart point radius")
88 nonnegative(barRadius, "invalid chart bar radius")
89 requireComponent(
90 barGap.isFinite() && barGap >= 0.0 && barGap < 1.0,
91 "chart bar gap must be in [0, 1)"
92 )
93 when (width) {
94 case Some(val value) -> nonnegative(value, "invalid chart width");
95 case None -> {}
96 }
97 when (height) {
98 case Some(val value) -> nonnegative(value, "invalid chart height");
99 case None -> {}
100 }
101 requireComponent(yAxis.kind == ChartAxisKind.Linear, "chart value axis must be linear")
102 val sweep = endAngle - startAngle
103 requireComponent(
104 startAngle.isFinite() && endAngle.isFinite() && sweep != 0.0 && abs(sweep) <= TAU,
105 "chart angle span must be finite, nonzero and at most one turn"
106 )
107 requireComponent(
108 innerRadius.isFinite() && innerRadius >= 0.0 && innerRadius < 1.0,
109 "chart inner radius must be in [0, 1)"
110 )
111 }
114public class ChartBuilder internal constructor() {
115 public var kind: ChartKind = ChartKind.Line
116 public var orientation: Orientation = Orientation.Vertical
117 public var curve: ChartCurve = ChartCurve.Linear
118 public var stack: ChartStack = ChartStack.None
119 public var connectMissing: Boolean = false
120 public var xAxis: ChartAxis = chartCategoryAxis
121 public var yAxis: ChartAxis = chartValueAxis
122 public var pointRadius: Double = 3.0
123 public var barRadius: Double = 4.0
124 public var barGap: Double = 0.2
125 public var labels: Boolean = false
126 public var startAngle: Double = -PI / 2.0
127 public var endAngle: Double = 3.0 * PI / 2.0
128 public var innerRadius: Double = 0.0
129 public var width: Double? = None
130 public var height: Double? = None
131 public var legend: Boolean = true
132 public var tooltip: Boolean = true
133 public var legendOptions: ChartLegendOptions = ChartLegendOptions()
134 public var tooltipOptions: ChartTooltipOptions = ChartTooltipOptions()
135 public var tooltipContent: ((ChartTooltipPayload) -> View)? = None
136 public var center: View? = None
137 public var active: Binding<ChartSelection?>? = None
138 public var focus: FocusOptions = FocusOptions()
139 public var enabled: Boolean = true
140 public var fonts: FontCollection? = None
141 public var theme: ThemeSource? = None
142 public var semantics: ComponentSemantics = ComponentSemantics()
143 public var key: ViewKey = ViewKey.Position
144 internal fun build(model: ChartModel): View =
145 component(
146 ChartProps(
147 model,
148 kind,
149 orientation,
150 curve,
151 stack,
152 connectMissing,
153 xAxis,
154 yAxis,
155 pointRadius,
156 barRadius,
157 barGap,
158 labels,
159 startAngle,
160 endAngle,
161 innerRadius,
162 width,
163 height,
164 legend,
165 tooltip,
166 legendOptions,
167 tooltipOptions,
168 tooltipContent,
169 center,
170 active,
171 focus,
172 enabled,
173 fonts,
174 theme,
175 semantics
176 ),
177 { props, scope -> Chart(props, scope) },
178 key
179 )
182public fun chart(model: ChartModel, configure: ChartBuilder.() -> Unit = {}): View {
183 val builder = ChartBuilder();
184 builder.configure();
185 return builder.build(model)