From fbf682d5fac8da8ec32231acc988c4c80fe617f7 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Sat, 20 Jun 2015 10:28:49 -0700 Subject: [PATCH] [Plot] Support fixed duration Support a fixed duration for plots, WTD-1273. --- platform/features/plot/src/PlotController.js | 7 +-- .../plot/src/elements/PlotLineBuffer.js | 6 +-- .../features/plot/src/elements/PlotUpdater.js | 52 ++++++++++++++++--- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/platform/features/plot/src/PlotController.js b/platform/features/plot/src/PlotController.js index fcce051968..106e0f94d5 100644 --- a/platform/features/plot/src/PlotController.js +++ b/platform/features/plot/src/PlotController.js @@ -99,7 +99,8 @@ define( updater = new PlotUpdater( handle, ($scope.axes[0].active || {}).key, - ($scope.axes[1].active || {}).key + ($scope.axes[1].active || {}).key, + 15000 // 15 seconds ); } @@ -161,7 +162,7 @@ define( // Unsubscribe when the plot is destroyed $scope.$on("$destroy", releaseSubscription); - + // Create a throttled update function scheduleUpdate = throttle(function () { modeOptions.getModeHandler().getSubPlots() @@ -248,4 +249,4 @@ define( return PlotController; } -); \ No newline at end of file +); diff --git a/platform/features/plot/src/elements/PlotLineBuffer.js b/platform/features/plot/src/elements/PlotLineBuffer.js index 2862a7e6be..e51e6e8a61 100644 --- a/platform/features/plot/src/elements/PlotLineBuffer.js +++ b/platform/features/plot/src/elements/PlotLineBuffer.js @@ -43,9 +43,9 @@ define( var mid = Math.floor((min + max) / 2), found = buffer[mid * 2]; - // Collisions are not wanted + // On collisions, insert at same index if (found === value) { - return -1; + return mid; } // Otherwise, if we're down to a single index, @@ -258,4 +258,4 @@ define( return PlotLineBuffer; } -); \ No newline at end of file +); diff --git a/platform/features/plot/src/elements/PlotUpdater.js b/platform/features/plot/src/elements/PlotUpdater.js index df7eef9abc..af19ef3eb3 100644 --- a/platform/features/plot/src/elements/PlotUpdater.js +++ b/platform/features/plot/src/elements/PlotUpdater.js @@ -42,8 +42,10 @@ define( * @param {TelemetryHandle} handle the handle to telemetry access * @param {string} domain the key to use when looking up domain values * @param {string} range the key to use when looking up range values + * @param {number} maxDuration maximum plot duration to display + * @param {number} maxPoints maximum number of points to display */ - function PlotUpdater(handle, domain, range, maxPoints) { + function PlotUpdater(handle, domain, range, fixedDuration, maxPoints) { var ids = [], lines = {}, dimensions = [0, 0], @@ -107,6 +109,7 @@ define( lines = next; } + // Initialize the domain offset, based on these observed values function initializeDomainOffset(values) { domainOffset = @@ -147,6 +150,35 @@ define( [dimensionsOf(domainExtrema), 2.0 ] : [dimensionsOf(domainExtrema), dimensionsOf(rangeExtrema)]; origin = [originOf(domainExtrema), originOf(rangeExtrema)]; + + if (fixedDuration !== undefined) { + origin[0] = Math.min( + origin[0], + origin[0] + dimensions[0] - fixedDuration + ); + dimensions[0] = Math.max(dimensions[0], fixedDuration); + } + } + } + + // Enforce maximum duration on all plot lines; not that + // domain extrema must be up-to-date for this to behave correctly. + function enforceDuration() { + var cutoff; + + function enforceDurationForBuffer(plotLineBuffer) { + var index = plotLineBuffer.findInsertionIndex(cutoff); + if (index > 0) { + plotLineBuffer.trim(index); + } + } + + if (fixedDuration !== undefined && + domainExtrema !== undefined && + (domainExtrema[1] - domainExtrema[0] > fixedDuration)) { + cutoff = domainExtrema[1] - fixedDuration; + bufferArray.forEach(enforceDurationForBuffer); + updateExtrema(); // Extrema may have changed now } } @@ -161,6 +193,12 @@ define( } } + // Update plot extremea and enforce maximum duration + function updateBounds() { + updateExtrema(); + enforceDuration(); + } + // Handle new telemetry data function update() { var objects = handle.getTelemetryObjects(); @@ -180,8 +218,8 @@ define( // Add new data objects.forEach(addPointFor); - // Finally, update extrema - updateExtrema(); + // Then, update extrema + updateBounds(); } // Add historical data for this domain object @@ -213,12 +251,12 @@ define( line.addSeries(series, domain, range); } - // Finally, update extrema - updateExtrema(); + // Update extrema + updateBounds(); } // Use a default MAX_POINTS if none is provided - maxPoints = maxPoints || MAX_POINTS; + maxPoints = maxPoints !== undefined ? maxPoints : MAX_POINTS; // Initially prepare state for these objects. // Note that this may be an empty array at this time, @@ -290,4 +328,4 @@ define( return PlotUpdater; } -); \ No newline at end of file +);