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 * axes_ - array of axis index to { series : [ series names ] , options : { axis-specific options. }
15 * series_ - { seriesName -> { idx, yAxis, options }}
16 * labels_ - used as mapping from index to series name.
20 * This parses attributes into an object that can be easily queried.
22 * It doesn't necessarily mean that all options are available, specifically
23 * if labels are not yet available, since those drive details of the per-series
24 * and per-axis options.
26 * @param {Dyraph} dygraph The chart to which these options belong.
29 var DygraphOptions
= function(dygraph
) {
30 this.dygraph_
= dygraph
;
34 // Once these two objects are initialized, you can call get();
35 this.global_
= this.dygraph_
.attrs_
;
36 this.user_
= this.dygraph_
.user_attrs_
|| {};
38 this.highlightSeries_
= this.get("highlightSeriesOpts") || {};
39 // Get a list of series names.
41 var labels
= this.get("labels");
43 return; // -- can't do more for now, will parse after getting the labels.
50 * Not optimal, but does the trick when you're only using two axes.
51 * If we move to more axes, this can just become a function.
53 DygraphOptions
.AXIS_STRING_MAPPINGS_
= {
62 DygraphOptions
.axisToIndex_
= function(axis
) {
63 if (typeof(axis
) == "string") {
64 if (DygraphOptions
.AXIS_STRING_MAPPINGS_
.hasOwnProperty(axis
)) {
65 return DygraphOptions
.AXIS_STRING_MAPPINGS_
[axis
];
67 throw "Unknown axis : " + axis
;
69 if (typeof(axis
) == "number") {
70 if (axis
=== 0 || axis
=== 1) {
73 throw "Dygraphs only supports two y-axes, indexed from 0-1.";
75 if (typeof(axis
) == "object") {
76 throw "Using objects for axis specification "
77 + "is not supported inside the 'series' option.";
80 throw "Unknown axis : " + axis
;
82 // No axis specification means axis 0.
87 * Reparses options that are all related to series. This typically occurs when
88 * options are either updated, or source data has been made avaialble.
90 * TODO(konigsberg): The method name is kind of weak; fix.
92 DygraphOptions
.prototype.reparseSeries
= function() {
93 this.labels
= this.get("labels").slice(1);
95 this.axes_
= [ { series
: [], options
: {}} ]; // Always one axis at least.
98 // Traditionally, per-series options were specified right up there with the options. For instance
100 // labels: [ "X", "foo", "bar" ],
102 // foo : {}, // options
for foo
103 // bar : {} // options
for bar
106 // Moving forward, series really should be specified in the series element, separating them.
110 // labels: [ "X", "foo", "bar" ],
113 // foo : {}, // options
for foo
114 // bar : {} // options
for bar
118 // So, if series is found, it's expected to contain per-series data, otherwise we fall
120 var oldStyleSeries
= !this.user_
["series"];
122 if (oldStyleSeries
) {
123 var axisId
= 0; // 0-offset; there's always one.
124 // Go through once, add all the series, and for those with {} axis options, add a new axis.
125 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
126 var seriesName
= this.labels
[idx
];
128 var optionsForSeries
= this.user_
[seriesName
] || {};
131 var axis
= optionsForSeries
["axis"];
132 if (typeof(axis
) == 'object') {
134 this.axes_
[yAxis
] = { series
: [ seriesName
], options
: axis
};
137 // Associate series without axis options with axis 0.
138 if (!axis
) { // undefined
139 this.axes_
[0].series
.push(seriesName
);
142 this.series_
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
145 // Go through one more time and assign series to an axis defined by another
146 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
147 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
148 var seriesName
= this.labels
[idx
];
149 var optionsForSeries
= this.series_
[seriesName
]["options"];
150 var axis
= optionsForSeries
["axis"];
152 if (typeof(axis
) == 'string') {
153 if (!this.series_
.hasOwnProperty(axis
)) {
154 this.dygraph_
.error("Series " + seriesName
+ " wants to share a y-axis with " +
155 "series " + axis
+ ", which does not define its own axis.");
158 var yAxis
= this.series_
[axis
].yAxis
;
159 this.series_
[seriesName
].yAxis
= yAxis
;
160 this.axes_
[yAxis
].series
.push(seriesName
);
164 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
165 var seriesName
= this.labels
[idx
];
166 var optionsForSeries
= this.user_
.series
[seriesName
] || {};
167 var yAxis
= DygraphOptions
.axisToIndex_(optionsForSeries
["axis"]);
169 this.series_
[seriesName
] = {
172 options
: optionsForSeries
};
174 if (!this.axes_
[yAxis
]) {
175 this.axes_
[yAxis
] = { series
: [ seriesName
], options
: {} };
177 this.axes_
[yAxis
].series
.push(seriesName
);
182 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
183 var axis_opts
= this.user_
["axes"] || {};
184 Dygraph
.update(this.axes_
[0].options
, axis_opts
["y"] || {});
185 if (this.axes_
.length
> 1) {
186 Dygraph
.update(this.axes_
[1].options
, axis_opts
["y2"] || {});
191 * Get a global value.
193 * @param {string} name the name of the option.
195 DygraphOptions
.prototype.get
= function(name
) {
196 var result
= this.getGlobalUser_(name
);
197 if (result
!= null) {
200 return this.getGlobalDefault_(name
);
203 DygraphOptions
.prototype.getGlobalUser_
= function(name
) {
204 if (this.user_
.hasOwnProperty(name
)) {
205 return this.user_
[name
];
210 DygraphOptions
.prototype.getGlobalDefault_
= function(name
) {
211 if (this.global_
.hasOwnProperty(name
)) {
212 return this.global_
[name
];
214 if (Dygraph
.DEFAULT_ATTRS
.hasOwnProperty(name
)) {
215 return Dygraph
.DEFAULT_ATTRS
[name
];
221 * Get a value for a specific axis. If there is no specific value for the axis,
222 * the global value is returned.
224 * @param {string} name the name of the option.
225 * @param {string|number} axis the axis to search. Can be the string representation
226 * ("y", "y2") or the axis number (0, 1).
228 DygraphOptions
.prototype.getForAxis
= function(name
, axis
) {
230 if (typeof(axis
) == 'number') {
233 // TODO(konigsberg): Accept only valid axis strings?
234 axisIdx
= (axis
== "y2") ? 1 : 0;
236 // Search the user-specified axis option first.
237 if (this.axes_
[axisIdx
]) {
238 var axisOptions
= this.axes_
[axisIdx
].options
;
239 if (axisOptions
.hasOwnProperty(name
)) {
240 return axisOptions
[name
];
244 // User-specified global options second.
245 var result
= this.getGlobalUser_(name
);
246 if (result
!= null) {
250 // Default axis options third.
251 var axisString
= axis
== 0 ? "y" : "y2";
252 var defaultAxisOptions
= Dygraph
.DEFAULT_ATTRS
.axes
[axisString
];
253 if (defaultAxisOptions
.hasOwnProperty(name
)) {
254 return defaultAxisOptions
[name
];
257 // Default global options last.
258 return this.getGlobalDefault_(name
);
262 * Get a value for a specific series. If there is no specific value for the series,
263 * the value for the axis is returned (and afterwards, the global value.)
265 * @param {string} name the name of the option.
266 * @param {string|number} series the series to search. Can be the string representation
267 * or 0-offset series number.
269 DygraphOptions
.prototype.getForSeries
= function(name
, series
) {
270 // Honors indexes as series.
271 var seriesName
= (typeof(series
) == "number") ? this.labels
[series
] : series
;
273 if (seriesName
=== this.dygraph_
.highlightSet_
) {
274 if (this.highlightSeries_
.hasOwnProperty(name
)) {
275 return this.highlightSeries_
[name
];
279 if (!this.series_
.hasOwnProperty(seriesName
)) {
280 throw "Unknown series: " + series
;
283 var seriesObj
= this.series_
[seriesName
];
284 var seriesOptions
= seriesObj
["options"];
285 if (seriesOptions
.hasOwnProperty(name
)) {
286 return seriesOptions
[name
];
289 return this.getForAxis(name
, seriesObj
["yAxis"]);
293 * Returns the number of y-axes on the chart.
294 * @return {Number} the number of axes.
296 DygraphOptions
.prototype.numAxes
= function() {
297 return this.axes_
.length
;
301 * Return the y-axis for a given series, specified by name.
303 DygraphOptions
.prototype.axisForSeries
= function(seriesName
) {
304 return this.series_
[seriesName
].yAxis
;
308 * Returns the options for the specified axis.
310 DygraphOptions
.prototype.axisOptions
= function(yAxis
) {
311 return this.axes_
[yAxis
].options
;
315 * Return the series associated with an axis.
317 DygraphOptions
.prototype.seriesForAxis
= function(yAxis
) {
318 return this.axes_
[yAxis
].series
;
322 * Return the list of all series, in their columnar order.
324 DygraphOptions
.prototype.seriesNames
= function() {
328 /* Are we using this? */
330 * Return the index of the specified series.
331 * @param {string} series the series name.
333 DygraphOptions
.prototype.indexOfSeries
= function(series
) {
334 return this.series_
[series
].idx
;