scoop:scoop.ui.component:0.1.0
src/chart/scale.scoop
Selected production source · src/chart/scale.scoop
1
package scoop.ui.component3
import scoop.math.*5
internal struct ChartRange(val minimum: Double, val maximum: Double) {6
fun unit(value: Double): Double {7
val span = maximum - minimum8
return if (span.isFinite())9
(value - minimum) / span10
else11
(value / 2.0 - minimum / 2.0) / (maximum / 2.0 - minimum / 2.0)12
}13
}15
internal fun chartNumber(value: Double): String =16
if (abs(value) < 9000000000000000.0 && floor(value) == value)17
f"${value.toLong()}"18
else19
f"${value}"21
internal fun chartFormat(axis: ChartAxis, value: Double): String =22
when (axis.formatter) {23
case Some(val format) -> format(value);24
case None -> chartNumber(value)25
}27
internal fun chartDomain(28
axis: ChartAxis,29
minimum: Double?,30
maximum: Double?,31
baseline: Boolean = false32
): 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.038
var high = maximum ?: 1.039
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.046
else {47
val pad = abs(low) * 0.148
val lower = low - pad49
val upper = high + pad50
low =51
if (lower.isFinite() && lower < low)52
lower53
else54
if (low > 0.0)55
0.056
else57
low58
high =59
if (upper.isFinite() && upper > high)60
upper61
else62
if (high < 0.0)63
0.064
else65
high66
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) * step76
val last = ceil(high / step) * step77
if (first.isFinite() && last.isFinite() && last > first) {78
low = first;79
high = last80
}81
}82
}83
return ChartRange(low, high)84
}86
/** D3-array's 1/2/5 tick thresholds, with a finite endpoint fallback. See LICENSE-D3. */87
internal 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 step91
val power = floor(log10(step))92
val unit = pow(10.0, power)93
val error = step / unit94
val factor =95
if (error >= sqrt(50.0))96
10.097
else98
if (error >= sqrt(10.0))99
5.0100
else101
if (error >= sqrt(2.0))102
2.0103
else104
1.0105
return unit * factor106
}108
internal 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 / step116
else117
0.0118
val first =119
if (inverse > 0.0 && inverse.isFinite())120
ceil(range.minimum * inverse)121
else122
ceil(range.minimum / step)123
val last =124
if (inverse > 0.0 && inverse.isFinite())125
floor(range.maximum * inverse)126
else127
floor(range.maximum / step)128
val size = last - first + 1.0129
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.0139
)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 / inverse146
else147
tick * step148
}149
}151
internal fun chartClamp(value: Double, minimum: Double, maximum: Double): Double =152
upperBound(lowerBound(value, minimum), maximum)