2020-05-11 05:37:22 +00:00
|
|
|
export class RelativeScale {
|
2020-06-05 22:02:31 +00:00
|
|
|
static scale (data, tickCount, maxFactor) {
|
2020-06-08 21:29:15 +00:00
|
|
|
const { min, max } = RelativeScale.calculateBounds(data)
|
2020-05-11 04:39:35 +00:00
|
|
|
|
|
|
|
let factor = 1
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const scale = Math.pow(10, factor)
|
|
|
|
|
|
|
|
const scaledMin = min - (min % scale)
|
2021-05-31 06:27:20 +00:00
|
|
|
let scaledMax = max + (max % scale === 0 ? 0 : scale - (max % scale))
|
2020-06-18 03:22:29 +00:00
|
|
|
|
|
|
|
// Prevent min/max from being equal (and generating 0 ticks)
|
|
|
|
// This happens when all data points are products of scale value
|
|
|
|
if (scaledMin === scaledMax) {
|
|
|
|
scaledMax += scale
|
|
|
|
}
|
2020-05-11 04:39:35 +00:00
|
|
|
|
|
|
|
const ticks = (scaledMax - scaledMin) / scale
|
|
|
|
|
2020-06-08 21:29:15 +00:00
|
|
|
if (ticks <= tickCount || (typeof maxFactor === 'number' && factor === maxFactor)) {
|
|
|
|
return {
|
|
|
|
scaledMin,
|
|
|
|
scaledMax,
|
|
|
|
scale
|
|
|
|
}
|
2020-05-11 04:39:35 +00:00
|
|
|
} else {
|
|
|
|
// Too many steps between min/max, increase factor and try again
|
|
|
|
factor++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:02:31 +00:00
|
|
|
static scaleMatrix (data, tickCount, maxFactor) {
|
2021-05-31 06:27:20 +00:00
|
|
|
const nonNullData = data.flat().filter((val) => val !== null)
|
2020-05-11 08:10:23 +00:00
|
|
|
|
2021-05-31 06:27:20 +00:00
|
|
|
// when used with the spread operator large nonNullData/data arrays can reach the max call stack size
|
|
|
|
// use reduce calls to safely determine min/max values for any size of array
|
|
|
|
// https://stackoverflow.com/questions/63705432/maximum-call-stack-size-exceeded-when-using-the-dots-operator/63706516#63706516
|
|
|
|
const max = nonNullData.reduce((a, b) => {
|
|
|
|
return Math.max(a, b)
|
2021-06-07 16:51:47 +00:00
|
|
|
}, 0)
|
2021-05-31 06:27:20 +00:00
|
|
|
|
|
|
|
return RelativeScale.scale(
|
|
|
|
[0, RelativeScale.isFiniteOrZero(max)],
|
|
|
|
tickCount,
|
|
|
|
maxFactor
|
|
|
|
)
|
2020-05-11 07:28:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 04:39:35 +00:00
|
|
|
static generateTicks (min, max, step) {
|
|
|
|
const ticks = []
|
|
|
|
for (let i = min; i <= max; i += step) {
|
|
|
|
ticks.push(i)
|
|
|
|
}
|
|
|
|
return ticks
|
|
|
|
}
|
|
|
|
|
|
|
|
static calculateBounds (data) {
|
|
|
|
if (data.length === 0) {
|
2020-06-08 21:29:15 +00:00
|
|
|
return {
|
|
|
|
min: 0,
|
|
|
|
max: 0
|
2020-05-11 04:39:35 +00:00
|
|
|
}
|
2020-06-08 21:29:15 +00:00
|
|
|
} else {
|
2021-05-31 06:27:20 +00:00
|
|
|
const nonNullData = data.filter((val) => val !== null)
|
|
|
|
|
|
|
|
// when used with the spread operator large nonNullData/data arrays can reach the max call stack size
|
|
|
|
// use reduce calls to safely determine min/max values for any size of array
|
|
|
|
// https://stackoverflow.com/questions/63705432/maximum-call-stack-size-exceeded-when-using-the-dots-operator/63706516#63706516
|
|
|
|
const min = nonNullData.reduce((a, b) => {
|
|
|
|
return Math.min(a, b)
|
2021-06-07 16:51:47 +00:00
|
|
|
}, 0)
|
2021-05-31 06:27:20 +00:00
|
|
|
const max = nonNullData.reduce((a, b) => {
|
|
|
|
return Math.max(a, b)
|
2021-06-07 16:51:47 +00:00
|
|
|
}, 0)
|
2020-05-11 04:39:35 +00:00
|
|
|
|
2020-06-08 21:29:15 +00:00
|
|
|
return {
|
|
|
|
min: RelativeScale.isFiniteOrZero(min),
|
|
|
|
max: RelativeScale.isFiniteOrZero(max)
|
2020-05-11 08:10:23 +00:00
|
|
|
}
|
2020-05-11 04:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 21:29:15 +00:00
|
|
|
|
|
|
|
static isFiniteOrZero (val) {
|
|
|
|
return Number.isFinite(val) ? val : 0
|
|
|
|
}
|
2020-05-11 04:39:35 +00:00
|
|
|
}
|