From 5b6d65e1c97cbc252875142d6629b1a36515312a Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Fri, 12 Jun 2020 18:32:53 -0500 Subject: [PATCH] simplify isZoomed historical graph update logic --- assets/js/graph.js | 40 ++++++++++------------------------------ assets/js/plugins.js | 24 ------------------------ 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/assets/js/graph.js b/assets/js/graph.js index cb59959..a52557d 100644 --- a/assets/js/graph.js +++ b/assets/js/graph.js @@ -3,7 +3,7 @@ import uPlot from 'uplot' import { RelativeScale } from './scale' import { formatNumber, formatTimestampSeconds } from './util' -import { uPlotTooltipPlugin, uPlotIsZoomedPlugin } from './plugins' +import { uPlotTooltipPlugin } from './plugins' import { FAVORITE_SERVERS_STORAGE_KEY } from './favorites' @@ -18,7 +18,6 @@ export class GraphDisplayManager { this._hasLoadedSettings = false this._initEventListenersOnce = false this._showOnlyFavorites = false - this._isPlotZoomed = false } addGraphPoint (timestamp, playerCounts) { @@ -50,14 +49,14 @@ export class GraphDisplayManager { } } - // If not zoomed, paint updated data structure - // Otherwise flag the plot data as dirty with repainting to be handled by #handlePlotZoomOut - // This prevents #addGraphPoint calls from resetting the graph's zoom state - if (!this._isPlotZoomed) { - this._plotInstance.setData(this.getGraphData()) - } else { - this._isPlotZoomedDataDirty = true - } + // Dedrive plotTimestamps from the uPlot instance's data since this._graphTimestamps has been mutated + const plotTimestamps = this._plotInstance.data[0] + const plotScaleX = this._plotInstance.scales.x + + const isZoomed = plotScaleX.min > plotTimestamps[0] || plotScaleX.max < plotTimestamps[plotTimestamps.length - 1] + + // Avoid redrawing the plot when zoomed + this._plotInstance.setData(this.getGraphData(), !isZoomed) } loadLocalStorage () { @@ -251,8 +250,7 @@ export class GraphDisplayManager { } else { this._app.tooltip.hide() } - }), - uPlotIsZoomedPlugin(this.handlePlotZoomIn, this.handlePlotZoomOut) + }) ], ...this.getPlotSize(), cursor: { @@ -347,21 +345,6 @@ export class GraphDisplayManager { this._resizeRequestTimeout = undefined } - handlePlotZoomIn = () => { - this._isPlotZoomed = true - } - - handlePlotZoomOut = () => { - this._isPlotZoomed = false - - // Test if the data has changed while the plot was zoomed in - if (this._isPlotZoomedDataDirty) { - this._isPlotZoomedDataDirty = false - - this._plotInstance.setData(this.getGraphData()) - } - } - initEventListeners () { if (!this._initEventListenersOnce) { this._initEventListenersOnce = true @@ -469,9 +452,6 @@ export class GraphDisplayManager { this._graphData = [] this._hasLoadedSettings = false - this._isPlotZoomed = false - this._isPlotZoomedDataDirty = false - // Fire #clearTimeout if the timeout is currently defined if (this._resizeRequestTimeout) { clearTimeout(this._resizeRequestTimeout) diff --git a/assets/js/plugins.js b/assets/js/plugins.js index 1027d61..0595d7d 100644 --- a/assets/js/plugins.js +++ b/assets/js/plugins.js @@ -26,27 +26,3 @@ export function uPlotTooltipPlugin (onHover) { } } } - -export function uPlotIsZoomedPlugin (onZoomIn, onZoomOut) { - return { - hooks: { - setSelect: u => { - u._zoomPluginIgnoreNextSetScale = true - - if (onZoomIn) { - onZoomIn(u) - } - }, - setScale: u => { - if (typeof u._zoomPluginIgnoreNextSetScale !== 'boolean') { - return - } - if (u._zoomPluginIgnoreNextSetScale) { - u._zoomPluginIgnoreNextSetScale = false - } else if (onZoomOut) { - onZoomOut(u) - } - } - } - } -}