2 * @fileoverview DygraphOptions is responsible for parsing and returning information about options.
4 * Still tightly coupled to Dygraphs, we could remove some of that, you know.
10 * Interesting member variables:
11 * dygraph_ - the graph.
12 * global_ - global attributes (common among all graphs, AIUI)
13 * user - attributes set by the user
14 * yAxes_ - array of axis index to { series : [ series names ] , options : { axis-specific options. }
15 * xAxis_ - { options : { axis-specific options. }
16 * series_ - { seriesName -> { idx, yAxis, options }}
17 * labels_ - used as mapping from index to series name.
21 * This parses attributes into an object that can be easily queried.
23 * It doesn't necessarily mean that all options are available, specifically
24 * if labels are not yet available, since those drive details of the per-series
25 * and per-axis options.
27 * @param {Dyraph} dygraph The chart to which these options belong.
30 var DygraphOptions
= function(dygraph
) {
31 this.dygraph_
= dygraph
;
36 // Once these two objects are initialized, you can call get();
37 this.global_
= this.dygraph_
.attrs_
;
38 this.user_
= this.dygraph_
.user_attrs_
|| {};
40 this.highlightSeries_
= this.get("highlightSeriesOpts") || {};
41 // Get a list of series names.
43 var labels
= this.get("labels");
45 return; // -- can't do more for now, will parse after getting the labels.
52 * Not optimal, but does the trick when you're only using two axes.
53 * If we move to more axes, this can just become a function.
55 DygraphOptions
.AXIS_STRING_MAPPINGS_
= {
64 DygraphOptions
.axisToIndex_
= function(axis
) {
65 if (typeof(axis
) == "string") {
66 if (DygraphOptions
.AXIS_STRING_MAPPINGS_
.hasOwnProperty(axis
)) {
67 return DygraphOptions
.AXIS_STRING_MAPPINGS_
[axis
];
69 throw "Unknown axis : " + axis
;
71 if (typeof(axis
) == "number") {
72 if (axis
=== 0 || axis
=== 1) {
75 throw "Dygraphs only supports two y-axes, indexed from 0-1.";
77 if (typeof(axis
) == "object") {
78 throw "Using objects for axis specification "
79 + "is not supported inside the 'series' option.";
82 throw "Unknown axis : " + axis
;
84 // No axis specification means axis 0.
89 * Reparses options that are all related to series. This typically occurs when
90 * options are either updated, or source data has been made avaialble.
92 * TODO(konigsberg): The method name is kind of weak; fix.
94 DygraphOptions
.prototype.reparseSeries
= function() {
95 this.labels
= this.get("labels").slice(1);
97 this.yAxes_
= [ { series
: [], options
: {}} ]; // Always one axis at least.
98 this.xAxis_
= { options
: {} };
101 // Traditionally, per-series options were specified right up there with the options. For instance
103 // labels: [ "X", "foo", "bar" ],
105 // foo : {}, // options
for foo
106 // bar : {} // options
for bar
109 // Moving forward, series really should be specified in the series element, separating them.
113 // labels: [ "X", "foo", "bar" ],
116 // foo : {}, // options
for foo
117 // bar : {} // options
for bar
121 // So, if series is found, it's expected to contain per-series data, otherwise we fall
123 var oldStyleSeries
= !this.user_
["series"];
125 if (oldStyleSeries
) {
126 var axisId
= 0; // 0-offset; there's always one.
127 // Go through once, add all the series, and for those with {} axis options, add a new axis.
128 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
129 var seriesName
= this.labels
[idx
];
131 var optionsForSeries
= this.user_
[seriesName
] || {};
134 var axis
= optionsForSeries
["axis"];
135 if (typeof(axis
) == 'object') {
137 this.yAxes_
[yAxis
] = { series
: [ seriesName
], options
: axis
};
140 // Associate series without axis options with axis 0.
141 if (!axis
) { // undefined
142 this.yAxes_
[0].series
.push(seriesName
);
145 this.series_
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
148 // Go through one more time and assign series to an axis defined by another
149 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
150 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
151 var seriesName
= this.labels
[idx
];
152 var optionsForSeries
= this.series_
[seriesName
]["options"];
153 var axis
= optionsForSeries
["axis"];
155 if (typeof(axis
) == 'string') {
156 if (!this.series_
.hasOwnProperty(axis
)) {
157 this.dygraph_
.error("Series " + seriesName
+ " wants to share a y-axis with " +
158 "series " + axis
+ ", which does not define its own axis.");
161 var yAxis
= this.series_
[axis
].yAxis
;
162 this.series_
[seriesName
].yAxis
= yAxis
;
163 this.yAxes_
[yAxis
].series
.push(seriesName
);
167 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
168 var seriesName
= this.labels
[idx
];
169 var optionsForSeries
= this.user_
.series
[seriesName
] || {};
170 var yAxis
= DygraphOptions
.axisToIndex_(optionsForSeries
["axis"]);
172 this.series_
[seriesName
] = {
175 options
: optionsForSeries
};
177 if (!this.yAxes_
[yAxis
]) {
178 this.yAxes_
[yAxis
] = { series
: [ seriesName
], options
: {} };
180 this.yAxes_
[yAxis
].series
.push(seriesName
);
185 var axis_opts
= this.user_
["axes"] || {};
186 Dygraph
.update(this.yAxes_
[0].options
, axis_opts
["y"] || {});
187 if (this.yAxes_
.length
> 1) {
188 Dygraph
.update(this.yAxes_
[1].options
, axis_opts
["y2"] || {});
190 Dygraph
.update(this.xAxis_
.options
, axis_opts
["x"] || {});
194 * Get a global value.
196 * @param {String} name the name of the option.
198 DygraphOptions
.prototype.get
= function(name
) {
199 var result
= this.getGlobalUser_(name
);
200 if (result
!= null) {
203 return this.getGlobalDefault_(name
);
206 DygraphOptions
.prototype.getGlobalUser_
= function(name
) {
207 if (this.user_
.hasOwnProperty(name
)) {
208 return this.user_
[name
];
213 DygraphOptions
.prototype.getGlobalDefault_
= function(name
) {
214 if (this.global_
.hasOwnProperty(name
)) {
215 return this.global_
[name
];
217 if (Dygraph
.DEFAULT_ATTRS
.hasOwnProperty(name
)) {
218 return Dygraph
.DEFAULT_ATTRS
[name
];
224 * Get a value for a specific axis. If there is no specific value for the axis,
225 * the global value is returned.
227 * @param {String} name the name of the option.
228 * @param {String|number} axis the axis to search. Can be the string representation
229 * ("x", "y", "y2") or the y-axis number (0, 1). (x-axis can't be specified by number.)
231 DygraphOptions
.prototype.getForAxis
= function(name
, axis
) {
235 // Since axis can be a number or a string, straighten everything out here.
236 if (typeof(axis
) == 'number') {
238 axisString
= axisIdx
== 0 ? "y" : "y2";
240 if (axis
== "y1") { axis
= "y"; } // Standardize on 'y'. Is this bad? I think so.
243 } else if (axis
== "y2") {
245 } else if (axis
== "x") {
246 axisIdx
= -1; // simply a placeholder for below.
248 throw "Unknown axis " + axis
;
253 var userAxis
= (axisIdx
== -1) ? this.xAxis_
: this.yAxes_
[axisIdx
];
255 // Search the user-specified axis option first.
256 if (userAxis
) { // This condition could be removed if we always set up this.yAxes_ for y2.
257 var axisOptions
= userAxis
.options
;
258 if (axisOptions
.hasOwnProperty(name
)) {
259 return axisOptions
[name
];
263 // User-specified global options second.
264 var result
= this.getGlobalUser_(name
);
265 if (result
!= null) {
269 // Default axis options third.
270 var defaultAxisOptions
= Dygraph
.DEFAULT_ATTRS
.axes
[axisString
];
271 if (defaultAxisOptions
.hasOwnProperty(name
)) {
272 return defaultAxisOptions
[name
];
275 // Default global options last.
276 return this.getGlobalDefault_(name
);
280 * Get a value for a specific series. If there is no specific value for the series,
281 * the value for the axis is returned (and afterwards, the global value.)
283 * @param {String} name the name of the option.
284 * @param {String|number} series the series to search. Can be the string representation
285 * or 0-offset series number.
287 DygraphOptions
.prototype.getForSeries
= function(name
, series
) {
288 // Honors indexes as series.
289 var seriesName
= (typeof(series
) == "number") ? this.labels
[series
] : series
;
291 if (seriesName
=== this.dygraph_
.highlightSet_
) {
292 if (this.highlightSeries_
.hasOwnProperty(name
)) {
293 return this.highlightSeries_
[name
];
297 if (!this.series_
.hasOwnProperty(seriesName
)) {
298 throw "Unknown series: " + series
;
301 var seriesObj
= this.series_
[seriesName
];
302 var seriesOptions
= seriesObj
["options"];
303 if (seriesOptions
.hasOwnProperty(name
)) {
304 return seriesOptions
[name
];
307 return this.getForAxis(name
, seriesObj
["yAxis"]);
311 * Returns the number of y-axes on the chart.
312 * @return {Number} the number of axes.
314 DygraphOptions
.prototype.numAxes
= function() {
315 return this.yAxes_
.length
;
319 * Return the y-axis for a given series, specified by name.
321 DygraphOptions
.prototype.axisForSeries
= function(seriesName
) {
322 return this.series_
[seriesName
].yAxis
;
326 * Returns the options for the specified axis.
328 // TODO(konigsberg): this is y-axis specific. Support the x axis.
329 DygraphOptions
.prototype.axisOptions
= function(yAxis
) {
330 return this.yAxes_
[yAxis
].options
;
334 * Return the series associated with an axis.
336 DygraphOptions
.prototype.seriesForAxis
= function(yAxis
) {
337 return this.yAxes_
[yAxis
].series
;
341 * Return the list of all series, in their columnar order.
343 DygraphOptions
.prototype.seriesNames
= function() {
347 /* Are we using this? */
349 * Return the index of the specified series.
350 * @param {string} series the series name.
352 DygraphOptions
.prototype.indexOfSeries
= function(series
) {
353 return this.series_
[series
].idx
;