Rename test and test methods so they do not conflict with multiple-axes.js.
[dygraphs.git] / dygraph-options.js
CommitLineData
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.
673a3b87 12 * global_ - global attributes (common among all graphs, AIUI)
ed30673a 13 * user - attributes set by the user
16f00742 14 * axes_ - array of axis index to { series : [ series names ] , options : { axis-specific options. }
242a8bc8 15 * series_ - { seriesName -> { idx, yAxis, options }}
673a3b87 16 * labels_ - used as mapping from index to series name.
c1780ad0
RK
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 */
29var 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
632bd78c
RK
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 */
53DygraphOptions.AXIS_STRING_MAPPINGS_ = {
54 'y' : 0,
55 'Y' : 0,
56 'y1' : 0,
57 'Y1' : 0,
58 'y2' : 1,
59 'Y2' : 1
60}
61
62DygraphOptions.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
5daa462d
RK
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 */
34825ef5 88DygraphOptions.prototype.reparseSeries = function() {
5daa462d 89 this.labels = this.get("labels").slice(1);
c1780ad0 90
16f00742 91 this.axes_ = [ { series : [], options : {}} ]; // Always one axis at least.
ed30673a 92 this.series_ = {};
eb0da59f 93
73e953cd
RK
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.
632bd78c
RK
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;
6ad8b6a4 130 this.axes_[yAxis] = { series : [ seriesName ], options : axis };
632bd78c 131 }
c1780ad0 132
6ad8b6a4
RK
133 // Associate series without axis options with axis 0.
134 if (!axis) { // undefined
135 this.axes_[0].series.push(seriesName);
136 }
c1780ad0 137
632bd78c
RK
138 this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries };
139 }
140
141 // Go through one more time and assign series to an axis defined by another
142 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
143 for (var idx = 0; idx < this.labels.length; idx++) {
144 var seriesName = this.labels[idx];
145 var optionsForSeries = this.series_[seriesName]["options"];
146 var axis = optionsForSeries["axis"];
147
148 if (typeof(axis) == 'string') {
149 if (!this.series_.hasOwnProperty(axis)) {
150 this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " +
151 "series " + axis + ", which does not define its own axis.");
152 return null;
153 }
6ad8b6a4
RK
154 var yAxis = this.series_[axis].yAxis;
155 this.series_[seriesName].yAxis = yAxis;
156 this.axes_[yAxis].series.push(seriesName);
632bd78c
RK
157 }
158 }
159 } else {
632bd78c
RK
160 for (var idx = 0; idx < this.labels.length; idx++) {
161 var seriesName = this.labels[idx];
162 var optionsForSeries = this.user_.series[seriesName] || {};
163 var yAxis = DygraphOptions.axisToIndex_(optionsForSeries["axis"]);
c1780ad0 164
632bd78c
RK
165 this.series_[seriesName] = {
166 idx: idx,
167 yAxis: yAxis,
168 options : optionsForSeries };
c1780ad0 169
6ad8b6a4
RK
170 if (!this.axes_[yAxis]) {
171 this.axes_[yAxis] = { series : [ seriesName ], options : {} };
172 } else {
173 this.axes_[yAxis].series.push(seriesName);
c1780ad0 174 }
c1780ad0
RK
175 }
176 }
177
178 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
6ad8b6a4
RK
179 var axis_opts = this.user_["axes"] || {};
180 Dygraph.update(this.axes_[0].options, axis_opts["y"] || {});
181 if (this.axes_.length > 1) {
182 Dygraph.update(this.axes_[1].options, axis_opts["y2"] || {});
c1780ad0
RK
183 }
184};
185
5daa462d
RK
186/**
187 * Get a global value.
188 *
189 * @param {String} name the name of the option.
190 */
191DygraphOptions.prototype.get = function(name) {
ed30673a
RK
192 if (this.user_.hasOwnProperty(name)) {
193 return this.user_[name];
c1780ad0 194 }
ed30673a
RK
195 if (this.global_.hasOwnProperty(name)) {
196 return this.global_[name];
c1780ad0
RK
197 }
198 return null;
5daa462d 199};
c1780ad0 200
5daa462d
RK
201/**
202 * Get a value for a specific axis. If there is no specific value for the axis,
203 * the global value is returned.
204 *
205 * @param {String} name the name of the option.
206 * @param {String|number} axis the axis to search. Can be the string representation
207 * ("y", "y2") or the axis number (0, 1).
208 */
209DygraphOptions.prototype.getForAxis = function(name, axis) {
210 var axisIdx = 0;
211 if (typeof(axis) == 'number') {
212 axisIdx = axis;
213 } else {
214 // TODO(konigsberg): Accept only valid axis strings?
215 axisIdx = (axis == "y2") ? 1 : 0;
216 }
c1780ad0 217
16f00742 218 var axisOptions = this.axes_[axisIdx].options;
c1780ad0
RK
219 if (axisOptions.hasOwnProperty(name)) {
220 return axisOptions[name];
221 }
5daa462d
RK
222 return this.get(name);
223};
c1780ad0 224
5daa462d
RK
225/**
226 * Get a value for a specific series. If there is no specific value for the series,
227 * the value for the axis is returned (and afterwards, the global value.)
228 *
229 * @param {String} name the name of the option.
230 * @param {String|number} series the series to search. Can be the string representation
231 * or 0-offset series number.
232 */
233DygraphOptions.prototype.getForSeries = function(name, series) {
c1780ad0
RK
234 // Honors indexes as series.
235 var seriesName = (typeof(series) == "number") ? this.labels[series] : series;
236
ed30673a
RK
237 if (seriesName === this.dygraph_.highlightSet_) {
238 if (this.highlightSeries_.hasOwnProperty(name)) {
239 return this.highlightSeries_[name];
240 }
241 }
242
243 if (!this.series_.hasOwnProperty(seriesName)) {
c1780ad0
RK
244 throw "Unknown series: " + series;
245 }
246
ed30673a 247 var seriesObj = this.series_[seriesName];
c1780ad0
RK
248 var seriesOptions = seriesObj["options"];
249 if (seriesOptions.hasOwnProperty(name)) {
250 return seriesOptions[name];
251 }
ed30673a 252
5daa462d
RK
253 return this.getForAxis(name, seriesObj["yAxis"]);
254};
c1780ad0 255
673a3b87
RK
256/**
257 * Returns the number of y-axes on the chart.
258 * @return {Number} the number of axes.
259 */
260DygraphOptions.prototype.numAxes = function() {
261 return this.axes_.length;
262}
16f00742
RK
263
264/**
265 * Return the y-axis for a given series, specified by name.
266 */
267DygraphOptions.prototype.axisForSeries = function(seriesName) {
268 return this.series_[seriesName].yAxis;
269}
270
271/**
272 * Returns the options for the specified axis.
273 */
274DygraphOptions.prototype.axisOptions = function(yAxis) {
275 return this.axes_[yAxis].options;
276}
277
278/**
279 * Return the series associated with an axis.
280 */
281DygraphOptions.prototype.seriesForAxis = function(yAxis) {
282 return this.axes_[yAxis].series;
283}
6ad8b6a4
RK
284
285/**
286 * Return the list of all series, in their columnar order.
287 */
288DygraphOptions.prototype.seriesNames = function() {
289 return this.labels_;
290}
291
292/* Are we using this? */
293/**
294 * Return the index of the specified series.
295 * @param {string} series the series name.
296 */
297DygraphOptions.prototype.indexOfSeries = function(series) {
298 return this.series_[series].idx;
299}