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.
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] = { series : [ seriesName ], options : axis };
+ this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries };
}
-
- // Associate series without axis options with axis 0.
- if (!axis) { // undefined
- this.axes_[0].series.push(seriesName);
+
+ // 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);
+ }
}
+ } 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.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"];
+ this.series_[seriesName] = {
+ idx: idx,
+ yAxis: yAxis,
+ options : optionsForSeries };
- 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;
+ if (!this.axes_[yAxis]) {
+ this.axes_[yAxis] = { series : [ seriesName ], options : {} };
+ } else {
+ this.axes_[yAxis].series.push(seriesName);
}
- var yAxis = this.series_[axis].yAxis;
- this.series_[seriesName].yAxis = yAxis;
- this.axes_[yAxis].series.push(seriesName);
}
}
// 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;
-
- if (axis_opts.hasOwnProperty("y")) {
- Dygraph.update(this.axes_[0].options, axis_opts.y);
- }
-
- if (axis_opts.hasOwnProperty("y2")) {
- this.axes_[1] = this.axes_[1] || {}; // FIX
- Dygraph.update(this.axes_[1].options, axis_opts.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"] || {});
}
};
* @param {String} name the name of the option.
*/
DygraphOptions.prototype.get = function(name) {
+ var result = this.getGlobalUser_(name);
+ if (result != null) {
+ return result;
+ }
+ return this.getGlobalDefault_(name);
+};
+
+DygraphOptions.prototype.getGlobalUser_ = function(name) {
if (this.user_.hasOwnProperty(name)) {
return this.user_[name];
}
+ return null;
+};
+
+DygraphOptions.prototype.getGlobalDefault_ = function(name) {
if (this.global_.hasOwnProperty(name)) {
return this.global_[name];
}
+ if (Dygraph.DEFAULT_ATTRS.hasOwnProperty(name)) {
+ return Dygraph.DEFAULT_ATTRS[name];
+ }
return null;
-};
+}
/**
* Get a value for a specific axis. If there is no specific value for the axis,
// TODO(konigsberg): Accept only valid axis strings?
axisIdx = (axis == "y2") ? 1 : 0;
}
+ // Search the user-specified axis option first.
+ if (this.axes_[axisIdx]) {
+ var axisOptions = this.axes_[axisIdx].options;
+ if (axisOptions.hasOwnProperty(name)) {
+ return axisOptions[name];
+ }
+ }
- var axisOptions = this.axes_[axisIdx].options;
- if (axisOptions.hasOwnProperty(name)) {
- return axisOptions[name];
+ // User-specified global options second.
+ var result = this.getGlobalUser_(name);
+ if (result != null) {
+ return result;
}
- return this.get(name);
+
+ // Default axis options third.
+ var axisString = axis == 0 ? "y" : "y2";
+ var defaultAxisOptions = Dygraph.DEFAULT_ATTRS.axes[axisString];
+ if (defaultAxisOptions.hasOwnProperty(name)) {
+ return defaultAxisOptions[name];
+ }
+
+ // Default global options last.
+ return this.getGlobalDefault_(name);
};
/**
*/
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;
+};