From: Robert Konigsberg Date: Sun, 15 Sep 2013 14:45:54 +0000 (-0400) Subject: Add setName to two of the dataHandler methods, making it possible to determine per... X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=0eb0162922c5f3bb193fed3bd1bbcf307554be78;p=dygraphs.git Add setName to two of the dataHandler methods, making it possible to determine per-series and per-axis options within the handler. --- diff --git a/auto_tests/tests/error_bars.js b/auto_tests/tests/error_bars.js index 1c3894c..d6f26ce 100644 --- a/auto_tests/tests/error_bars.js +++ b/auto_tests/tests/error_bars.js @@ -155,7 +155,7 @@ errorBarsTestCase.prototype.testRollingAveragePreservesNaNs = function() { } ); - var in_series = g.dataHandler_.extractSeries(data, 1, g.attributes_); + var in_series = g.dataHandler_.extractSeries(data, 1, 'A', g.attributes_); assertEquals(null, in_series[4][1]); assertEquals(null, in_series[4][2][0]); @@ -164,7 +164,7 @@ errorBarsTestCase.prototype.testRollingAveragePreservesNaNs = function() { assertNaN(in_series[5][2][0]); assertNaN(in_series[5][2][1]); - var out_series = g.dataHandler_.rollingAverage(in_series, 1, g.attributes_); + var out_series = g.dataHandler_.rollingAverage(in_series, 1, 'A', g.attributes_); assertNaN(out_series[5][1]); assertNaN(out_series[5][2][0]); assertNaN(out_series[5][2][1]); diff --git a/auto_tests/tests/rolling_average.js b/auto_tests/tests/rolling_average.js index 86b3e3c..1287aa1 100644 --- a/auto_tests/tests/rolling_average.js +++ b/auto_tests/tests/rolling_average.js @@ -230,7 +230,10 @@ rollingAverageTestCase.prototype.testRollFractionsBarsWilson = function() { } }; -rollingAverageTestCase.prototype.getRolledData = function(g, data, seriesIdx, rollPeriod){ +rollingAverageTestCase.prototype.getRolledData = function(g, data, seriesIdx, rollPeriod) { + // TODO(konigsberg): exposure to attributes_. var options = g.attributes_; - return g.dataHandler_.rollingAverage(g.dataHandler_.extractSeries(data, seriesIdx, options), rollPeriod, options); + var seriesName = g.getLabels()[seriesIdx]; + var interim = g.dataHandler_.extractSeries(data, seriesIdx, seriesName, options); + return g.dataHandler_.rollingAverage(interim, rollPeriod, seriesName, options); }; diff --git a/datahandler/bars-custom.js b/datahandler/bars-custom.js index 37255e5..9e60a03 100644 --- a/datahandler/bars-custom.js +++ b/datahandler/bars-custom.js @@ -19,11 +19,11 @@ var CustomBarsHandler = Dygraph.DataHandlers.CustomBarsHandler; CustomBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler(); // customBars -CustomBarsHandler.prototype.extractSeries = function(rawData, i, options) { +CustomBarsHandler.prototype.extractSeries = function(rawData, i, seriesName, options) { // TODO(danvk): pre-allocate series here. var series = []; var x, y, point; - var logScale = options.get('logscale'); + var logScale = options.getForAxis('logscale', options.axisForSeries(seriesName)); for ( var j = 0; j < rawData.length; j++) { x = rawData[j][0]; point = rawData[j][i]; @@ -49,8 +49,8 @@ CustomBarsHandler.prototype.extractSeries = function(rawData, i, options) { return series; }; -CustomBarsHandler.prototype.rollingAverage = function(originalData, rollPeriod, - options) { +CustomBarsHandler.prototype.rollingAverage = function( + originalData, rollPeriod, seriesName, options) { rollPeriod = Math.min(rollPeriod, originalData.length); var rollingData = []; var y, low, high, mid,count, i, extremes; diff --git a/datahandler/bars-error.js b/datahandler/bars-error.js index 6da2fc5..5ca60b8 100644 --- a/datahandler/bars-error.js +++ b/datahandler/bars-error.js @@ -19,12 +19,12 @@ var ErrorBarsHandler = Dygraph.DataHandlers.ErrorBarsHandler; ErrorBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler(); // errorBars -ErrorBarsHandler.prototype.extractSeries = function(rawData, i, options) { +ErrorBarsHandler.prototype.extractSeries = function(rawData, i, seriesName, options) { // TODO(danvk): pre-allocate series here. var series = []; var x, y, variance, point; - var sigma = options.get("sigma"); - var logScale = options.get('logscale'); + var sigma = options.getForSeries('sigma', seriesName); + var logScale = options.getForAxis('logscale', options.axisForSeries(seriesName)); for ( var j = 0; j < rawData.length; j++) { x = rawData[j][0]; point = rawData[j][i]; @@ -53,11 +53,11 @@ ErrorBarsHandler.prototype.extractSeries = function(rawData, i, options) { return series; }; -ErrorBarsHandler.prototype.rollingAverage = function(originalData, rollPeriod, - options) { +ErrorBarsHandler.prototype.rollingAverage = function( + originalData, rollPeriod, seriesName, options) { rollPeriod = Math.min(rollPeriod, originalData.length); var rollingData = []; - var sigma = options.get("sigma"); + var sigma = options.getForSeries('sigma', seriesName); var i, j, y, v, sum, num_ok, stddev, variance, value; diff --git a/datahandler/bars-fractions.js b/datahandler/bars-fractions.js index 28c0491..d54cac6 100644 --- a/datahandler/bars-fractions.js +++ b/datahandler/bars-fractions.js @@ -20,13 +20,13 @@ var FractionsBarsHandler = Dygraph.DataHandlers.FractionsBarsHandler; FractionsBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler(); // errorBars -FractionsBarsHandler.prototype.extractSeries = function(rawData, i, options) { +FractionsBarsHandler.prototype.extractSeries = function(rawData, i, seriesName, options) { // TODO(danvk): pre-allocate series here. var series = []; var x, y, point, num, den, value, stddev, variance; var mult = 100.0; - var sigma = options.get("sigma"); - var logScale = options.get('logscale'); + var sigma = options.getForSeries('sigma', seriesName); + var logScale = options.getForAxis('logscale', options.axisForSeries(seriesName)); for ( var j = 0; j < rawData.length; j++) { x = rawData[j][0]; point = rawData[j][i]; @@ -58,12 +58,12 @@ FractionsBarsHandler.prototype.extractSeries = function(rawData, i, options) { return series; }; -FractionsBarsHandler.prototype.rollingAverage = function(originalData, rollPeriod, - options) { +FractionsBarsHandler.prototype.rollingAverage = function( + originalData, rollPeriod, seriesName, options) { rollPeriod = Math.min(rollPeriod, originalData.length); var rollingData = []; - var sigma = options.get("sigma"); - var wilsonInterval = options.get("wilsonInterval"); + var sigma = options.getForSeries('sigma', seriesName); + var wilsonInterval = options.getForSeries('wilsonInterval', seriesName); var low, high, i, stddev; var num = 0; diff --git a/datahandler/bars.js b/datahandler/bars.js index 914ee27..192766f 100644 --- a/datahandler/bars.js +++ b/datahandler/bars.js @@ -21,7 +21,7 @@ Dygraph.DataHandlers.BarsHandler = Dygraph.DataHandler(); var BarsHandler = Dygraph.DataHandlers.BarsHandler; // errorBars -BarsHandler.prototype.extractSeries = function(rawData, i, options) { +BarsHandler.prototype.extractSeries = function(rawData, i, seriesName, options) { // Not implemented here must be extended }; @@ -41,7 +41,7 @@ BarsHandler.prototype.onPointsCreated_ = function(series, points) { } }; -BarsHandler.prototype.getExtremeYValues = function(series, dateWindow, options) { +BarsHandler.prototype.getExtremeYValues = function(series, dateWindow, seriesName, options) { var minY = null, maxY = null, y; var firstIdx = 0; diff --git a/datahandler/datahandler.js b/datahandler/datahandler.js index 5ed0112..f93cda9 100644 --- a/datahandler/datahandler.js +++ b/datahandler/datahandler.js @@ -98,12 +98,13 @@ Dygraph.DataHandler = function () { * rawData[i] = [x,ySeries1,...,ySeriesN]. * @param seriesIndex {!number} Index of the series to extract. All other series should * be ignored. + * @param {!string} seriesName Name of the series. * @param options {!DygraphOptions} Dygraph options. * @returns {Array.<[!number,?number,?]>} The series in the unified data format * where series[i] = [x,y,{extras}]. * @public */ - handler.prototype.extractSeries = function(rawData, seriesIndex, options) { + handler.prototype.extractSeries = function(rawData, seriesIndex, seriesName, options) { }; /** @@ -111,13 +112,13 @@ Dygraph.DataHandler = function () { * * @param {!Array.<[!number,?number,?]>} series The series in the unified * data format where series[i] = [x,y,{extras}]. - * @param {!string} setName Name of the series. + * @param {!string} seriesName Name of the series. * @param {!number} boundaryIdStart Index offset of the first point, equal to the * number of skipped points left of the date window minimum (if any). * @return {!Array.} List of points for this series. * @public */ - handler.prototype.seriesToPoints = function(series, setName, boundaryIdStart) { + handler.prototype.seriesToPoints = function(series, seriesName, boundaryIdStart) { // TODO(bhs): these loops are a hot-spot for high-point-count charts. In // fact, // on chrome+linux, they are 6 times more expensive than iterating through @@ -134,7 +135,7 @@ Dygraph.DataHandler = function () { y : NaN, xval : DygraphLayout.parseFloat_(item[0]), yval : yval, - name : setName, // TODO(danvk): is this really necessary? + name : seriesName, // TODO(danvk): is this really necessary? idx : i + boundaryIdStart }; points.push(point); @@ -164,12 +165,13 @@ Dygraph.DataHandler = function () { * * @param {!Array.<[!number,?number,?]>} series The series in the unified * data format where series[i] = [x,y,{extras}]. - * @param {!number} rollPeriod The number of points over which to average the data + * @param {!number} rollPeriod The number of points over which to average the data. + * @param {!String} seriesName the name of the series. * @param {!DygraphOptions} options The dygraph options. * @return the rolled series. * @public */ - handler.prototype.rollingAverage = function(series, rollPeriod, options) { + handler.prototype.rollingAverage = function(series, rollPeriod, seriesName, options) { }; /** diff --git a/datahandler/default-fractions.js b/datahandler/default-fractions.js index 8da8e42..3663ced 100644 --- a/datahandler/default-fractions.js +++ b/datahandler/default-fractions.js @@ -18,12 +18,12 @@ Dygraph.DataHandlers.DefaultFractionHandler = Dygraph.DataHandler(); var DefaultFractionHandler = Dygraph.DataHandlers.DefaultFractionHandler; DefaultFractionHandler.prototype = new Dygraph.DataHandlers.DefaultHandler(); -DefaultFractionHandler.prototype.extractSeries = function(rawData, i, options) { +DefaultFractionHandler.prototype.extractSeries = function(rawData, i, seriesName, options) { // TODO(danvk): pre-allocate series here. var series = []; var x, y, point, num, den, value; var mult = 100.0; - var logScale = options.get('logscale'); + var logScale = options.getForAxis('logscale', options.axisForSeries(seriesName)); for ( var j = 0; j < rawData.length; j++) { x = rawData[j][0]; point = rawData[j][i]; @@ -53,8 +53,9 @@ DefaultFractionHandler.prototype.extractSeries = function(rawData, i, options) { return series; }; -DefaultFractionHandler.prototype.rollingAverage = function(originalData, rollPeriod, - options) { +DefaultFractionHandler.prototype.rollingAverage = function( + originalData, rollPeriod, seriesName, options) { + rollPeriod = Math.min(rollPeriod, originalData.length); var rollingData = []; diff --git a/datahandler/default.js b/datahandler/default.js index 6e6697f..741d523 100644 --- a/datahandler/default.js +++ b/datahandler/default.js @@ -17,10 +17,10 @@ Dygraph.DataHandlers.DefaultHandler = Dygraph.DataHandler(); var DefaultHandler = Dygraph.DataHandlers.DefaultHandler; -DefaultHandler.prototype.extractSeries = function(rawData, i, options) { +DefaultHandler.prototype.extractSeries = function(rawData, i, seriesName, options) { // TODO(danvk): pre-allocate series here. var series = []; - var logScale = options.get('logscale'); + var logScale = options.getForAxis('logscale', options.axisForSeries(seriesName)); for ( var j = 0; j < rawData.length; j++) { var x = rawData[j][0]; var point = rawData[j][i]; @@ -36,8 +36,8 @@ DefaultHandler.prototype.extractSeries = function(rawData, i, options) { return series; }; -DefaultHandler.prototype.rollingAverage = function(originalData, rollPeriod, - options) { +DefaultHandler.prototype.rollingAverage = function( + originalData, rollPeriod, seriesName, options) { rollPeriod = Math.min(rollPeriod, originalData.length); var rollingData = []; diff --git a/dygraph.js b/dygraph.js index cee63e8..501c917 100644 --- a/dygraph.js +++ b/dygraph.js @@ -509,6 +509,7 @@ Dygraph.prototype.__init__ = function(div, file, attrs) { this.registeredEvents_ = []; this.eventListeners_ = {}; + // TODO(konigsberg): Rename attributes_ to options_. this.attributes_ = new DygraphOptions(this); // Create the containing DIV and other interactive elements @@ -2227,11 +2228,11 @@ Dygraph.prototype.predraw_ = function() { // Convert the raw data (a 2D array) into the internal format and compute // rolling averages. this.rolledSeries_ = [null]; // x-axis is the first series and it's special - for (var i = 1; i < this.numColumns(); i++) { - // var logScale = this.attr_('logscale', i); // TODO(klausw): this looks wrong // konigsberg thinks so too. - var series = this.dataHandler_.extractSeries(this.rawData_, i, this.attributes_); + for (var seriesIdx = 1; seriesIdx < this.numColumns(); seriesIdx++) { + var seriesName = this.attr_('labels')[seriesIdx]; + var series = this.dataHandler_.extractSeries(this.rawData_, seriesIdx, seriesName, this.attributes_); if (this.rollPeriod_ > 1) { - series = this.dataHandler_.rollingAverage(series, this.rollPeriod_, this.attributes_); + series = this.dataHandler_.rollingAverage(series, this.rollPeriod_, seriesName, this.attributes_); } this.rolledSeries_.push(series); @@ -2451,7 +2452,7 @@ Dygraph.prototype.gatherDatasets_ = function(rolledSeries, dateWindow) { var seriesName = this.attr_("labels")[seriesIdx]; var seriesExtremes = this.dataHandler_.getExtremeYValues(series, - dateWindow, this.attr_("stepPlot",seriesName)); + dateWindow, this.attr_("stepPlot", seriesName)); var seriesPoints = this.dataHandler_.seriesToPoints(series, seriesName, boundaryIds[seriesIdx-1][0]);