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)
|
|
|
|
const scaledMax = max + (max % scale === 0 ? 0 : (scale - (max % scale)))
|
|
|
|
|
|
|
|
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) {
|
2020-06-12 23:59:59 +00:00
|
|
|
const max = Math.max(...data.flat().filter(val => val !== null))
|
2020-05-11 08:10:23 +00:00
|
|
|
|
2020-06-08 21:29:15 +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 {
|
2020-06-12 23:59:59 +00:00
|
|
|
const min = Math.min(...data.filter(val => val !== null))
|
|
|
|
const max = Math.max(...data.filter(val => val !== null))
|
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
|
|
|
}
|