From a258aa406d2ba95653befdae2044c8e605ece92b Mon Sep 17 00:00:00 2001 From: Graeme Date: Tue, 25 Nov 2014 18:30:23 -0500 Subject: [PATCH 1/2] Unify [max|min]ValueScale and consider range as the base --- smoothie.js | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/smoothie.js b/smoothie.js index f47bb6b..90aba29 100644 --- a/smoothie.js +++ b/smoothie.js @@ -490,33 +490,33 @@ SmoothieChart.prototype.updateValueRange = function() { // Calculate the current scale of the chart, from all time series. var chartOptions = this.options, - chartMaxValue = Number.NaN, - chartMinValue = Number.NaN; - - for (var d = 0; d < this.seriesSet.length; d++) { - // TODO(ndunn): We could calculate / track these values as they stream in. - var timeSeries = this.seriesSet[d].timeSeries; - if (!isNaN(timeSeries.maxValue)) { - chartMaxValue = !isNaN(chartMaxValue) ? Math.max(chartMaxValue, timeSeries.maxValue) : timeSeries.maxValue; - } + chartMaxValue = chartOptions.maxValue, + chartMinValue = chartOptions.minValue; + + if (isNaN(chartMinValue + chartMaxValue)) { + for (var d = 0; d < this.seriesSet.length; d++) { + // TODO(ndunn): We could calculate / track these values as they stream in. + var timeSeries = this.seriesSet[d].timeSeries; + if (!isNaN(timeSeries.maxValue)) { + chartMaxValue = chartMaxValue > timeSeries.maxValue ? chartMaxValue : timeSeries.maxValue; + } - if (!isNaN(timeSeries.minValue)) { - chartMinValue = !isNaN(chartMinValue) ? Math.min(chartMinValue, timeSeries.minValue) : timeSeries.minValue; + if (!isNaN(timeSeries.minValue)) { + chartMinValue = chartMinValue > timeSeries.minValue ? chartMinValue : timeSeries.minValue; + } } } + var range = chartMaxValue - chartMinValue; + // Scale the chartMaxValue to add padding at the top if required - if (chartOptions.maxValue != null) { - chartMaxValue = chartOptions.maxValue; - } else { - chartMaxValue *= chartOptions.maxValueScale; + if (chartOptions.maxValue == null) { + chartMaxValue += range * chartOptions.maxValueScale - range; } // Set the minimum if we've specified one - if (chartOptions.minValue != null) { - chartMinValue = chartOptions.minValue; - } else { - chartMinValue -= Math.abs(chartMinValue * chartOptions.minValueScale - chartMinValue); + if (chartOptions.minValue == null) { + chartMinValue -= range * chartOptions.minValueScale - range; } // If a custom range function is set, call it @@ -527,8 +527,7 @@ } if (!isNaN(chartMaxValue) && !isNaN(chartMinValue)) { - var targetValueRange = chartMaxValue - chartMinValue; - var valueRangeDiff = (targetValueRange - this.currentValueRange); + var valueRangeDiff = (chartMaxValue - chartMinValue) - this.currentValueRange; var minValueDiff = (chartMinValue - this.currentVisMinValue); this.isAnimatingScale = Math.abs(valueRangeDiff) > 0.1 || Math.abs(minValueDiff) > 0.1; this.currentValueRange += chartOptions.scaleSmoothing * valueRangeDiff; From b9279106b09eb5faea2729f7d519b2ef0a419fc7 Mon Sep 17 00:00:00 2001 From: Graeme Date: Tue, 25 Nov 2014 19:36:51 -0500 Subject: [PATCH 2/2] Force [min|max]Value to chartOptions value if provided --- smoothie.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smoothie.js b/smoothie.js index 90aba29..eb1947b 100644 --- a/smoothie.js +++ b/smoothie.js @@ -497,11 +497,11 @@ for (var d = 0; d < this.seriesSet.length; d++) { // TODO(ndunn): We could calculate / track these values as they stream in. var timeSeries = this.seriesSet[d].timeSeries; - if (!isNaN(timeSeries.maxValue)) { + if (chartOptions.maxValue == null && !isNaN(timeSeries.maxValue)) { chartMaxValue = chartMaxValue > timeSeries.maxValue ? chartMaxValue : timeSeries.maxValue; } - if (!isNaN(timeSeries.minValue)) { + if (chartOptions.minValue == null && !isNaN(timeSeries.minValue)) { chartMinValue = chartMinValue > timeSeries.minValue ? chartMinValue : timeSeries.minValue; } }