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") || {};
45 * Not optimal, but does the trick when you're only using two axes.
46 * If we move to more axes, this can just become a function.
48 DygraphOptions
.AXIS_STRING_MAPPINGS_
= {
57 DygraphOptions
.axisToIndex_
= function(axis
) {
58 if (typeof(axis
) == "string") {
59 if (DygraphOptions
.AXIS_STRING_MAPPINGS_
.hasOwnProperty(axis
)) {
60 return DygraphOptions
.AXIS_STRING_MAPPINGS_
[axis
];
62 throw "Unknown axis : " + axis
;
64 if (typeof(axis
) == "number") {
65 if (axis
=== 0 || axis
=== 1) {
68 throw "Dygraphs only supports two y-axes, indexed from 0-1.";
70 if (typeof(axis
) == "object") {
71 throw "Using objects for axis specification "
72 + "is not supported inside the 'series' option.";
75 throw "Unknown axis : " + axis
;
77 // No axis specification means axis 0.
82 * Reparses options that are all related to series. This typically occurs when
83 * options are either updated, or source data has been made avaialble.
85 * TODO(konigsberg): The method name is kind of weak; fix.
87 DygraphOptions
.prototype.reparseSeries
= function() {
88 var labels
= this.get("labels");
90 return; // -- can't do more for now, will parse after getting the labels.
93 this.labels
= labels
.slice(1);
95 this.yAxes_
= [ { series
: [], options
: {}} ]; // Always one axis at least.
96 this.xAxis_
= { options
: {} };
99 // Traditionally, per-series options were specified right up there with the options. For instance
101 // labels: [ "X", "foo", "bar" ],
103 // foo : {}, // options
for foo
104 // bar : {} // options
for bar
107 // Moving forward, series really should be specified in the series element, separating them.
111 // labels: [ "X", "foo", "bar" ],
114 // foo : {}, // options
for foo
115 // bar : {} // options
for bar
119 // So, if series is found, it's expected to contain per-series data, otherwise we fall
121 var oldStyleSeries
= !this.user_
["series"];
123 if (oldStyleSeries
) {
124 var axisId
= 0; // 0-offset; there's always one.
125 // Go through once, add all the series, and for those with {} axis options, add a new axis.
126 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
127 var seriesName
= this.labels
[idx
];
129 var optionsForSeries
= this.user_
[seriesName
] || {};
132 var axis
= optionsForSeries
["axis"];
133 if (typeof(axis
) == 'object') {
135 this.yAxes_
[yAxis
] = { series
: [ seriesName
], options
: axis
};
138 // Associate series without axis options with axis 0.
139 if (!axis
) { // undefined
140 this.yAxes_
[0].series
.push(seriesName
);
143 this.series_
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
146 // Go through one more time and assign series to an axis defined by another
147 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
148 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
149 var seriesName
= this.labels
[idx
];
150 var optionsForSeries
= this.series_
[seriesName
]["options"];
151 var axis
= optionsForSeries
["axis"];
153 if (typeof(axis
) == 'string') {
154 if (!this.series_
.hasOwnProperty(axis
)) {
155 this.dygraph_
.error("Series " + seriesName
+ " wants to share a y-axis with " +
156 "series " + axis
+ ", which does not define its own axis.");
159 var yAxis
= this.series_
[axis
].yAxis
;
160 this.series_
[seriesName
].yAxis
= yAxis
;
161 this.yAxes_
[yAxis
].series
.push(seriesName
);
165 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
166 var seriesName
= this.labels
[idx
];
167 var optionsForSeries
= this.user_
.series
[seriesName
] || {};
168 var yAxis
= DygraphOptions
.axisToIndex_(optionsForSeries
["axis"]);
170 this.series_
[seriesName
] = {
173 options
: optionsForSeries
};
175 if (!this.yAxes_
[yAxis
]) {
176 this.yAxes_
[yAxis
] = { series
: [ seriesName
], options
: {} };
178 this.yAxes_
[yAxis
].series
.push(seriesName
);
183 var axis_opts
= this.user_
["axes"] || {};
184 Dygraph
.update(this.yAxes_
[0].options
, axis_opts
["y"] || {});
185 if (this.yAxes_
.length
> 1) {
186 Dygraph
.update(this.yAxes_
[1].options
, axis_opts
["y2"] || {});
188 Dygraph
.update(this.xAxis_
.options
, axis_opts
["x"] || {});
192 * Get a global value.
194 * @param {string} name the name of the option.
196 DygraphOptions
.prototype.get
= function(name
) {
197 var result
= this.getGlobalUser_(name
);
198 if (result
!= null) {
201 return this.getGlobalDefault_(name
);
204 DygraphOptions
.prototype.getGlobalUser_
= function(name
) {
205 if (this.user_
.hasOwnProperty(name
)) {
206 return this.user_
[name
];
211 DygraphOptions
.prototype.getGlobalDefault_
= function(name
) {
212 if (this.global_
.hasOwnProperty(name
)) {
213 return this.global_
[name
];
215 if (Dygraph
.DEFAULT_ATTRS
.hasOwnProperty(name
)) {
216 return Dygraph
.DEFAULT_ATTRS
[name
];
222 * Get a value for a specific axis. If there is no specific value for the axis,
223 * the global value is returned.
225 * @param {string} name the name of the option.
226 * @param {string|number} axis the axis to search. Can be the string representation
227 * ("y", "y2") or the axis number (0, 1).
229 DygraphOptions
.prototype.getForAxis
= function(name
, axis
) {
233 // Since axis can be a number or a string, straighten everything out here.
234 if (typeof(axis
) == 'number') {
236 axisString
= axisIdx
== 0 ? "y" : "y2";
238 if (axis
== "y1") { axis
= "y"; } // Standardize on 'y'. Is this bad? I think so.
241 } else if (axis
== "y2") {
243 } else if (axis
== "x") {
244 axisIdx
= -1; // simply a placeholder for below.
246 throw "Unknown axis " + axis
;
251 var userAxis
= (axisIdx
== -1) ? this.xAxis_
: this.yAxes_
[axisIdx
];
253 // Search the user-specified axis option first.
254 if (userAxis
) { // This condition could be removed if we always set up this.yAxes_ for y2.
255 var axisOptions
= userAxis
.options
;
256 if (axisOptions
.hasOwnProperty(name
)) {
257 return axisOptions
[name
];
261 // User-specified global options second.
262 var result
= this.getGlobalUser_(name
);
263 if (result
!= null) {
267 // Default axis options third.
268 var defaultAxisOptions
= Dygraph
.DEFAULT_ATTRS
.axes
[axisString
];
269 if (defaultAxisOptions
.hasOwnProperty(name
)) {
270 return defaultAxisOptions
[name
];
273 // Default global options last.
274 return this.getGlobalDefault_(name
);
278 * Get a value for a specific series. If there is no specific value for the series,
279 * the value for the axis is returned (and afterwards, the global value.)
281 * @param {string} name the name of the option.
282 * @param {string} series the series to search.
284 DygraphOptions
.prototype.getForSeries
= function(name
, series
) {
285 // Honors indexes as series.
286 if (series
=== this.dygraph_
.highlightSet_
) {
287 if (this.highlightSeries_
.hasOwnProperty(name
)) {
288 return this.highlightSeries_
[name
];
292 if (!this.series_
.hasOwnProperty(series
)) {
293 throw "Unknown series: " + series
;
296 var seriesObj
= this.series_
[series
];
297 var seriesOptions
= seriesObj
["options"];
298 if (seriesOptions
.hasOwnProperty(name
)) {
299 return seriesOptions
[name
];
302 return this.getForAxis(name
, seriesObj
["yAxis"]);
306 * Returns the number of y-axes on the chart.
307 * @return {Number} the number of axes.
309 DygraphOptions
.prototype.numAxes
= function() {
310 return this.yAxes_
.length
;
314 * Return the y-axis for a given series, specified by name.
316 DygraphOptions
.prototype.axisForSeries
= function(series
) {
317 return this.series_
[series
].yAxis
;
321 * Returns the options for the specified axis.
323 // TODO(konigsberg): this is y-axis specific. Support the x axis.
324 DygraphOptions
.prototype.axisOptions
= function(yAxis
) {
325 return this.yAxes_
[yAxis
].options
;
329 * Return the series associated with an axis.
331 DygraphOptions
.prototype.seriesForAxis
= function(yAxis
) {
332 return this.yAxes_
[yAxis
].series
;
336 * Return the list of all series, in their columnar order.
338 DygraphOptions
.prototype.seriesNames
= function() {
342 /* Are we using this? */
344 * Return the index of the specified series.
345 * @param {string} series the series name.
347 DygraphOptions
.prototype.indexOfSeries
= function(series
) {
348 return this.series_
[series
].idx
;