X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-options.js;h=815a893a145371adef0c787f05f6f5009e4654ac;hb=24cfd518a83f7ffa86dc68759641d606c60cead8;hp=7295fb7f4139364a24b8ab2bbb6c2664726cd807;hpb=ed8df44ec5bb0b1445474652d4de8f9cd52ca2cb;p=dygraphs.git diff --git a/dygraph-options.js b/dygraph-options.js index 7295fb7..815a893 100644 --- a/dygraph-options.js +++ b/dygraph-options.js @@ -9,11 +9,11 @@ /* * Interesting member variables: * dygraph_ - the graph. - * global - global attributes (common among all graphs, AIUI) + * global_ - global attributes (common among all graphs, AIUI) * user - attributes set by the user - * axes - map of options specific to the axis. - * series - { seriesName -> { idx, yAxis, options } - * labels - used as mapping from index to series name. + * axes_ - array of axis index to { series : [ series names ] , options : { axis-specific options. } + * series_ - { seriesName -> { idx, yAxis, options }} + * labels_ - used as mapping from index to series name. */ /** @@ -46,6 +46,43 @@ 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 : " + axis; + } + if (typeof(axis) == "number") { + if (axis === 0 || axis === 1) { + return axis; + } + throw "Dygraphs only supports two y-axes, indexed from 0-1."; + } + if (typeof(axis) == "object") { + throw "Using objects for axis specification " + + "is not supported inside the 'series' option."; + } + 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. @@ -55,7 +92,7 @@ var DygraphOptions = function(dygraph) { DygraphOptions.prototype.reparseSeries = function() { this.labels = this.get("labels").slice(1); - this.axes_ = [ {} ]; // Always one axis at least. + this.axes_ = [ { series : [], options : {}} ]; // Always one axis at least. this.series_ = {}; // Traditionally, per-series options were specified right up there with the options. For instance @@ -80,54 +117,73 @@ 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 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 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] = { series : [ seriesName ], options : axis }; + } - var optionsForSeries = allseries[seriesName] || {}; - var yAxis = 0; + // Associate series without axis options with axis 0. + if (!axis) { // undefined + this.axes_[0].series.push(seriesName); + } - 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; + + // 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; + } + var yAxis = this.series_[axis].yAxis; + this.series_[seriesName].yAxis = yAxis; + this.axes_[yAxis].series.push(seriesName); } - this.series_[seriesName].yAxis = this.series_[axis].yAxis; } - } + } else { + 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"]); - // 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; + this.series_[seriesName] = { + idx: idx, + yAxis: yAxis, + options : optionsForSeries }; - if (axis_opts.hasOwnProperty("y")) { - Dygraph.update(this.axes_[0], axis_opts.y); + if (!this.axes_[yAxis]) { + this.axes_[yAxis] = { series : [ seriesName ], options : {} }; + } else { + this.axes_[yAxis].series.push(seriesName); + } } + } - if (axis_opts.hasOwnProperty("y2")) { - this.axes_[1] = this.axes_[1] || {}; - Dygraph.update(this.axes_[1], axis_opts.y2); - } + // This doesn't support reading from the 'x' axis, only 'y' and 'y2. + var axis_opts = this.user_["axes"] || {}; + Dygraph.update(this.axes_[0].options, axis_opts["y"] || {}); + if (this.axes_.length > 1) { + Dygraph.update(this.axes_[1].options, axis_opts["y2"] || {}); } }; @@ -163,7 +219,7 @@ DygraphOptions.prototype.getForAxis = function(name, axis) { axisIdx = (axis == "y2") ? 1 : 0; } - var axisOptions = this.axes_[axisIdx]; + var axisOptions = this.axes_[axisIdx].options; if (axisOptions.hasOwnProperty(name)) { return axisOptions[name]; } @@ -201,3 +257,47 @@ DygraphOptions.prototype.getForSeries = function(name, series) { return this.getForAxis(name, seriesObj["yAxis"]); }; +/** + * Returns the number of y-axes on the chart. + * @return {Number} the number of axes. + */ +DygraphOptions.prototype.numAxes = function() { + return this.axes_.length; +}; + +/** + * Return the y-axis for a given series, specified by name. + */ +DygraphOptions.prototype.axisForSeries = function(seriesName) { + return this.series_[seriesName].yAxis; +}; + +/** + * Returns the options for the specified axis. + */ +DygraphOptions.prototype.axisOptions = function(yAxis) { + return this.axes_[yAxis].options; +}; + +/** + * Return the series associated with an axis. + */ +DygraphOptions.prototype.seriesForAxis = function(yAxis) { + return this.axes_[yAxis].series; +}; + +/** + * Return the list of all series, in their columnar order. + */ +DygraphOptions.prototype.seriesNames = function() { + return this.labels_; +}; + +/* Are we using this? */ +/** + * Return the index of the specified series. + * @param {string} series the series name. + */ +DygraphOptions.prototype.indexOfSeries = function(series) { + return this.series_[series].idx; +};