Commit | Line | Data |
---|---|---|
c1780ad0 | 1 | /** |
8e1ec00c MF |
2 | * @license |
3 | * Copyright 2011 Dan Vanderkam (danvdk@gmail.com) | |
4 | * MIT-licensed (http://opensource.org/licenses/MIT) | |
5 | */ | |
6 | ||
7 | /** | |
c1780ad0 RK |
8 | * @fileoverview DygraphOptions is responsible for parsing and returning information about options. |
9 | * | |
10 | * Still tightly coupled to Dygraphs, we could remove some of that, you know. | |
11 | */ | |
12 | ||
384bfa4a DV |
13 | var DygraphOptions = (function() { |
14 | ||
15 | /*jshint sub:true */ | |
83b0c192 | 16 | /*global Dygraph:false */ |
c1780ad0 RK |
17 | "use strict"; |
18 | ||
19 | /* | |
20 | * Interesting member variables: | |
21 | * dygraph_ - the graph. | |
673a3b87 | 22 | * global_ - global attributes (common among all graphs, AIUI) |
ed30673a | 23 | * user - attributes set by the user |
48dc3815 RK |
24 | * yAxes_ - array of axis index to { series : [ series names ] , options : { axis-specific options. } |
25 | * xAxis_ - { options : { axis-specific options. } | |
242a8bc8 | 26 | * series_ - { seriesName -> { idx, yAxis, options }} |
673a3b87 | 27 | * labels_ - used as mapping from index to series name. |
c1780ad0 RK |
28 | */ |
29 | ||
30 | /** | |
c1780ad0 RK |
31 | * This parses attributes into an object that can be easily queried. |
32 | * | |
5daa462d RK |
33 | * It doesn't necessarily mean that all options are available, specifically |
34 | * if labels are not yet available, since those drive details of the per-series | |
35 | * and per-axis options. | |
36 | * | |
c1780ad0 | 37 | * @param {Dyraph} dygraph The chart to which these options belong. |
d67a4279 | 38 | * @constructor |
c1780ad0 RK |
39 | */ |
40 | var DygraphOptions = function(dygraph) { | |
41 | this.dygraph_ = dygraph; | |
48dc3815 RK |
42 | this.yAxes_ = []; |
43 | this.xAxis_ = {}; | |
ed30673a | 44 | this.series_ = {}; |
c1780ad0 | 45 | |
5daa462d | 46 | // Once these two objects are initialized, you can call get(); |
ed30673a RK |
47 | this.global_ = this.dygraph_.attrs_; |
48 | this.user_ = this.dygraph_.user_attrs_ || {}; | |
c1780ad0 | 49 | |
5daa462d | 50 | this.highlightSeries_ = this.get("highlightSeriesOpts") || {}; |
dd724e22 | 51 | this.reparseSeries(); |
5daa462d | 52 | }; |
34825ef5 | 53 | |
632bd78c RK |
54 | /* |
55 | * Not optimal, but does the trick when you're only using two axes. | |
56 | * If we move to more axes, this can just become a function. | |
57 | */ | |
58 | DygraphOptions.AXIS_STRING_MAPPINGS_ = { | |
59 | 'y' : 0, | |
60 | 'Y' : 0, | |
61 | 'y1' : 0, | |
62 | 'Y1' : 0, | |
63 | 'y2' : 1, | |
64 | 'Y2' : 1 | |
04f2bce6 | 65 | }; |
632bd78c RK |
66 | |
67 | DygraphOptions.axisToIndex_ = function(axis) { | |
68 | if (typeof(axis) == "string") { | |
69 | if (DygraphOptions.AXIS_STRING_MAPPINGS_.hasOwnProperty(axis)) { | |
70 | return DygraphOptions.AXIS_STRING_MAPPINGS_[axis]; | |
71 | } | |
04f2bce6 | 72 | throw "Unknown axis : " + axis; |
632bd78c RK |
73 | } |
74 | if (typeof(axis) == "number") { | |
04f2bce6 | 75 | if (axis === 0 || axis === 1) { |
632bd78c RK |
76 | return axis; |
77 | } | |
04f2bce6 | 78 | throw "Dygraphs only supports two y-axes, indexed from 0-1."; |
632bd78c | 79 | } |
cee95ae0 | 80 | if (typeof(axis) == "object") { |
83b0c192 DV |
81 | throw "Using objects for axis specification " + |
82 | "is not supported inside the 'series' option."; | |
cee95ae0 | 83 | } |
632bd78c RK |
84 | if (axis) { |
85 | throw "Unknown axis : " + axis; | |
86 | } | |
87 | // No axis specification means axis 0. | |
88 | return 0; | |
89 | }; | |
90 | ||
5daa462d RK |
91 | /** |
92 | * Reparses options that are all related to series. This typically occurs when | |
2fd143d3 | 93 | * options are either updated, or source data has been made available. |
5daa462d RK |
94 | * |
95 | * TODO(konigsberg): The method name is kind of weak; fix. | |
96 | */ | |
34825ef5 | 97 | DygraphOptions.prototype.reparseSeries = function() { |
9d562235 PF |
98 | var labels = this.get("labels"); |
99 | if (!labels) { | |
100 | return; // -- can't do more for now, will parse after getting the labels. | |
101 | } | |
102 | ||
cd872296 | 103 | this.labels_ = labels.slice(1); |
c1780ad0 | 104 | |
48dc3815 RK |
105 | this.yAxes_ = [ { series : [], options : {}} ]; // Always one axis at least. |
106 | this.xAxis_ = { options : {} }; | |
ed30673a | 107 | this.series_ = {}; |
eb0da59f | 108 | |
73e953cd RK |
109 | // Traditionally, per-series options were specified right up there with the options. For instance |
110 | // { | |
111 | // labels: [ "X", "foo", "bar" ], | |
112 | // pointSize: 3, | |
113 | // foo : {}, // options for foo | |
114 | // bar : {} // options for bar | |
115 | // } | |
116 | // | |
117 | // Moving forward, series really should be specified in the series element, separating them. | |
118 | // like so: | |
119 | // | |
120 | // { | |
121 | // labels: [ "X", "foo", "bar" ], | |
122 | // pointSize: 3, | |
123 | // series : { | |
124 | // foo : {}, // options for foo | |
125 | // bar : {} // options for bar | |
126 | // } | |
127 | // } | |
128 | // | |
129 | // So, if series is found, it's expected to contain per-series data, otherwise we fall | |
130 | // back. | |
632bd78c | 131 | var oldStyleSeries = !this.user_["series"]; |
9d562235 | 132 | |
632bd78c RK |
133 | if (oldStyleSeries) { |
134 | var axisId = 0; // 0-offset; there's always one. | |
135 | // Go through once, add all the series, and for those with {} axis options, add a new axis. | |
cd872296 RK |
136 | for (var idx = 0; idx < this.labels_.length; idx++) { |
137 | var seriesName = this.labels_[idx]; | |
9d562235 | 138 | |
632bd78c | 139 | var optionsForSeries = this.user_[seriesName] || {}; |
9d562235 | 140 | |
632bd78c RK |
141 | var yAxis = 0; |
142 | var axis = optionsForSeries["axis"]; | |
143 | if (typeof(axis) == 'object') { | |
144 | yAxis = ++axisId; | |
48dc3815 | 145 | this.yAxes_[yAxis] = { series : [ seriesName ], options : axis }; |
632bd78c | 146 | } |
c1780ad0 | 147 | |
6ad8b6a4 RK |
148 | // Associate series without axis options with axis 0. |
149 | if (!axis) { // undefined | |
48dc3815 | 150 | this.yAxes_[0].series.push(seriesName); |
6ad8b6a4 | 151 | } |
c1780ad0 | 152 | |
632bd78c RK |
153 | this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; |
154 | } | |
9d562235 | 155 | |
632bd78c RK |
156 | // Go through one more time and assign series to an axis defined by another |
157 | // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } | |
cd872296 RK |
158 | for (var idx = 0; idx < this.labels_.length; idx++) { |
159 | var seriesName = this.labels_[idx]; | |
9d562235 | 160 | var optionsForSeries = this.series_[seriesName]["options"]; |
632bd78c | 161 | var axis = optionsForSeries["axis"]; |
9d562235 | 162 | |
632bd78c RK |
163 | if (typeof(axis) == 'string') { |
164 | if (!this.series_.hasOwnProperty(axis)) { | |
165 | this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " + | |
166 | "series " + axis + ", which does not define its own axis."); | |
167 | return null; | |
168 | } | |
6ad8b6a4 RK |
169 | var yAxis = this.series_[axis].yAxis; |
170 | this.series_[seriesName].yAxis = yAxis; | |
48dc3815 | 171 | this.yAxes_[yAxis].series.push(seriesName); |
632bd78c RK |
172 | } |
173 | } | |
174 | } else { | |
cd872296 RK |
175 | for (var idx = 0; idx < this.labels_.length; idx++) { |
176 | var seriesName = this.labels_[idx]; | |
632bd78c RK |
177 | var optionsForSeries = this.user_.series[seriesName] || {}; |
178 | var yAxis = DygraphOptions.axisToIndex_(optionsForSeries["axis"]); | |
c1780ad0 | 179 | |
632bd78c RK |
180 | this.series_[seriesName] = { |
181 | idx: idx, | |
182 | yAxis: yAxis, | |
183 | options : optionsForSeries }; | |
c1780ad0 | 184 | |
48dc3815 RK |
185 | if (!this.yAxes_[yAxis]) { |
186 | this.yAxes_[yAxis] = { series : [ seriesName ], options : {} }; | |
6ad8b6a4 | 187 | } else { |
48dc3815 | 188 | this.yAxes_[yAxis].series.push(seriesName); |
c1780ad0 | 189 | } |
c1780ad0 RK |
190 | } |
191 | } | |
192 | ||
6ad8b6a4 | 193 | var axis_opts = this.user_["axes"] || {}; |
48dc3815 RK |
194 | Dygraph.update(this.yAxes_[0].options, axis_opts["y"] || {}); |
195 | if (this.yAxes_.length > 1) { | |
9d562235 | 196 | Dygraph.update(this.yAxes_[1].options, axis_opts["y2"] || {}); |
c1780ad0 | 197 | } |
48dc3815 | 198 | Dygraph.update(this.xAxis_.options, axis_opts["x"] || {}); |
c1780ad0 RK |
199 | }; |
200 | ||
5daa462d RK |
201 | /** |
202 | * Get a global value. | |
203 | * | |
a4aceb34 | 204 | * @param {string} name the name of the option. |
5daa462d RK |
205 | */ |
206 | DygraphOptions.prototype.get = function(name) { | |
d574a45e | 207 | var result = this.getGlobalUser_(name); |
2e3d8088 | 208 | if (result !== null) { |
d574a45e RK |
209 | return result; |
210 | } | |
211 | return this.getGlobalDefault_(name); | |
212 | }; | |
213 | ||
214 | DygraphOptions.prototype.getGlobalUser_ = function(name) { | |
ed30673a RK |
215 | if (this.user_.hasOwnProperty(name)) { |
216 | return this.user_[name]; | |
c1780ad0 | 217 | } |
d574a45e RK |
218 | return null; |
219 | }; | |
220 | ||
221 | DygraphOptions.prototype.getGlobalDefault_ = function(name) { | |
ed30673a RK |
222 | if (this.global_.hasOwnProperty(name)) { |
223 | return this.global_[name]; | |
c1780ad0 | 224 | } |
d574a45e RK |
225 | if (Dygraph.DEFAULT_ATTRS.hasOwnProperty(name)) { |
226 | return Dygraph.DEFAULT_ATTRS[name]; | |
227 | } | |
c1780ad0 | 228 | return null; |
83b0c192 | 229 | }; |
c1780ad0 | 230 | |
5daa462d RK |
231 | /** |
232 | * Get a value for a specific axis. If there is no specific value for the axis, | |
233 | * the global value is returned. | |
234 | * | |
a4aceb34 RK |
235 | * @param {string} name the name of the option. |
236 | * @param {string|number} axis the axis to search. Can be the string representation | |
5daa462d RK |
237 | * ("y", "y2") or the axis number (0, 1). |
238 | */ | |
239 | DygraphOptions.prototype.getForAxis = function(name, axis) { | |
48dc3815 RK |
240 | var axisIdx; |
241 | var axisString; | |
242 | ||
243 | // Since axis can be a number or a string, straighten everything out here. | |
5daa462d RK |
244 | if (typeof(axis) == 'number') { |
245 | axisIdx = axis; | |
2e3d8088 | 246 | axisString = axisIdx === 0 ? "y" : "y2"; |
5daa462d | 247 | } else { |
48dc3815 RK |
248 | if (axis == "y1") { axis = "y"; } // Standardize on 'y'. Is this bad? I think so. |
249 | if (axis == "y") { | |
250 | axisIdx = 0; | |
251 | } else if (axis == "y2") { | |
252 | axisIdx = 1; | |
253 | } else if (axis == "x") { | |
254 | axisIdx = -1; // simply a placeholder for below. | |
255 | } else { | |
256 | throw "Unknown axis " + axis; | |
257 | } | |
258 | axisString = axis; | |
5daa462d | 259 | } |
48dc3815 RK |
260 | |
261 | var userAxis = (axisIdx == -1) ? this.xAxis_ : this.yAxes_[axisIdx]; | |
262 | ||
d574a45e | 263 | // Search the user-specified axis option first. |
48dc3815 RK |
264 | if (userAxis) { // This condition could be removed if we always set up this.yAxes_ for y2. |
265 | var axisOptions = userAxis.options; | |
d574a45e RK |
266 | if (axisOptions.hasOwnProperty(name)) { |
267 | return axisOptions[name]; | |
268 | } | |
269 | } | |
c1780ad0 | 270 | |
d574a45e RK |
271 | // User-specified global options second. |
272 | var result = this.getGlobalUser_(name); | |
2e3d8088 | 273 | if (result !== null) { |
d574a45e | 274 | return result; |
c1780ad0 | 275 | } |
d574a45e RK |
276 | |
277 | // Default axis options third. | |
d574a45e RK |
278 | var defaultAxisOptions = Dygraph.DEFAULT_ATTRS.axes[axisString]; |
279 | if (defaultAxisOptions.hasOwnProperty(name)) { | |
280 | return defaultAxisOptions[name]; | |
281 | } | |
282 | ||
283 | // Default global options last. | |
284 | return this.getGlobalDefault_(name); | |
5daa462d | 285 | }; |
c1780ad0 | 286 | |
5daa462d RK |
287 | /** |
288 | * Get a value for a specific series. If there is no specific value for the series, | |
289 | * the value for the axis is returned (and afterwards, the global value.) | |
290 | * | |
a4aceb34 | 291 | * @param {string} name the name of the option. |
8e19509a | 292 | * @param {string} series the series to search. |
5daa462d RK |
293 | */ |
294 | DygraphOptions.prototype.getForSeries = function(name, series) { | |
c1780ad0 | 295 | // Honors indexes as series. |
8e19509a | 296 | if (series === this.dygraph_.highlightSet_) { |
ed30673a RK |
297 | if (this.highlightSeries_.hasOwnProperty(name)) { |
298 | return this.highlightSeries_[name]; | |
299 | } | |
300 | } | |
301 | ||
8e19509a | 302 | if (!this.series_.hasOwnProperty(series)) { |
c1780ad0 RK |
303 | throw "Unknown series: " + series; |
304 | } | |
305 | ||
8e19509a | 306 | var seriesObj = this.series_[series]; |
c1780ad0 RK |
307 | var seriesOptions = seriesObj["options"]; |
308 | if (seriesOptions.hasOwnProperty(name)) { | |
309 | return seriesOptions[name]; | |
310 | } | |
ed30673a | 311 | |
5daa462d RK |
312 | return this.getForAxis(name, seriesObj["yAxis"]); |
313 | }; | |
c1780ad0 | 314 | |
673a3b87 RK |
315 | /** |
316 | * Returns the number of y-axes on the chart. | |
317 | * @return {Number} the number of axes. | |
318 | */ | |
319 | DygraphOptions.prototype.numAxes = function() { | |
48dc3815 | 320 | return this.yAxes_.length; |
04f2bce6 | 321 | }; |
16f00742 RK |
322 | |
323 | /** | |
324 | * Return the y-axis for a given series, specified by name. | |
325 | */ | |
8e19509a RK |
326 | DygraphOptions.prototype.axisForSeries = function(series) { |
327 | return this.series_[series].yAxis; | |
04f2bce6 | 328 | }; |
16f00742 RK |
329 | |
330 | /** | |
331 | * Returns the options for the specified axis. | |
332 | */ | |
48dc3815 | 333 | // TODO(konigsberg): this is y-axis specific. Support the x axis. |
16f00742 | 334 | DygraphOptions.prototype.axisOptions = function(yAxis) { |
48dc3815 | 335 | return this.yAxes_[yAxis].options; |
04f2bce6 | 336 | }; |
16f00742 RK |
337 | |
338 | /** | |
339 | * Return the series associated with an axis. | |
340 | */ | |
341 | DygraphOptions.prototype.seriesForAxis = function(yAxis) { | |
48dc3815 | 342 | return this.yAxes_[yAxis].series; |
04f2bce6 | 343 | }; |
6ad8b6a4 RK |
344 | |
345 | /** | |
346 | * Return the list of all series, in their columnar order. | |
347 | */ | |
348 | DygraphOptions.prototype.seriesNames = function() { | |
349 | return this.labels_; | |
04f2bce6 | 350 | }; |
6ad8b6a4 | 351 | |
9d562235 | 352 | /* Are we using this? */ |
6ad8b6a4 RK |
353 | /** |
354 | * Return the index of the specified series. | |
355 | * @param {string} series the series name. | |
356 | */ | |
357 | DygraphOptions.prototype.indexOfSeries = function(series) { | |
358 | return this.series_[series].idx; | |
04f2bce6 | 359 | }; |
384bfa4a DV |
360 | |
361 | return DygraphOptions; | |
362 | ||
363 | })(); |