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