From: Robert Konigsberg Date: Sat, 24 Nov 2012 18:48:13 +0000 (-0500) Subject: Start working on new-version axis support. Tests still fail. X-Git-Tag: v1.0.0~157^2~4 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=632bd78ca394e97157f2e09e0f10a05f813a810b;p=dygraphs.git Start working on new-version axis support. Tests still fail. --- diff --git a/auto_tests/tests/per_series.js b/auto_tests/tests/per_series.js index a198a95..bba18ad 100644 --- a/auto_tests/tests/per_series.js +++ b/auto_tests/tests/per_series.js @@ -12,7 +12,6 @@ perSeriesTestCase.prototype.setUp = function() { perSeriesTestCase.prototype.tearDown = function() { }; - perSeriesTestCase.prototype.testPerSeriesFill = function() { var opts = { width: 480, @@ -101,3 +100,43 @@ perSeriesTestCase.prototype.testNewStyleSeriesTrumpsOldStyle = function() { assertEquals(5, g.getOption("pointSize", "Y")); }; +perSeriesTestCase.prototype.testAxisInNewSeries = function() { + var opts = { + logscale: true, + series : { + D : { axis : 'y2' }, + C : { axis : 1 }, + B : { axis : 0 }, + E : { axis : 'y' } + } + }; + var graph = document.getElementById("graph"); + var data = "X,A,B,C,D,E\n0,1,2,3,4,5\n"; + g = new Dygraph(graph, data, opts); + + assertEquals(5, g.getOption("pointSize")); + assertEquals(4, g.getOption("pointSize", "Y")); + assertEquals(5, g.getOption("pointSize", "Z")); +}; + +perSeriesTestCase.prototype.testAxisInNewSeries_withAxes = function() { + var opts = { + series : { + D : { axis : 'y2' }, + C : { axis : 1 }, + B : { axis : 0 }, + E : { axis : 'y' } + }, + axes : { + y : {}, + y2 : {} + } + }; + var graph = document.getElementById("graph"); + var data = "X,A,B,C,D,E\n0,1,2,3,4,5\n"; + g = new Dygraph(graph, data, opts); + + assertEquals(5, g.getOption("pointSize")); + assertEquals(4, g.getOption("pointSize", "Y")); + assertEquals(5, g.getOption("pointSize", "Z")); +}; diff --git a/dygraph-options.js b/dygraph-options.js index 7295fb7..82daff6 100644 --- a/dygraph-options.js +++ b/dygraph-options.js @@ -46,6 +46,39 @@ var DygraphOptions = function(dygraph) { this.reparseSeries(); }; +/* + * Not optimal, but does the trick when you're only using two axes. + * If we move to more axes, this can just become a function. + */ +DygraphOptions.AXIS_STRING_MAPPINGS_ = { + 'y' : 0, + 'Y' : 0, + 'y1' : 0, + 'Y1' : 0, + 'y2' : 1, + 'Y2' : 1 +} + +DygraphOptions.axisToIndex_ = function(axis) { + if (typeof(axis) == "string") { + if (DygraphOptions.AXIS_STRING_MAPPINGS_.hasOwnProperty(axis)) { + return DygraphOptions.AXIS_STRING_MAPPINGS_[axis]; + } + throw "Unknown axis : " + text; + } + if (typeof(axis) == "number") { + if (axis == 0 || axis == 1) { + return axis; + } + throw "Dygraphs only supports two y-axes, indexed from 0-1." + } + if (axis) { + throw "Unknown axis : " + axis; + } + // No axis specification means axis 0. + return 0; +}; + /** * Reparses options that are all related to series. This typically occurs when * options are either updated, or source data has been made avaialble. @@ -80,43 +113,63 @@ DygraphOptions.prototype.reparseSeries = function() { // // So, if series is found, it's expected to contain per-series data, otherwise we fall // back. - var allseries = this.user_["series"] ? this.user_.series : this.user_; + var oldStyleSeries = !this.user_["series"]; + + if (oldStyleSeries) { + var axisId = 0; // 0-offset; there's always one. + // Go through once, add all the series, and for those with {} axis options, add a new axis. + for (var idx = 0; idx < this.labels.length; idx++) { + var seriesName = this.labels[idx]; + + var optionsForSeries = this.user_[seriesName] || {}; + + var yAxis = 0; + var axis = optionsForSeries["axis"]; + if (typeof(axis) == 'object') { + yAxis = ++axisId; + this.axes_[yAxis] = axis; + } + this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; + } + + // Go through one more time and assign series to an axis defined by another + // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } + for (var idx = 0; idx < this.labels.length; idx++) { + var seriesName = this.labels[idx]; + var optionsForSeries = this.series_[seriesName]["options"]; + var axis = optionsForSeries["axis"]; + + if (typeof(axis) == 'string') { + if (!this.series_.hasOwnProperty(axis)) { + this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " + + "series " + axis + ", which does not define its own axis."); + return null; + } + this.series_[seriesName].yAxis = this.series_[axis].yAxis; + } + } + } else { + var maxYAxis = 0; - var axisId = 0; // 0-offset; there's always one. - // Go through once, add all the series, and for those with {} axis options, add a new axis. - for (var idx = 0; idx < this.labels.length; idx++) { - var seriesName = this.labels[idx]; + for (var idx = 0; idx < this.labels.length; idx++) { + var seriesName = this.labels[idx]; + var optionsForSeries = this.user_.series[seriesName] || {}; + var yAxis = DygraphOptions.axisToIndex_(optionsForSeries["axis"]); - var optionsForSeries = allseries[seriesName] || {}; - var yAxis = 0; + maxYAxis = Math.max(yAxis, maxYAxis); - var axis = optionsForSeries["axis"]; - if (typeof(axis) == 'object') { - yAxis = ++axisId; - this.axes_[yAxis] = axis; + this.series_[seriesName] = { + idx: idx, + yAxis: yAxis, + options : optionsForSeries }; } - this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; - } - // Go through one more time and assign series to an axis defined by another - // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } - for (var idx = 0; idx < this.labels.length; idx++) { - var seriesName = this.labels[idx]; - var optionsForSeries = this.series_[seriesName]["options"]; - var axis = optionsForSeries["axis"]; - - if (typeof(axis) == 'string') { - if (!this.series_.hasOwnProperty(axis)) { - this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " + - "series " + axis + ", which does not define its own axis."); - return null; - } - this.series_[seriesName].yAxis = this.series_[axis].yAxis; + for (; maxYAxis >= 0; maxYAxis--) { + this.axes_[maxYAxis] = {}; } } // This doesn't support reading from the 'x' axis, only 'y' and 'y2. - // Read from the global "axes" option. if (this.user_["axes"]) { var axis_opts = this.user_.axes; @@ -200,4 +253,3 @@ DygraphOptions.prototype.getForSeries = function(name, series) { return this.getForAxis(name, seriesObj["yAxis"]); }; -