Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/scale.scoop

Selected production source · src/chart/scale.scoop

1package scoop.ui.component
3import scoop.math.*
5internal struct ChartRange(val minimum: Double, val maximum: Double) {
6 fun unit(value: Double): Double {
7 val span = maximum - minimum
8 return if (span.isFinite())
9 (value - minimum) / span
10 else
11 (value / 2.0 - minimum / 2.0) / (maximum / 2.0 - minimum / 2.0)
12 }
15internal fun chartNumber(value: Double): String =
16 if (abs(value) < 9000000000000000.0 && floor(value) == value)
17 f"${value.toLong()}"
18 else
19 f"${value}"
21internal fun chartFormat(axis: ChartAxis, value: Double): String =
22 when (axis.formatter) {
23 case Some(val format) -> format(value);
24 case None -> chartNumber(value)
25 }
27internal fun chartDomain(
28 axis: ChartAxis,
29 minimum: Double?,
30 maximum: Double?,
31 baseline: Boolean = false
32): ChartRange {
33 when (axis.domain) {
34 case Fixed(val low, val high) -> return ChartRange(low, high);
35 case Auto -> {}
36 }
37 var low = minimum ?: 0.0
38 var high = maximum ?: 1.0
39 if (axis.includeZero || baseline) {
40 low = upperBound(low, 0.0);
41 high = lowerBound(high, 0.0)
42 }
43 if (low == high) {
44 if (low == 0.0)
45 high = 1.0
46 else {
47 val pad = abs(low) * 0.1
48 val lower = low - pad
49 val upper = high + pad
50 low =
51 if (lower.isFinite() && lower < low)
52 lower
53 else
54 if (low > 0.0)
55 0.0
56 else
57 low
58 high =
59 if (upper.isFinite() && upper > high)
60 upper
61 else
62 if (high < 0.0)
63 0.0
64 else
65 high
66 if (low == high) {
67 low = upperBound(low, 0.0);
68 high = lowerBound(high, 1.0)
69 }
70 }
71 }
72 if (axis.nice) {
73 val step = chartTickStep(low, high, axis.tickCount)
74 if (step > 0.0 && step.isFinite()) {
75 val first = floor(low / step) * step
76 val last = ceil(high / step) * step
77 if (first.isFinite() && last.isFinite() && last > first) {
78 low = first;
79 high = last
80 }
81 }
82 }
83 return ChartRange(low, high)
86/** D3-array's 1/2/5 tick thresholds, with a finite endpoint fallback. See LICENSE-D3. */
87internal fun chartTickStep(low: Double, high: Double, count: Int): Double {
88 val step = high / count.toDouble() - low / count.toDouble()
89 if (!step.isFinite() || step <= 0.0)
90 return step
91 val power = floor(log10(step))
92 val unit = pow(10.0, power)
93 val error = step / unit
94 val factor =
95 if (error >= sqrt(50.0))
96 10.0
97 else
98 if (error >= sqrt(10.0))
99 5.0
100 else
101 if (error >= sqrt(2.0))
102 2.0
103 else
104 1.0
105 return unit * factor
108internal fun chartTicks(range: ChartRange, count: Int): Array<Double> {
109 val step = chartTickStep(range.minimum, range.maximum, count)
110 if (!step.isFinite() || step <= 0.0)
111 return [range.minimum, range.maximum]
112 // Integer indexes avoid accumulation drift for fractional decimal ticks.
113 val inverse =
114 if (step < 1.0)
115 1.0 / step
116 else
117 0.0
118 val first =
119 if (inverse > 0.0 && inverse.isFinite())
120 ceil(range.minimum * inverse)
121 else
122 ceil(range.minimum / step)
123 val last =
124 if (inverse > 0.0 && inverse.isFinite())
125 floor(range.maximum * inverse)
126 else
127 floor(range.maximum / step)
128 val size = last - first + 1.0
129 if (
130 !first.isFinite() ||
131 !last.isFinite() ||
132 !size.isFinite() ||
133 size <
134 1.0 ||
135 size >
136 count.toDouble() *
137 3.0 +
138 2.0
139 )
140 return [range.minimum, range.maximum]
141 return Array<Double>(size.toLong()) {
142 index ->
143 val tick = first + index.toDouble()
144 if (inverse > 0.0 && inverse.isFinite())
145 tick / inverse
146 else
147 tick * step
148 }
151internal fun chartClamp(value: Double, minimum: Double, maximum: Double): Double =
152 upperBound(lowerBound(value, minimum), maximum)