X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-options.js;h=815a893a145371adef0c787f05f6f5009e4654ac;hb=c97b87c282caf695bc7dea62a4668e491b165ad7;hp=9a4f6bf1962e4b6048a3f613a0ee1c5ff852c641;hpb=5daa462d93e850a64a7f6644afb8122336ccf84d;p=dygraphs.git diff --git a/dygraph-options.js b/dygraph-options.js index 9a4f6bf..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,55 +92,98 @@ 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_ = {}; - 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]; + // Traditionally, per-series options were specified right up there with the options. For instance + // { + // labels: [ "X", "foo", "bar" ], + // pointSize: 3, + // foo : {}, // options for foo + // bar : {} // options for bar + // } + // + // Moving forward, series really should be specified in the series element, separating them. + // like so: + // + // { + // labels: [ "X", "foo", "bar" ], + // pointSize: 3, + // series : { + // foo : {}, // options for foo + // bar : {} // options for bar + // } + // } + // + // So, if series is found, it's expected to contain per-series data, otherwise we fall + // back. + 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 = this.user_[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_.hasOwnProperty("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"] || {}); } }; @@ -139,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]; } @@ -177,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; +};