diff --git a/lib/client/chart.js b/lib/client/chart.js index a5db09f416a..ffd513cd650 100644 --- a/lib/client/chart.js +++ b/lib/client/chart.js @@ -273,7 +273,7 @@ function init (client, d3, $) { chart.createAdjustedRange = function() { var adjustedRange = chart.createBrushedRange(); - adjustedRange[1] = new Date(adjustedRange[1].getTime() + client.forecastTime); + adjustedRange[1] = new Date(Math.max(adjustedRange[1].getTime(), client.forecastTime)); return adjustedRange; } @@ -609,6 +609,8 @@ function init (client, d3, $) { var currentBrushExtent = scrollBrushExtent; var currentRange = scrollRange; + chart.setForecastTime(); + chart.xScale.domain(currentRange); focusYDomain = dynamicDomainOrElse(focusYDomain); @@ -663,6 +665,10 @@ function init (client, d3, $) { renderer.addTreatmentProfiles(client); renderer.drawTreatments(client); + + // console.log('Redrawing brush due to update: ', currentBrushExtent); + + chart.theBrush.call(chart.brush.move, currentBrushExtent.map(chart.xScale2)); } chart.scroll = function scroll (nowDate) { @@ -704,21 +710,30 @@ function init (client, d3, $) { if (client.sbx.pluginBase.forecastPoints) { var shownForecastPoints = chart.getForecastData(); - var focusHoursAheadMills = chart.getMaxForecastMills(); + // Get maximum time we will allow projected forward in time + // based on the number of hours the user has selected to show. + var maxForecastMills = chart.getMaxForecastMills(); var selectedRange = chart.createBrushedRange(); var to = selectedRange[1].getTime(); - var maxForecastMills = to + times.mins(30).msecs; + // Default min forecast projection times to the default amount of time to forecast + var minForecastMills = to + client.defaultForecastTime; + var availForecastMills = 0; + + // Determine what the maximum forecast time is that is available in the forecast data if (shownForecastPoints.length > 0) { - maxForecastMills = _.max(_.map(shownForecastPoints, function(point) { return point.mills })); + availForecastMills = _.max(_.map(shownForecastPoints, function(point) { return point.mills })); } - maxForecastMills = Math.min(focusHoursAheadMills, maxForecastMills); + // Limit the amount shown to the maximum time allowed to be projected forward based + // on the number of hours the user has selected to show + var forecastMills = Math.min(availForecastMills, maxForecastMills); var lastSGVMills = client.sbx.lastSGVMills(); - client.forecastTime = ((maxForecastMills > 0) && lastSGVMills) ? maxForecastMills - lastSGVMills : client.defaultForecastTime; + // Don't allow the forecast time to go below the minimum forecast time + client.forecastTime = Math.max(forecastMills, minForecastMills); } }; diff --git a/lib/client/index.js b/lib/client/index.js index 6ea155a0b5e..b4c428ff2b5 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -136,7 +136,8 @@ client.load = function load (serverSettings, callback) { client.now = Date.now(); client.ddata = require('../data/ddata')(); - client.forecastTime = client.defaultForecastTime = times.mins(30).msecs; + client.defaultForecastTime = times.mins(30).msecs; + client.forecastTime = client.now + client.defaultForecastTime; client.entries = []; client.ticks = require('./ticks');