Start working on new-version axis support. Tests still fail.
[dygraphs.git] / dygraph-options.js
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)
13 * user - attributes set by the user
14 * axes - map of options specific to the axis.
15 * series - { seriesName -> { idx, yAxis, options }
16 * labels - used as mapping from index to series name.
17 */
18
19 /**
20 * This parses attributes into an object that can be easily queried.
21 *
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 *
26 * @param {Dyraph} dygraph The chart to which these options belong.
27 * @constructor
28 */
29 var DygraphOptions = function(dygraph) {
30 this.dygraph_ = dygraph;
31 this.axes_ = [];
32 this.series_ = {};
33
34 // Once these two objects are initialized, you can call get();
35 this.global_ = this.dygraph_.attrs_;
36 this.user_ = this.dygraph_.user_attrs_ || {};
37
38 this.highlightSeries_ = this.get("highlightSeriesOpts") || {};
39 // Get a list of series names.
40
41 var labels = this.get("labels");
42 if (!labels) {
43 return; // -- can't do more for now, will parse after getting the labels.
44 }
45
46 this.reparseSeries();
47 };
48
49 /*
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.
52 */
53 DygraphOptions.AXIS_STRING_MAPPINGS_ = {
54 'y' : 0,
55 'Y' : 0,
56 'y1' : 0,
57 'Y1' : 0,
58 'y2' : 1,
59 'Y2' : 1
60 }
61
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];
66 }
67 throw "Unknown axis : " + text;
68 }
69 if (typeof(axis) == "number") {
70 if (axis == 0 || axis == 1) {
71 return axis;
72 }
73 throw "Dygraphs only supports two y-axes, indexed from 0-1."
74 }
75 if (axis) {
76 throw "Unknown axis : " + axis;
77 }
78 // No axis specification means axis 0.
79 return 0;
80 };
81
82 /**
83 * Reparses options that are all related to series. This typically occurs when
84 * options are either updated, or source data has been made avaialble.
85 *
86 * TODO(konigsberg): The method name is kind of weak; fix.
87 */
88 DygraphOptions.prototype.reparseSeries = function() {
89 this.labels = this.get("labels").slice(1);
90
91 this.axes_ = [ {} ]; // Always one axis at least.
92 this.series_ = {};
93
94 // Traditionally, per-series options were specified right up there with the options. For instance
95 // {
96 // labels: [ "X", "foo", "bar" ],
97 // pointSize: 3,
98 // foo : {}, // options for foo
99 // bar : {} // options for bar
100 // }
101 //
102 // Moving forward, series really should be specified in the series element, separating them.
103 // like so:
104 //
105 // {
106 // labels: [ "X", "foo", "bar" ],
107 // pointSize: 3,
108 // series : {
109 // foo : {}, // options for foo
110 // bar : {} // options for bar
111 // }
112 // }
113 //
114 // So, if series is found, it's expected to contain per-series data, otherwise we fall
115 // back.
116 var oldStyleSeries = !this.user_["series"];
117
118 if (oldStyleSeries) {
119 var axisId = 0; // 0-offset; there's always one.
120 // Go through once, add all the series, and for those with {} axis options, add a new axis.
121 for (var idx = 0; idx < this.labels.length; idx++) {
122 var seriesName = this.labels[idx];
123
124 var optionsForSeries = this.user_[seriesName] || {};
125
126 var yAxis = 0;
127 var axis = optionsForSeries["axis"];
128 if (typeof(axis) == 'object') {
129 yAxis = ++axisId;
130 this.axes_[yAxis] = axis;
131 }
132 this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries };
133 }
134
135 // Go through one more time and assign series to an axis defined by another
136 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
137 for (var idx = 0; idx < this.labels.length; idx++) {
138 var seriesName = this.labels[idx];
139 var optionsForSeries = this.series_[seriesName]["options"];
140 var axis = optionsForSeries["axis"];
141
142 if (typeof(axis) == 'string') {
143 if (!this.series_.hasOwnProperty(axis)) {
144 this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " +
145 "series " + axis + ", which does not define its own axis.");
146 return null;
147 }
148 this.series_[seriesName].yAxis = this.series_[axis].yAxis;
149 }
150 }
151 } else {
152 var maxYAxis = 0;
153
154 for (var idx = 0; idx < this.labels.length; idx++) {
155 var seriesName = this.labels[idx];
156 var optionsForSeries = this.user_.series[seriesName] || {};
157 var yAxis = DygraphOptions.axisToIndex_(optionsForSeries["axis"]);
158
159 maxYAxis = Math.max(yAxis, maxYAxis);
160
161 this.series_[seriesName] = {
162 idx: idx,
163 yAxis: yAxis,
164 options : optionsForSeries };
165 }
166
167 for (; maxYAxis >= 0; maxYAxis--) {
168 this.axes_[maxYAxis] = {};
169 }
170 }
171
172 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
173 if (this.user_["axes"]) {
174 var axis_opts = this.user_.axes;
175
176 if (axis_opts.hasOwnProperty("y")) {
177 Dygraph.update(this.axes_[0], axis_opts.y);
178 }
179
180 if (axis_opts.hasOwnProperty("y2")) {
181 this.axes_[1] = this.axes_[1] || {};
182 Dygraph.update(this.axes_[1], axis_opts.y2);
183 }
184 }
185 };
186
187 /**
188 * Get a global value.
189 *
190 * @param {String} name the name of the option.
191 */
192 DygraphOptions.prototype.get = function(name) {
193 if (this.user_.hasOwnProperty(name)) {
194 return this.user_[name];
195 }
196 if (this.global_.hasOwnProperty(name)) {
197 return this.global_[name];
198 }
199 return null;
200 };
201
202 /**
203 * Get a value for a specific axis. If there is no specific value for the axis,
204 * the global value is returned.
205 *
206 * @param {String} name the name of the option.
207 * @param {String|number} axis the axis to search. Can be the string representation
208 * ("y", "y2") or the axis number (0, 1).
209 */
210 DygraphOptions.prototype.getForAxis = function(name, axis) {
211 var axisIdx = 0;
212 if (typeof(axis) == 'number') {
213 axisIdx = axis;
214 } else {
215 // TODO(konigsberg): Accept only valid axis strings?
216 axisIdx = (axis == "y2") ? 1 : 0;
217 }
218
219 var axisOptions = this.axes_[axisIdx];
220 if (axisOptions.hasOwnProperty(name)) {
221 return axisOptions[name];
222 }
223 return this.get(name);
224 };
225
226 /**
227 * Get a value for a specific series. If there is no specific value for the series,
228 * the value for the axis is returned (and afterwards, the global value.)
229 *
230 * @param {String} name the name of the option.
231 * @param {String|number} series the series to search. Can be the string representation
232 * or 0-offset series number.
233 */
234 DygraphOptions.prototype.getForSeries = function(name, series) {
235 // Honors indexes as series.
236 var seriesName = (typeof(series) == "number") ? this.labels[series] : series;
237
238 if (seriesName === this.dygraph_.highlightSet_) {
239 if (this.highlightSeries_.hasOwnProperty(name)) {
240 return this.highlightSeries_[name];
241 }
242 }
243
244 if (!this.series_.hasOwnProperty(seriesName)) {
245 throw "Unknown series: " + series;
246 }
247
248 var seriesObj = this.series_[seriesName];
249 var seriesOptions = seriesObj["options"];
250 if (seriesOptions.hasOwnProperty(name)) {
251 return seriesOptions[name];
252 }
253
254 return this.getForAxis(name, seriesObj["yAxis"]);
255 };