3 * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview DygraphOptions is responsible for parsing and returning information about options.
10 * Still tightly coupled to Dygraphs, we could remove some of that, you know.
13 var DygraphOptions
= (function() {
16 /*global Dygraph:false */
20 * Interesting member variables: (REMOVING THIS LIST AS I CLOSURIZE)
21 * global_ - global attributes (common among all graphs, AIUI)
22 * user - attributes set by the user
23 * series_ - { seriesName -> { idx, yAxis, options }}
27 * This parses attributes into an object that can be easily queried.
29 * It doesn't necessarily mean that all options are available, specifically
30 * if labels are not yet available, since those drive details of the per-series
31 * and per-axis options.
33 * @param {Dygraph} dygraph The chart to which these options belong.
36 var DygraphOptions
= function(dygraph
) {
41 this.dygraph_
= dygraph
;
44 * Array of axis index to { series : [ series names ] , options : { axis-specific options. }
45 * @type {Array.<{series : Array.<string>, options : Object}>} @private
50 * { options : { axis-specific options. } }
51 * @type {Object} @private
56 // Once these two objects are initialized, you can call get();
57 this.global_
= this.dygraph_
.attrs_
;
58 this.user_
= this.dygraph_
.user_attrs_
|| {};
61 * A list of series in columnar order.
62 * @type {Array.<string>}
66 this.highlightSeries_
= this.get("highlightSeriesOpts") || {};
71 * Not optimal, but does the trick when you're only using two axes.
72 * If we move to more axes, this can just become a function.
74 * @type {Object.<string, number>}
77 DygraphOptions
.AXIS_STRING_MAPPINGS_
= {
87 * @param {string|number} axis
90 DygraphOptions
.axisToIndex_
= function(axis
) {
91 if (typeof(axis
) == "string") {
92 if (DygraphOptions
.AXIS_STRING_MAPPINGS_
.hasOwnProperty(axis
)) {
93 return DygraphOptions
.AXIS_STRING_MAPPINGS_
[axis
];
95 throw "Unknown axis : " + axis
;
97 if (typeof(axis
) == "number") {
98 if (axis
=== 0 || axis
=== 1) {
101 throw "Dygraphs only supports two y-axes, indexed from 0-1.";
104 throw "Unknown axis : " + axis
;
106 // No axis specification means axis 0.
111 * Reparses options that are all related to series. This typically occurs when
112 * options are either updated, or source data has been made available.
114 * TODO(konigsberg): The method name is kind of weak; fix.
116 DygraphOptions
.prototype.reparseSeries
= function() {
117 var labels
= this.get("labels");
119 return; // -- can't do more for now, will parse after getting the labels.
122 this.labels_
= labels
.slice(1);
124 this.yAxes_
= [ { series
: [], options
: {}} ]; // Always one axis at least.
125 this.xAxis_
= { options
: {} };
128 // Traditionally, per-series options were specified right up there with the options. For instance
130 // labels: [ "X", "foo", "bar" ],
132 // foo : {}, // options
for foo
133 // bar : {} // options
for bar
136 // Moving forward, series really should be specified in the series element, separating them.
140 // labels: [ "X", "foo", "bar" ],
143 // foo : {}, // options
for foo
144 // bar : {} // options
for bar
148 // So, if series is found, it's expected to contain per-series data, otherwise we fall
150 var oldStyleSeries
= !this.user_
["series"];
152 if (oldStyleSeries
) {
153 var axisId
= 0; // 0-offset; there's always one.
154 // Go through once, add all the series, and for those with {} axis options, add a new axis.
155 for (var idx
= 0; idx
< this.labels_
.length
; idx
++) {
156 var seriesName
= this.labels_
[idx
];
158 var optionsForSeries
= this.user_
[seriesName
] || {};
161 var axis
= optionsForSeries
["axis"];
162 if (typeof(axis
) == 'object') {
164 this.yAxes_
[yAxis
] = { series
: [ seriesName
], options
: axis
};
167 // Associate series without axis options with axis 0.
168 if (!axis
) { // undefined
169 this.yAxes_
[0].series
.push(seriesName
);
172 this.series_
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
175 // Go through one more time and assign series to an axis defined by another
176 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
177 for (var idx
= 0; idx
< this.labels_
.length
; idx
++) {
178 var seriesName
= this.labels_
[idx
];
179 var optionsForSeries
= this.series_
[seriesName
]["options"];
180 var axis
= optionsForSeries
["axis"];
182 if (typeof(axis
) == 'string') {
183 if (!this.series_
.hasOwnProperty(axis
)) {
184 Dygraph
.error("Series " + seriesName
+ " wants to share a y-axis with " +
185 "series " + axis
+ ", which does not define its own axis.");
188 var yAxis
= this.series_
[axis
].yAxis
;
189 this.series_
[seriesName
].yAxis
= yAxis
;
190 this.yAxes_
[yAxis
].series
.push(seriesName
);
194 for (var idx
= 0; idx
< this.labels_
.length
; idx
++) {
195 var seriesName
= this.labels_
[idx
];
196 var optionsForSeries
= this.user_
.series
[seriesName
] || {};
197 var yAxis
= DygraphOptions
.axisToIndex_(optionsForSeries
["axis"]);
199 this.series_
[seriesName
] = {
202 options
: optionsForSeries
};
204 if (!this.yAxes_
[yAxis
]) {
205 this.yAxes_
[yAxis
] = { series
: [ seriesName
], options
: {} };
207 this.yAxes_
[yAxis
].series
.push(seriesName
);
212 var axis_opts
= this.user_
["axes"] || {};
213 Dygraph
.update(this.yAxes_
[0].options
, axis_opts
["y"] || {});
214 if (this.yAxes_
.length
> 1) {
215 Dygraph
.update(this.yAxes_
[1].options
, axis_opts
["y2"] || {});
217 Dygraph
.update(this.xAxis_
.options
, axis_opts
["x"] || {});
221 * Get a global value.
223 * @param {string} name the name of the option.
225 DygraphOptions
.prototype.get
= function(name
) {
226 var result
= this.getGlobalUser_(name
);
227 if (result
!== null) {
230 return this.getGlobalDefault_(name
);
233 DygraphOptions
.prototype.getGlobalUser_
= function(name
) {
234 if (this.user_
.hasOwnProperty(name
)) {
235 return this.user_
[name
];
240 DygraphOptions
.prototype.getGlobalDefault_
= function(name
) {
241 if (this.global_
.hasOwnProperty(name
)) {
242 return this.global_
[name
];
244 if (Dygraph
.DEFAULT_ATTRS
.hasOwnProperty(name
)) {
245 return Dygraph
.DEFAULT_ATTRS
[name
];
251 * Get a value for a specific axis. If there is no specific value for the axis,
252 * the global value is returned.
254 * @param {string} name the name of the option.
255 * @param {string|number} axis the axis to search. Can be the string representation
256 * ("y", "y2") or the axis number (0, 1).
258 DygraphOptions
.prototype.getForAxis
= function(name
, axis
) {
262 // Since axis can be a number or a string, straighten everything out here.
263 if (typeof(axis
) == 'number') {
265 axisString
= axisIdx
=== 0 ? "y" : "y2";
267 if (axis
== "y1") { axis
= "y"; } // Standardize on 'y'. Is this bad? I think so.
270 } else if (axis
== "y2") {
272 } else if (axis
== "x") {
273 axisIdx
= -1; // simply a placeholder for below.
275 throw "Unknown axis " + axis
;
280 var userAxis
= (axisIdx
== -1) ? this.xAxis_
: this.yAxes_
[axisIdx
];
282 // Search the user-specified axis option first.
283 if (userAxis
) { // This condition could be removed if we always set up this.yAxes_ for y2.
284 var axisOptions
= userAxis
.options
;
285 if (axisOptions
.hasOwnProperty(name
)) {
286 return axisOptions
[name
];
290 // User-specified global options second.
291 var result
= this.getGlobalUser_(name
);
292 if (result
!== null) {
296 // Default axis options third.
297 var defaultAxisOptions
= Dygraph
.DEFAULT_ATTRS
.axes
[axisString
];
298 if (defaultAxisOptions
.hasOwnProperty(name
)) {
299 return defaultAxisOptions
[name
];
302 // Default global options last.
303 return this.getGlobalDefault_(name
);
307 * Get a value for a specific series. If there is no specific value for the series,
308 * the value for the axis is returned (and afterwards, the global value.)
310 * @param {string} name the name of the option.
311 * @param {string} series the series to search.
313 DygraphOptions
.prototype.getForSeries
= function(name
, series
) {
314 // Honors indexes as series.
315 if (series
=== this.dygraph_
.getHighlightSeries()) {
316 if (this.highlightSeries_
.hasOwnProperty(name
)) {
317 return this.highlightSeries_
[name
];
321 if (!this.series_
.hasOwnProperty(series
)) {
322 throw "Unknown series: " + series
;
325 var seriesObj
= this.series_
[series
];
326 var seriesOptions
= seriesObj
["options"];
327 if (seriesOptions
.hasOwnProperty(name
)) {
328 return seriesOptions
[name
];
331 return this.getForAxis(name
, seriesObj
["yAxis"]);
335 * Returns the number of y-axes on the chart.
336 * @return {number} the number of axes.
338 DygraphOptions
.prototype.numAxes
= function() {
339 return this.yAxes_
.length
;
343 * Return the y-axis for a given series, specified by name.
345 DygraphOptions
.prototype.axisForSeries
= function(series
) {
346 return this.series_
[series
].yAxis
;
350 * Returns the options for the specified axis.
352 // TODO(konigsberg): this is y-axis specific. Support the x axis.
353 DygraphOptions
.prototype.axisOptions
= function(yAxis
) {
354 return this.yAxes_
[yAxis
].options
;
358 * Return the series associated with an axis.
360 DygraphOptions
.prototype.seriesForAxis
= function(yAxis
) {
361 return this.yAxes_
[yAxis
].series
;
365 * Return the list of all series, in their columnar order.
367 DygraphOptions
.prototype.seriesNames
= function() {
371 /* Are we using this? */
373 * Return the index of the specified series.
374 * @param {string} series the series name.
376 DygraphOptions
.prototype.indexOfSeries
= function(series
) {
377 return this.series_
[series
].idx
;
380 return DygraphOptions
;