2020-05-11 05:37:22 +00:00
|
|
|
export class RelativeScale {
|
2020-05-11 04:39:35 +00:00
|
|
|
static scale (data, tickCount) {
|
|
|
|
const [min, max] = RelativeScale.calculateBounds(data)
|
|
|
|
|
|
|
|
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-05-11 23:12:29 +00:00
|
|
|
if (ticks < tickCount + 1) {
|
2020-05-11 04:39:35 +00:00
|
|
|
return [scaledMin, scaledMax, scale]
|
|
|
|
} else {
|
|
|
|
// Too many steps between min/max, increase factor and try again
|
|
|
|
factor++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 07:28:41 +00:00
|
|
|
static scaleMatrix (data, tickCount) {
|
|
|
|
let max = Number.MIN_VALUE
|
|
|
|
|
|
|
|
for (const row of data) {
|
|
|
|
let testMax = Number.MIN_VALUE
|
|
|
|
|
|
|
|
for (const point of row) {
|
|
|
|
if (point > testMax) {
|
|
|
|
testMax = point
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (testMax > max) {
|
|
|
|
max = testMax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:10:23 +00:00
|
|
|
if (max === Number.MAX_VALUE) {
|
|
|
|
max = 0
|
|
|
|
}
|
|
|
|
|
2020-05-11 07:28:41 +00:00
|
|
|
return RelativeScale.scale([0, max], tickCount)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return [0, 0]
|
|
|
|
} else {
|
|
|
|
let min = Number.MAX_VALUE
|
|
|
|
let max = Number.MIN_VALUE
|
|
|
|
|
|
|
|
for (const point of data) {
|
2020-05-11 08:10:23 +00:00
|
|
|
if (typeof point === 'number') {
|
|
|
|
if (point > max) {
|
|
|
|
max = point
|
|
|
|
}
|
|
|
|
if (point < min) {
|
|
|
|
min = point
|
|
|
|
}
|
2020-05-11 04:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:10:23 +00:00
|
|
|
if (min === Number.MAX_VALUE) {
|
|
|
|
min = 0
|
|
|
|
}
|
|
|
|
if (max === Number.MIN_VALUE) {
|
|
|
|
max = 0
|
|
|
|
}
|
|
|
|
|
2020-05-11 04:39:35 +00:00
|
|
|
return [min, max]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|