Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/cartesian.scoop

Selected production source · src/chart/cartesian.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.*
5import scoop.ui.*
6import scoop.ui.style.*
8internal fun chartCartesian(plan: ChartPlan, size: Size): ChartGeometry {
9 val input = plan.input
10 val prepared = plan.prepared
11 val a = plan.appearance
12 val d = a.density
13 val horizontal = input.orientation == Orientation.Horizontal
14 val rtl = a.direction == LayoutDirection.Rtl
15 val count = plan.snapshot.data.datums.size
16 val labels = ArrayList<ChartLabel>()
17 val grid = ArrayList<ChartGrid>()
18 val valueTicks = chartTicks(prepared.yRange, input.yAxis.tickCount)
19 var valueWidth = 0.0
20 val valueLabels =
21 Array<String>(valueTicks.size) { i -> chartFormat(input.yAxis, valueTicks[i]) }
22 for (text in valueLabels)
23 valueWidth = lowerBound(valueWidth, a.measure(text).width)
24 var categoryWidth = 0.0
25 for (datum in plan.snapshot.data.datums)
26 categoryWidth = lowerBound(categoryWidth, a.measure(datum.label).width)
27 val line = a.measure("0").height
28 val side =
29 if (horizontal) {
30 if (input.xAxis.shown)
31 upperBound(categoryWidth, size.width / 3.0) + 12.0 * d
32 else
33 12.0 * d
34 } else
35 if (input.yAxis.shown)
36 valueWidth + 12.0 * d
37 else
38 12.0 * d
39 val bottom =
40 if (
41 if (horizontal)
42 input.yAxis.shown
43 else
44 input.xAxis.shown
45 )
46 line + 12.0 * d
47 else
48 12.0 * d
49 val top =
50 if (input.labels)
51 line + 12.0 * d
52 else
53 12.0 * d
54 val plot =
55 Rect(
56 if (rtl)
57 12.0 * d
58 else
59 side,
60 top,
61 lowerBound(size.width - side - 12.0 * d, 0.0),
62 lowerBound(size.height - top - bottom, 0.0)
63 )
64 if (plot.width == 0.0 || plot.height == 0.0)
65 return ChartGeometry(size, plot, [], [], [])
66 val extent =
67 if (horizontal)
68 plot.height
69 else
70 plot.width
71 val band =
72 if (count > 0L)
73 extent / count.toDouble()
74 else
75 extent
76 fun category(index: Long): Double {
77 val unit =
78 if (input.xAxis.kind == ChartAxisKind.Category)
79 (index.toDouble() + 0.5) / lowerBound(count.toDouble(), 1.0)
80 else
81 prepared.xRange.unit(prepared.x[index])
82 return if (horizontal)
83 plot.y + unit * plot.height
84 else
85 plot.x +
86 (
87 if (rtl)
88 1.0 - unit
89 else
90 unit
91 ) *
92 plot.width
93 }
94 fun value(number: Double): Double {
95 val unit = prepared.yRange.unit(number)
96 return if (horizontal)
97 plot.x +
98 (
99 if (rtl)
100 1.0 - unit
101 else
102 unit
103 ) *
104 plot.width
105 else
106 plot.y + (1.0 - unit) * plot.height
107 }
108 for (at in 0L ..< valueTicks.size) {
109 val text = valueLabels[at]
110 val measured = a.measure(text)
111 val position = value(valueTicks[at])
112 if (input.yAxis.grid)
113 grid.add(
114 if (horizontal)
115 ChartGrid(Point(position, plot.y), Point(position, plot.y + plot.height))
116 else
117 ChartGrid(Point(plot.x, position), Point(plot.x + plot.width, position))
118 )
119 if (input.yAxis.shown)
120 labels.add(
121 ChartLabel(
122 text,
123 if (horizontal)
124 Point(
125 chartClamp(
126 position - measured.width / 2.0,
127 0.0,
128 lowerBound(size.width - measured.width, 0.0)
129 ),
130 plot.y + plot.height + 8.0 * d
131 )
132 else
133 Point(
134 if (rtl)
135 plot.x + plot.width + 8.0 * d
136 else
137 plot.x - measured.width - 8.0 * d,
138 position - measured.height / 2.0
139 )
140 )
141 )
142 }
143 if (input.xAxis.kind == ChartAxisKind.Category) {
144 val stride =
145 lowerBound(
146 ceil(
147 (
148 (
149 if (horizontal)
150 line
151 else
152 categoryWidth
153 ) +
154 8.0 *
155 d
156 ) /
157 lowerBound(band, 1.0)
158 ),
159 1.0
160 ).toLong()
161 for (at in 0L ..< count)
162 if (at % stride == 0L) {
163 val text = plan.snapshot.data.datums[at].label
164 val measured = a.measure(text)
165 val position = category(at)
166 if (input.xAxis.grid)
167 grid.add(
168 if (horizontal)
169 ChartGrid(Point(plot.x, position), Point(plot.x + plot.width, position))
170 else
171 ChartGrid(
172 Point(position, plot.y),
173 Point(position, plot.y + plot.height)
174 )
175 )
176 if (input.xAxis.shown)
177 labels.add(
178 ChartLabel(
179 text,
180 if (horizontal)
181 Point(
182 if (rtl)
183 plot.x + plot.width + 8.0 * d
184 else
185 plot.x - measured.width - 8.0 * d,
186 position - measured.height / 2.0
187 )
188 else
189 Point(
190 chartClamp(
191 position - measured.width / 2.0,
192 0.0,
193 lowerBound(size.width - measured.width, 0.0)
194 ),
195 plot.y + plot.height + 8.0 * d
196 )
197 )
198 )
199 }
200 } else {
201 val ticks = chartTicks(prepared.xRange, input.xAxis.tickCount)
202 for (tick in ticks) {
203 val unit = prepared.xRange.unit(tick)
204 val position =
205 if (horizontal)
206 plot.y + unit * plot.height
207 else
208 plot.x +
209 (
210 if (rtl)
211 1.0 - unit
212 else
213 unit
214 ) *
215 plot.width
216 val text = chartFormat(input.xAxis, tick)
217 val measured = a.measure(text)
218 if (input.xAxis.grid)
219 grid.add(
220 if (horizontal)
221 ChartGrid(Point(plot.x, position), Point(plot.x + plot.width, position))
222 else
223 ChartGrid(Point(position, plot.y), Point(position, plot.y + plot.height))
224 )
225 if (input.xAxis.shown)
226 labels.add(
227 ChartLabel(
228 text,
229 if (horizontal)
230 Point(
231 if (rtl)
232 plot.x + plot.width + 8.0 * d
233 else
234 plot.x - measured.width - 8.0 * d,
235 position - measured.height / 2.0
236 )
237 else
238 Point(
239 chartClamp(
240 position - measured.width / 2.0,
241 0.0,
242 lowerBound(size.width - measured.width, 0.0)
243 ),
244 plot.y + plot.height + 8.0 * d
245 )
246 )
247 )
248 }
249 }
250 var bars = 0L
251 for (series in prepared.series)
252 if (series.mark == ChartMark.Bar)
253 bars += 1L
254 var bar = 0L
255 val result = ArrayList<ChartSeriesGeometry>()
256 for (series in prepared.series) {
257 val groupCount =
258 if (input.stack == ChartStack.None)
259 lowerBound(bars.toDouble(), 1.0)
260 else
261 1.0
262 val groupWidth = band * (1.0 - input.barGap)
263 val width = groupWidth / groupCount
264 val offset =
265 if (input.stack == ChartStack.None)
266 -groupWidth / 2.0 + (bar.toDouble() + 0.5) * width
267 else
268 0.0
269 val samples =
270 Array<ChartSample?>(count) {
271 at -> when (series.samples[at]) {
272 case None -> None
273 case Some(val sample) -> {
274 val center =
275 category(at) +
276 (
277 if (series.mark == ChartMark.Bar)
278 (
279 if (!horizontal && rtl)
280 -offset
281 else
282 offset
283 )
284 else
285 0.0
286 )
287 val end = value(sample.end)
288 val start = value(sample.base)
289 val point =
290 if (horizontal)
291 Point(end, center)
292 else
293 Point(center, end)
294 val base =
295 if (horizontal)
296 Point(start, center)
297 else
298 Point(center, start)
299 val shape: ChartShape =
300 if (series.mark == ChartMark.Bar)
301 ChartShape.Rectangle(
302 if (horizontal)
303 Rect(
304 upperBound(start, end),
305 center - width / 2.0,
306 abs(end - start),
307 width
308 )
309 else
310 Rect(
311 center - width / 2.0,
312 upperBound(start, end),
313 width,
314 abs(end - start)
315 )
316 )
317 else
318 ChartShape.Point
319 val visible =
320 when (shape) {
321 case Rectangle(val rect) -> rect.width >
322 0.0 &&
323 rect.height >
324 0.0 &&
325 chartOverlap(plot, rect);
326 else -> chartInside(plot, point)
327 }
328 Some(
329 ChartSample(
330 ChartSelection(
331 plan.snapshot.data.series[series.source].key,
332 plan.snapshot.data.datums[at].key
333 ),
334 sample.value,
335 point,
336 base,
337 shape,
338 visible
339 )
340 )
341 }
342 }
343 }
344 result.add(ChartSeriesGeometry(series, samples))
345 if (series.mark == ChartMark.Bar)
346 bar += 1L
347 }
348 return ChartGeometry(size, plot, result.toArray(), labels.toArray(), grid.toArray(), input.kind)