add maxFactor to RelativeScale to prevent large gaps

This commit is contained in:
Nick Krecklow 2020-06-05 17:02:31 -05:00
parent 00347ed0a9
commit 2336c9f78b
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94
2 changed files with 7 additions and 6 deletions

@ -160,6 +160,7 @@ export class GraphDisplayManager {
}) })
const tickCount = 10 const tickCount = 10
const maxFactor = 4
// eslint-disable-next-line new-cap // eslint-disable-next-line new-cap
this._plotInstance = new uPlot({ this._plotInstance = new uPlot({
@ -237,7 +238,7 @@ export class GraphDisplayManager {
}, },
split: () => { split: () => {
const visibleGraphData = this.getVisibleGraphData() const visibleGraphData = this.getVisibleGraphData()
const [, max, scale] = RelativeScale.scaleMatrix(visibleGraphData, tickCount) const [, max, scale] = RelativeScale.scaleMatrix(visibleGraphData, tickCount, maxFactor)
const ticks = RelativeScale.generateTicks(0, max, scale) const ticks = RelativeScale.generateTicks(0, max, scale)
return ticks return ticks
} }
@ -248,7 +249,7 @@ export class GraphDisplayManager {
auto: false, auto: false,
range: () => { range: () => {
const visibleGraphData = this.getVisibleGraphData() const visibleGraphData = this.getVisibleGraphData()
const [, scaledMax] = RelativeScale.scaleMatrix(visibleGraphData, tickCount) const [, scaledMax] = RelativeScale.scaleMatrix(visibleGraphData, tickCount, maxFactor)
return [0, scaledMax] return [0, scaledMax]
} }
} }

@ -1,5 +1,5 @@
export class RelativeScale { export class RelativeScale {
static scale (data, tickCount) { static scale (data, tickCount, maxFactor) {
const [min, max] = RelativeScale.calculateBounds(data) const [min, max] = RelativeScale.calculateBounds(data)
let factor = 1 let factor = 1
@ -12,7 +12,7 @@ export class RelativeScale {
const ticks = (scaledMax - scaledMin) / scale const ticks = (scaledMax - scaledMin) / scale
if (ticks < tickCount + 1) { if (ticks < tickCount + 1 || (typeof maxFactor === 'number' && factor === maxFactor)) {
return [scaledMin, scaledMax, scale] return [scaledMin, scaledMax, scale]
} else { } else {
// Too many steps between min/max, increase factor and try again // Too many steps between min/max, increase factor and try again
@ -21,7 +21,7 @@ export class RelativeScale {
} }
} }
static scaleMatrix (data, tickCount) { static scaleMatrix (data, tickCount, maxFactor) {
let max = Number.MIN_VALUE let max = Number.MIN_VALUE
for (const row of data) { for (const row of data) {
@ -42,7 +42,7 @@ export class RelativeScale {
max = 0 max = 0
} }
return RelativeScale.scale([0, max], tickCount) return RelativeScale.scale([0, max], tickCount, maxFactor)
} }
static generateTicks (min, max, step) { static generateTicks (min, max, step) {