Commit | Line | Data |
---|---|---|
c1780ad0 RK |
1 | /** |
2 | * @fileoverview DygraphOptions is responsible for parsing and returning information about options. | |
3 | * | |
4 | * Still tightly coupled to Dygraphs, we could remove some of that, you know. | |
5 | */ | |
6 | ||
7 | "use strict"; | |
8 | ||
9 | /* | |
10 | * Interesting member variables: | |
11 | * dygraph_ - the graph. | |
12 | * global - global attributes (common among all graphs, AIUI) | |
ed30673a | 13 | * user - attributes set by the user |
5daa462d | 14 | * axes - map of options specific to the axis. |
c1780ad0 RK |
15 | * series - { seriesName -> { idx, yAxis, options } |
16 | * labels - used as mapping from index to series name. | |
17 | */ | |
18 | ||
19 | /** | |
c1780ad0 RK |
20 | * This parses attributes into an object that can be easily queried. |
21 | * | |
5daa462d RK |
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. | |
25 | * | |
c1780ad0 | 26 | * @param {Dyraph} dygraph The chart to which these options belong. |
d67a4279 | 27 | * @constructor |
c1780ad0 RK |
28 | */ |
29 | var DygraphOptions = function(dygraph) { | |
30 | this.dygraph_ = dygraph; | |
ed30673a RK |
31 | this.axes_ = []; |
32 | this.series_ = {}; | |
c1780ad0 | 33 | |
5daa462d | 34 | // Once these two objects are initialized, you can call get(); |
ed30673a RK |
35 | this.global_ = this.dygraph_.attrs_; |
36 | this.user_ = this.dygraph_.user_attrs_ || {}; | |
c1780ad0 | 37 | |
5daa462d | 38 | this.highlightSeries_ = this.get("highlightSeriesOpts") || {}; |
c1780ad0 | 39 | // Get a list of series names. |
34825ef5 | 40 | |
5daa462d | 41 | var labels = this.get("labels"); |
34825ef5 RK |
42 | if (!labels) { |
43 | return; // -- can't do more for now, will parse after getting the labels. | |
5daa462d | 44 | } |
34825ef5 | 45 | |
dd724e22 | 46 | this.reparseSeries(); |
5daa462d | 47 | }; |
34825ef5 | 48 | |
5daa462d RK |
49 | /** |
50 | * Reparses options that are all related to series. This typically occurs when | |
51 | * options are either updated, or source data has been made avaialble. | |
52 | * | |
53 | * TODO(konigsberg): The method name is kind of weak; fix. | |
54 | */ | |
34825ef5 | 55 | DygraphOptions.prototype.reparseSeries = function() { |
5daa462d | 56 | this.labels = this.get("labels").slice(1); |
c1780ad0 | 57 | |
ed30673a RK |
58 | this.axes_ = [ {} ]; // Always one axis at least. |
59 | this.series_ = {}; | |
eb0da59f | 60 | |
73e953cd RK |
61 | // Traditionally, per-series options were specified right up there with the options. For instance |
62 | // { | |
63 | // labels: [ "X", "foo", "bar" ], | |
64 | // pointSize: 3, | |
65 | // foo : {}, // options for foo | |
66 | // bar : {} // options for bar | |
67 | // } | |
68 | // | |
69 | // Moving forward, series really should be specified in the series element, separating them. | |
70 | // like so: | |
71 | // | |
72 | // { | |
73 | // labels: [ "X", "foo", "bar" ], | |
74 | // pointSize: 3, | |
75 | // series : { | |
76 | // foo : {}, // options for foo | |
77 | // bar : {} // options for bar | |
78 | // } | |
79 | // } | |
80 | // | |
81 | // So, if series is found, it's expected to contain per-series data, otherwise we fall | |
82 | // back. | |
d89973c0 | 83 | var allseries = this.user_["series"] ? this.user_.series : this.user_; |
73e953cd | 84 | |
c1780ad0 RK |
85 | var axisId = 0; // 0-offset; there's always one. |
86 | // Go through once, add all the series, and for those with {} axis options, add a new axis. | |
87 | for (var idx = 0; idx < this.labels.length; idx++) { | |
88 | var seriesName = this.labels[idx]; | |
89 | ||
73e953cd | 90 | var optionsForSeries = allseries[seriesName] || {}; |
c1780ad0 RK |
91 | var yAxis = 0; |
92 | ||
93 | var axis = optionsForSeries["axis"]; | |
94 | if (typeof(axis) == 'object') { | |
95 | yAxis = ++axisId; | |
ed30673a | 96 | this.axes_[yAxis] = axis; |
c1780ad0 | 97 | } |
ed30673a | 98 | this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; |
c1780ad0 RK |
99 | } |
100 | ||
101 | // Go through one more time and assign series to an axis defined by another | |
102 | // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } | |
103 | for (var idx = 0; idx < this.labels.length; idx++) { | |
104 | var seriesName = this.labels[idx]; | |
ed30673a | 105 | var optionsForSeries = this.series_[seriesName]["options"]; |
c1780ad0 RK |
106 | var axis = optionsForSeries["axis"]; |
107 | ||
108 | if (typeof(axis) == 'string') { | |
ed30673a | 109 | if (!this.series_.hasOwnProperty(axis)) { |
c1780ad0 RK |
110 | this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " + |
111 | "series " + axis + ", which does not define its own axis."); | |
112 | return null; | |
113 | } | |
ed30673a | 114 | this.series_[seriesName].yAxis = this.series_[axis].yAxis; |
c1780ad0 RK |
115 | } |
116 | } | |
117 | ||
118 | // This doesn't support reading from the 'x' axis, only 'y' and 'y2. | |
119 | // Read from the global "axes" option. | |
d89973c0 | 120 | if (this.user_["axes"]) { |
ed30673a | 121 | var axis_opts = this.user_.axes; |
c1780ad0 | 122 | |
eb0da59f | 123 | if (axis_opts.hasOwnProperty("y")) { |
ed30673a | 124 | Dygraph.update(this.axes_[0], axis_opts.y); |
eb0da59f RK |
125 | } |
126 | ||
127 | if (axis_opts.hasOwnProperty("y2")) { | |
ed30673a RK |
128 | this.axes_[1] = this.axes_[1] || {}; |
129 | Dygraph.update(this.axes_[1], axis_opts.y2); | |
eb0da59f | 130 | } |
c1780ad0 RK |
131 | } |
132 | }; | |
133 | ||
5daa462d RK |
134 | /** |
135 | * Get a global value. | |
136 | * | |
137 | * @param {String} name the name of the option. | |
138 | */ | |
139 | DygraphOptions.prototype.get = function(name) { | |
ed30673a RK |
140 | if (this.user_.hasOwnProperty(name)) { |
141 | return this.user_[name]; | |
c1780ad0 | 142 | } |
ed30673a RK |
143 | if (this.global_.hasOwnProperty(name)) { |
144 | return this.global_[name]; | |
c1780ad0 RK |
145 | } |
146 | return null; | |
5daa462d | 147 | }; |
c1780ad0 | 148 | |
5daa462d RK |
149 | /** |
150 | * Get a value for a specific axis. If there is no specific value for the axis, | |
151 | * the global value is returned. | |
152 | * | |
153 | * @param {String} name the name of the option. | |
154 | * @param {String|number} axis the axis to search. Can be the string representation | |
155 | * ("y", "y2") or the axis number (0, 1). | |
156 | */ | |
157 | DygraphOptions.prototype.getForAxis = function(name, axis) { | |
158 | var axisIdx = 0; | |
159 | if (typeof(axis) == 'number') { | |
160 | axisIdx = axis; | |
161 | } else { | |
162 | // TODO(konigsberg): Accept only valid axis strings? | |
163 | axisIdx = (axis == "y2") ? 1 : 0; | |
164 | } | |
c1780ad0 | 165 | |
ed30673a | 166 | var axisOptions = this.axes_[axisIdx]; |
c1780ad0 RK |
167 | if (axisOptions.hasOwnProperty(name)) { |
168 | return axisOptions[name]; | |
169 | } | |
5daa462d RK |
170 | return this.get(name); |
171 | }; | |
c1780ad0 | 172 | |
5daa462d RK |
173 | /** |
174 | * Get a value for a specific series. If there is no specific value for the series, | |
175 | * the value for the axis is returned (and afterwards, the global value.) | |
176 | * | |
177 | * @param {String} name the name of the option. | |
178 | * @param {String|number} series the series to search. Can be the string representation | |
179 | * or 0-offset series number. | |
180 | */ | |
181 | DygraphOptions.prototype.getForSeries = function(name, series) { | |
c1780ad0 RK |
182 | // Honors indexes as series. |
183 | var seriesName = (typeof(series) == "number") ? this.labels[series] : series; | |
184 | ||
ed30673a RK |
185 | if (seriesName === this.dygraph_.highlightSet_) { |
186 | if (this.highlightSeries_.hasOwnProperty(name)) { | |
187 | return this.highlightSeries_[name]; | |
188 | } | |
189 | } | |
190 | ||
191 | if (!this.series_.hasOwnProperty(seriesName)) { | |
c1780ad0 RK |
192 | throw "Unknown series: " + series; |
193 | } | |
194 | ||
ed30673a | 195 | var seriesObj = this.series_[seriesName]; |
c1780ad0 RK |
196 | var seriesOptions = seriesObj["options"]; |
197 | if (seriesOptions.hasOwnProperty(name)) { | |
198 | return seriesOptions[name]; | |
199 | } | |
ed30673a | 200 | |
5daa462d RK |
201 | return this.getForAxis(name, seriesObj["yAxis"]); |
202 | }; | |
c1780ad0 | 203 |