From 9cc7e1ccd9fa1d9e836f2d53ff2b78328fdd50a0 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 31 Jan 2020 11:33:07 +0100 Subject: [PATCH] Coordinate System raising events when scale changes --- ChartsSpace/CoordinateSystem.js | 15 ++++++++++++++- ChartsSpace/TimeMachine.js | 2 ++ ChartsSpace/TimelineChart.js | 1 + Plotter.js | 18 ++++++++++++++++++ Scales/RateScale.js | 14 +++++++------- Scales/TimeScale.js | 12 +++++++----- 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/ChartsSpace/CoordinateSystem.js b/ChartsSpace/CoordinateSystem.js index 5fc9728fe..a8757a1b7 100644 --- a/ChartsSpace/CoordinateSystem.js +++ b/ChartsSpace/CoordinateSystem.js @@ -14,6 +14,7 @@ function newCoordinateSystem () { maxHeight: undefined, maxWidth: undefined, scale: undefined, + eventHandler: undefined, recalculateScale: recalculateScale, transformThisPoint: transformThisPoint, transformThisPoint2: transformThisPoint2, @@ -21,7 +22,8 @@ function newCoordinateSystem () { inverseTransformUncappedY: inverseTransformUncappedY, initializeX: initializeX, initializeY: initializeY, - initialize: initialize + initialize: initialize, + finalize: finalize } thisObject.min = { @@ -37,8 +39,16 @@ function newCoordinateSystem () { y: 0 } + thisObject.eventHandler = newEventHandler() + thisObject.eventHandler.name = 'Coordinate System' + return thisObject + function finalize () { + thisObject.eventHandler.finalize() + thisObject.eventHandler = undefined + } + function initialize (minValue, maxValue, pMaxWidth, pMaxHeight) { /* Defines the min and max value of rate that we are going to transport to the available screen at the center position. */ thisObject.min.x = minValue.x // * 0.999; // 0.1% less @@ -57,6 +67,9 @@ function newCoordinateSystem () { /* Defines the initial Zoom level at center position. */ thisObject.scale.x = thisObject.maxWidth / (thisObject.max.x - thisObject.min.x) thisObject.scale.y = thisObject.maxHeight / (thisObject.max.y - thisObject.min.y) + + thisObject.eventHandler.raiseEvent('Scale Changed') + console.log('Scale Changed') } function initializeX (minValue, maxValue, maxWidth) { diff --git a/ChartsSpace/TimeMachine.js b/ChartsSpace/TimeMachine.js index a046f350c..0534784bb 100644 --- a/ChartsSpace/TimeMachine.js +++ b/ChartsSpace/TimeMachine.js @@ -104,6 +104,8 @@ function newTimeMachine () { mouse = undefined timelineChartsMap = undefined + + timeMachineCoordinateSystem.finalize() timeMachineCoordinateSystem = undefined } diff --git a/ChartsSpace/TimelineChart.js b/ChartsSpace/TimelineChart.js index 8ff2e5d0b..fb39a1a36 100644 --- a/ChartsSpace/TimelineChart.js +++ b/ChartsSpace/TimelineChart.js @@ -86,6 +86,7 @@ function newTimelineChart () { logger = undefined timeMachineCoordinateSystem = undefined + timelineChartCoordinateSystem.finalize() timelineChartCoordinateSystem = undefined coordinateSystem = undefined layersPanel = undefined diff --git a/Plotter.js b/Plotter.js index 3f5d2b1d4..d3797b9f7 100644 --- a/Plotter.js +++ b/Plotter.js @@ -52,6 +52,7 @@ function newPlotter () { let marketFilesUpdatedEventSubscriptionId let dailyFilesUpdatedEventSubscriptionId let onDisplaceEventSubscriptionId + let scaleChangedEventSubscriptionId let logged = false return thisObject @@ -78,6 +79,7 @@ function newPlotter () { marketFile = undefined fileCursor = undefined + finalizeCoordinateSystem() coordinateSystem = undefined slotCoordinateSystem = undefined plotterModuleConfig = undefined @@ -99,6 +101,7 @@ function newPlotter () { datetime = pDatetime timeFrame = pTimeFrame coordinateSystem = pCoordinateSystem + initializeCoordinateSystem() productDefinition = pProductDefinition @@ -131,6 +134,19 @@ function newPlotter () { } } + function initializeCoordinateSystem () { + scaleChangedEventSubscriptionId = coordinateSystem.eventHandler.listenToEvent('Scale Changed', onScaleChanged) + } + + function finalizeCoordinateSystem () { + coordinateSystem.eventHandler.stopListening(scaleChangedEventSubscriptionId) + } + + function onScaleChanged () { + mustRecalculateDataPoints = true + recalculate() + } + function onMouseOver (event) { let userPosition = getDateFromPoint(event, thisObject.container, coordinateSystem) userPositionDate = userPosition.valueOf() @@ -208,7 +224,9 @@ function newPlotter () { } function setCoordinateSystem (pCoordinateSystem) { + finalizeCoordinateSystem() coordinateSystem = pCoordinateSystem + initializeCoordinateSystem() } function onDailyFileLoaded (event) { diff --git a/Scales/RateScale.js b/Scales/RateScale.js index 232c4c309..907c8f484 100644 --- a/Scales/RateScale.js +++ b/Scales/RateScale.js @@ -277,11 +277,13 @@ function newRateScale () { ) { // not using this value } else { - thisObject.minValue = code.minValue - thisObject.maxValue = code.maxValue - coordinateSystem.min.y = thisObject.minValue - coordinateSystem.max.y = thisObject.maxValue - coordinateSystem.recalculateScale() + if (thisObject.minValue !== code.minValue || thisObject.maxValue !== code.maxValue) { + thisObject.minValue = code.minValue + thisObject.maxValue = code.maxValue + coordinateSystem.min.y = thisObject.minValue + coordinateSystem.max.y = thisObject.maxValue + coordinateSystem.recalculateScale() + } } saveObjectState() // this overrides any invalid value at the config. } catch (err) { @@ -393,11 +395,9 @@ function newRateScale () { if (rate < coordinateSystem.min.y) { rate = coordinateSystem.min.y - coordinateSystem.recalculateScale() } if (rate > coordinateSystem.max.y) { rate = coordinateSystem.max.y - coordinateSystem.recalculateScale() } let label = (rate - Math.trunc(rate)).toFixed(2) diff --git a/Scales/TimeScale.js b/Scales/TimeScale.js index ccbe3cbc2..7e822a723 100644 --- a/Scales/TimeScale.js +++ b/Scales/TimeScale.js @@ -200,11 +200,13 @@ function newTimeScale () { ) { // not using this value } else { - thisObject.fromDate = Date.parse(code.fromDate) - thisObject.toDate = Date.parse(code.toDate) - coordinateSystem.min.x = thisObject.fromDate - coordinateSystem.max.x = thisObject.toDate - coordinateSystem.recalculateScale() + if (thisObject.fromDate !== Date.parse(code.fromDate) || thisObject.toDate !== Date.parse(code.toDate)) { + thisObject.fromDate = Date.parse(code.fromDate) + thisObject.toDate = Date.parse(code.toDate) + coordinateSystem.min.x = thisObject.fromDate + coordinateSystem.max.x = thisObject.toDate + coordinateSystem.recalculateScale() + } } saveObjectState() // this overrides any invalid value at the config. } catch (err) {