scoop:scoop.ui.component:0.1.0
src/chart/cartesian.scoop
Selected production source · src/chart/cartesian.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal fun chartCartesian(plan: ChartPlan, size: Size): ChartGeometry {9
val input = plan.input10
val prepared = plan.prepared11
val a = plan.appearance12
val d = a.density13
val horizontal = input.orientation == Orientation.Horizontal14
val rtl = a.direction == LayoutDirection.Rtl15
val count = plan.snapshot.data.datums.size16
val labels = ArrayList<ChartLabel>()17
val grid = ArrayList<ChartGrid>()18
val valueTicks = chartTicks(prepared.yRange, input.yAxis.tickCount)19
var valueWidth = 0.020
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.025
for (datum in plan.snapshot.data.datums)26
categoryWidth = lowerBound(categoryWidth, a.measure(datum.label).width)27
val line = a.measure("0").height28
val side =29
if (horizontal) {30
if (input.xAxis.shown)31
upperBound(categoryWidth, size.width / 3.0) + 12.0 * d32
else33
12.0 * d34
} else35
if (input.yAxis.shown)36
valueWidth + 12.0 * d37
else38
12.0 * d39
val bottom =40
if (41
if (horizontal)42
input.yAxis.shown43
else44
input.xAxis.shown45
)46
line + 12.0 * d47
else48
12.0 * d49
val top =50
if (input.labels)51
line + 12.0 * d52
else53
12.0 * d54
val plot =55
Rect(56
if (rtl)57
12.0 * d58
else59
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.height69
else70
plot.width71
val band =72
if (count > 0L)73
extent / count.toDouble()74
else75
extent76
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
else81
prepared.xRange.unit(prepared.x[index])82
return if (horizontal)83
plot.y + unit * plot.height84
else85
plot.x +86
(87
if (rtl)88
1.0 - unit89
else90
unit91
) *92
plot.width93
}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 - unit101
else102
unit103
) *104
plot.width105
else106
plot.y + (1.0 - unit) * plot.height107
}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
else117
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 * d131
)132
else133
Point(134
if (rtl)135
plot.x + plot.width + 8.0 * d136
else137
plot.x - measured.width - 8.0 * d,138
position - measured.height / 2.0139
)140
)141
)142
}143
if (input.xAxis.kind == ChartAxisKind.Category) {144
val stride =145
lowerBound(146
ceil(147
(148
(149
if (horizontal)150
line151
else152
categoryWidth153
) +154
8.0 *155
d156
) /157
lowerBound(band, 1.0)158
),159
1.0160
).toLong()161
for (at in 0L ..< count)162
if (at % stride == 0L) {163
val text = plan.snapshot.data.datums[at].label164
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
else171
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 * d184
else185
plot.x - measured.width - 8.0 * d,186
position - measured.height / 2.0187
)188
else189
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 * d196
)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.height207
else208
plot.x +209
(210
if (rtl)211
1.0 - unit212
else213
unit214
) *215
plot.width216
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
else223
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 * d233
else234
plot.x - measured.width - 8.0 * d,235
position - measured.height / 2.0236
)237
else238
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 * d245
)246
)247
)248
}249
}250
var bars = 0L251
for (series in prepared.series)252
if (series.mark == ChartMark.Bar)253
bars += 1L254
var bar = 0L255
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
else261
1.0262
val groupWidth = band * (1.0 - input.barGap)263
val width = groupWidth / groupCount264
val offset =265
if (input.stack == ChartStack.None)266
-groupWidth / 2.0 + (bar.toDouble() + 0.5) * width267
else268
0.0269
val samples =270
Array<ChartSample?>(count) {271
at -> when (series.samples[at]) {272
case None -> None273
case Some(val sample) -> {274
val center =275
category(at) +276
(277
if (series.mark == ChartMark.Bar)278
(279
if (!horizontal && rtl)280
-offset281
else282
offset283
)284
else285
0.0286
)287
val end = value(sample.end)288
val start = value(sample.base)289
val point =290
if (horizontal)291
Point(end, center)292
else293
Point(center, end)294
val base =295
if (horizontal)296
Point(start, center)297
else298
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
width308
)309
else310
Rect(311
center - width / 2.0,312
upperBound(start, end),313
width,314
abs(end - start)315
)316
)317
else318
ChartShape.Point319
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].key333
),334
sample.value,335
point,336
base,337
shape,338
visible339
)340
)341
}342
}343
}344
result.add(ChartSeriesGeometry(series, samples))345
if (series.mark == ChartMark.Bar)346
bar += 1L347
}348
return ChartGeometry(size, plot, result.toArray(), labels.toArray(), grid.toArray(), input.kind)349
}