Document the new "axis", not the old.
[dygraphs.git] / dygraph-options.js
CommitLineData
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
13var DygraphOptions = (function() {
14
8887663f
DV
15// For "production" code, this gets set to false by uglifyjs.
16// Need to define it outside of "use strict", hence the nested IIFEs.
17if (typeof(DEBUG) === 'undefined') DEBUG=true;
18
19return (function() {
20
384bfa4a 21/*jshint sub:true */
83b0c192 22/*global Dygraph:false */
c1780ad0
RK
23"use strict";
24
25/*
e321ff2a 26 * Interesting member variables: (REMOVING THIS LIST AS I CLOSURIZE)
673a3b87 27 * global_ - global attributes (common among all graphs, AIUI)
ed30673a 28 * user - attributes set by the user
242a8bc8 29 * series_ - { seriesName -> { idx, yAxis, options }}
c1780ad0
RK
30 */
31
32/**
c1780ad0
RK
33 * This parses attributes into an object that can be easily queried.
34 *
5daa462d
RK
35 * It doesn't necessarily mean that all options are available, specifically
36 * if labels are not yet available, since those drive details of the per-series
37 * and per-axis options.
38 *
e321ff2a 39 * @param {Dygraph} dygraph The chart to which these options belong.
d67a4279 40 * @constructor
c1780ad0
RK
41 */
42var DygraphOptions = function(dygraph) {
e321ff2a
RK
43 /**
44 * The dygraph.
34655aba 45 * @type {!Dygraph}
e321ff2a 46 */
c1780ad0 47 this.dygraph_ = dygraph;
e321ff2a
RK
48
49 /**
50 * Array of axis index to { series : [ series names ] , options : { axis-specific options. }
72621b9a 51 * @type {Array.<{series : Array.<string>, options : Object}>} @private
e321ff2a 52 */
48dc3815 53 this.yAxes_ = [];
e321ff2a
RK
54
55 /**
2ed3480b
RK
56 * Contains x-axis specific options, which are stored in the options key.
57 * This matches the yAxes_ object structure (by being a dictionary with an
58 * options element) allowing for shared code.
59 * @type {options: Object} @private
e321ff2a 60 */
48dc3815 61 this.xAxis_ = {};
ed30673a 62 this.series_ = {};
c1780ad0 63
5daa462d 64 // Once these two objects are initialized, you can call get();
ed30673a
RK
65 this.global_ = this.dygraph_.attrs_;
66 this.user_ = this.dygraph_.user_attrs_ || {};
c1780ad0 67
e321ff2a
RK
68 /**
69 * A list of series in columnar order.
70 * @type {Array.<string>}
71 */
72 this.labels_ = [];
73
5daa462d 74 this.highlightSeries_ = this.get("highlightSeriesOpts") || {};
dd724e22 75 this.reparseSeries();
5daa462d 76};
34825ef5 77
e321ff2a 78/**
632bd78c
RK
79 * Not optimal, but does the trick when you're only using two axes.
80 * If we move to more axes, this can just become a function.
e321ff2a 81 *
34655aba 82 * @type {Object.<number>}
e321ff2a 83 * @private
632bd78c
RK
84 */
85DygraphOptions.AXIS_STRING_MAPPINGS_ = {
86 'y' : 0,
87 'Y' : 0,
88 'y1' : 0,
89 'Y1' : 0,
90 'y2' : 1,
91 'Y2' : 1
04f2bce6 92};
632bd78c 93
3f265488
LB
94/**
95 * @param {string|number} axis
96 * @private
97 */
632bd78c
RK
98DygraphOptions.axisToIndex_ = function(axis) {
99 if (typeof(axis) == "string") {
100 if (DygraphOptions.AXIS_STRING_MAPPINGS_.hasOwnProperty(axis)) {
101 return DygraphOptions.AXIS_STRING_MAPPINGS_[axis];
102 }
04f2bce6 103 throw "Unknown axis : " + axis;
632bd78c
RK
104 }
105 if (typeof(axis) == "number") {
04f2bce6 106 if (axis === 0 || axis === 1) {
632bd78c
RK
107 return axis;
108 }
04f2bce6 109 throw "Dygraphs only supports two y-axes, indexed from 0-1.";
632bd78c
RK
110 }
111 if (axis) {
112 throw "Unknown axis : " + axis;
113 }
114 // No axis specification means axis 0.
115 return 0;
116};
117
5daa462d
RK
118/**
119 * Reparses options that are all related to series. This typically occurs when
2fd143d3 120 * options are either updated, or source data has been made available.
5daa462d
RK
121 *
122 * TODO(konigsberg): The method name is kind of weak; fix.
123 */
34825ef5 124DygraphOptions.prototype.reparseSeries = function() {
9d562235
PF
125 var labels = this.get("labels");
126 if (!labels) {
127 return; // -- can't do more for now, will parse after getting the labels.
128 }
129
e321ff2a 130 this.labels_ = labels.slice(1);
c1780ad0 131
48dc3815
RK
132 this.yAxes_ = [ { series : [], options : {}} ]; // Always one axis at least.
133 this.xAxis_ = { options : {} };
ed30673a 134 this.series_ = {};
eb0da59f 135
73e953cd
RK
136 // Traditionally, per-series options were specified right up there with the options. For instance
137 // {
138 // labels: [ "X", "foo", "bar" ],
139 // pointSize: 3,
140 // foo : {}, // options for foo
141 // bar : {} // options for bar
142 // }
143 //
144 // Moving forward, series really should be specified in the series element, separating them.
145 // like so:
146 //
147 // {
148 // labels: [ "X", "foo", "bar" ],
149 // pointSize: 3,
150 // series : {
151 // foo : {}, // options for foo
152 // bar : {} // options for bar
153 // }
154 // }
155 //
156 // So, if series is found, it's expected to contain per-series data, otherwise we fall
157 // back.
632bd78c 158 var oldStyleSeries = !this.user_["series"];
9d562235 159
632bd78c
RK
160 if (oldStyleSeries) {
161 var axisId = 0; // 0-offset; there's always one.
162 // Go through once, add all the series, and for those with {} axis options, add a new axis.
e321ff2a
RK
163 for (var idx = 0; idx < this.labels_.length; idx++) {
164 var seriesName = this.labels_[idx];
9d562235 165
632bd78c 166 var optionsForSeries = this.user_[seriesName] || {};
9d562235 167
632bd78c
RK
168 var yAxis = 0;
169 var axis = optionsForSeries["axis"];
170 if (typeof(axis) == 'object') {
171 yAxis = ++axisId;
48dc3815 172 this.yAxes_[yAxis] = { series : [ seriesName ], options : axis };
632bd78c 173 }
c1780ad0 174
6ad8b6a4
RK
175 // Associate series without axis options with axis 0.
176 if (!axis) { // undefined
48dc3815 177 this.yAxes_[0].series.push(seriesName);
6ad8b6a4 178 }
c1780ad0 179
632bd78c
RK
180 this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries };
181 }
9d562235 182
632bd78c
RK
183 // Go through one more time and assign series to an axis defined by another
184 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
e321ff2a
RK
185 for (var idx = 0; idx < this.labels_.length; idx++) {
186 var seriesName = this.labels_[idx];
9d562235 187 var optionsForSeries = this.series_[seriesName]["options"];
632bd78c 188 var axis = optionsForSeries["axis"];
9d562235 189
632bd78c
RK
190 if (typeof(axis) == 'string') {
191 if (!this.series_.hasOwnProperty(axis)) {
8a68db7d 192 console.error("Series " + seriesName + " wants to share a y-axis with " +
632bd78c 193 "series " + axis + ", which does not define its own axis.");
e1119361 194 return;
632bd78c 195 }
6ad8b6a4
RK
196 var yAxis = this.series_[axis].yAxis;
197 this.series_[seriesName].yAxis = yAxis;
48dc3815 198 this.yAxes_[yAxis].series.push(seriesName);
632bd78c
RK
199 }
200 }
201 } else {
e321ff2a
RK
202 for (var idx = 0; idx < this.labels_.length; idx++) {
203 var seriesName = this.labels_[idx];
632bd78c
RK
204 var optionsForSeries = this.user_.series[seriesName] || {};
205 var yAxis = DygraphOptions.axisToIndex_(optionsForSeries["axis"]);
c1780ad0 206
632bd78c
RK
207 this.series_[seriesName] = {
208 idx: idx,
209 yAxis: yAxis,
210 options : optionsForSeries };
c1780ad0 211
48dc3815
RK
212 if (!this.yAxes_[yAxis]) {
213 this.yAxes_[yAxis] = { series : [ seriesName ], options : {} };
6ad8b6a4 214 } else {
48dc3815 215 this.yAxes_[yAxis].series.push(seriesName);
c1780ad0 216 }
c1780ad0
RK
217 }
218 }
219
6ad8b6a4 220 var axis_opts = this.user_["axes"] || {};
48dc3815
RK
221 Dygraph.update(this.yAxes_[0].options, axis_opts["y"] || {});
222 if (this.yAxes_.length > 1) {
9d562235 223 Dygraph.update(this.yAxes_[1].options, axis_opts["y2"] || {});
c1780ad0 224 }
48dc3815 225 Dygraph.update(this.xAxis_.options, axis_opts["x"] || {});
8887663f
DV
226
227 if (DEBUG) this.validateOptions_();
c1780ad0
RK
228};
229
5daa462d
RK
230/**
231 * Get a global value.
232 *
a4aceb34 233 * @param {string} name the name of the option.
5daa462d
RK
234 */
235DygraphOptions.prototype.get = function(name) {
d574a45e 236 var result = this.getGlobalUser_(name);
2e3d8088 237 if (result !== null) {
d574a45e
RK
238 return result;
239 }
240 return this.getGlobalDefault_(name);
241};
242
243DygraphOptions.prototype.getGlobalUser_ = function(name) {
ed30673a
RK
244 if (this.user_.hasOwnProperty(name)) {
245 return this.user_[name];
c1780ad0 246 }
d574a45e
RK
247 return null;
248};
249
250DygraphOptions.prototype.getGlobalDefault_ = function(name) {
ed30673a
RK
251 if (this.global_.hasOwnProperty(name)) {
252 return this.global_[name];
c1780ad0 253 }
d574a45e
RK
254 if (Dygraph.DEFAULT_ATTRS.hasOwnProperty(name)) {
255 return Dygraph.DEFAULT_ATTRS[name];
256 }
c1780ad0 257 return null;
83b0c192 258};
c1780ad0 259
5daa462d
RK
260/**
261 * Get a value for a specific axis. If there is no specific value for the axis,
262 * the global value is returned.
263 *
a4aceb34
RK
264 * @param {string} name the name of the option.
265 * @param {string|number} axis the axis to search. Can be the string representation
5daa462d
RK
266 * ("y", "y2") or the axis number (0, 1).
267 */
268DygraphOptions.prototype.getForAxis = function(name, axis) {
48dc3815
RK
269 var axisIdx;
270 var axisString;
271
272 // Since axis can be a number or a string, straighten everything out here.
5daa462d
RK
273 if (typeof(axis) == 'number') {
274 axisIdx = axis;
2e3d8088 275 axisString = axisIdx === 0 ? "y" : "y2";
5daa462d 276 } else {
48dc3815
RK
277 if (axis == "y1") { axis = "y"; } // Standardize on 'y'. Is this bad? I think so.
278 if (axis == "y") {
279 axisIdx = 0;
280 } else if (axis == "y2") {
281 axisIdx = 1;
282 } else if (axis == "x") {
283 axisIdx = -1; // simply a placeholder for below.
284 } else {
285 throw "Unknown axis " + axis;
286 }
287 axisString = axis;
5daa462d 288 }
48dc3815
RK
289
290 var userAxis = (axisIdx == -1) ? this.xAxis_ : this.yAxes_[axisIdx];
291
d574a45e 292 // Search the user-specified axis option first.
48dc3815
RK
293 if (userAxis) { // This condition could be removed if we always set up this.yAxes_ for y2.
294 var axisOptions = userAxis.options;
d574a45e
RK
295 if (axisOptions.hasOwnProperty(name)) {
296 return axisOptions[name];
297 }
298 }
c1780ad0 299
d574a45e 300 // User-specified global options second.
5b9b2142
RK
301 // But, hack, ignore globally-specified 'logscale' for 'x' axis declaration.
302 if (!(axis === 'x' && name === 'logscale')) {
303 var result = this.getGlobalUser_(name);
304 if (result !== null) {
305 return result;
306 }
c1780ad0 307 }
d574a45e 308 // Default axis options third.
d574a45e
RK
309 var defaultAxisOptions = Dygraph.DEFAULT_ATTRS.axes[axisString];
310 if (defaultAxisOptions.hasOwnProperty(name)) {
311 return defaultAxisOptions[name];
312 }
313
314 // Default global options last.
315 return this.getGlobalDefault_(name);
5daa462d 316};
c1780ad0 317
5daa462d
RK
318/**
319 * Get a value for a specific series. If there is no specific value for the series,
320 * the value for the axis is returned (and afterwards, the global value.)
321 *
a4aceb34 322 * @param {string} name the name of the option.
8e19509a 323 * @param {string} series the series to search.
5daa462d
RK
324 */
325DygraphOptions.prototype.getForSeries = function(name, series) {
c1780ad0 326 // Honors indexes as series.
3f265488 327 if (series === this.dygraph_.getHighlightSeries()) {
ed30673a
RK
328 if (this.highlightSeries_.hasOwnProperty(name)) {
329 return this.highlightSeries_[name];
330 }
331 }
332
8e19509a 333 if (!this.series_.hasOwnProperty(series)) {
c1780ad0
RK
334 throw "Unknown series: " + series;
335 }
336
8e19509a 337 var seriesObj = this.series_[series];
c1780ad0
RK
338 var seriesOptions = seriesObj["options"];
339 if (seriesOptions.hasOwnProperty(name)) {
340 return seriesOptions[name];
341 }
ed30673a 342
5daa462d
RK
343 return this.getForAxis(name, seriesObj["yAxis"]);
344};
c1780ad0 345
673a3b87
RK
346/**
347 * Returns the number of y-axes on the chart.
e321ff2a 348 * @return {number} the number of axes.
673a3b87
RK
349 */
350DygraphOptions.prototype.numAxes = function() {
48dc3815 351 return this.yAxes_.length;
04f2bce6 352};
16f00742
RK
353
354/**
355 * Return the y-axis for a given series, specified by name.
356 */
8e19509a
RK
357DygraphOptions.prototype.axisForSeries = function(series) {
358 return this.series_[series].yAxis;
04f2bce6 359};
16f00742
RK
360
361/**
362 * Returns the options for the specified axis.
363 */
48dc3815 364// TODO(konigsberg): this is y-axis specific. Support the x axis.
16f00742 365DygraphOptions.prototype.axisOptions = function(yAxis) {
48dc3815 366 return this.yAxes_[yAxis].options;
04f2bce6 367};
16f00742
RK
368
369/**
370 * Return the series associated with an axis.
371 */
372DygraphOptions.prototype.seriesForAxis = function(yAxis) {
48dc3815 373 return this.yAxes_[yAxis].series;
04f2bce6 374};
6ad8b6a4
RK
375
376/**
377 * Return the list of all series, in their columnar order.
378 */
379DygraphOptions.prototype.seriesNames = function() {
380 return this.labels_;
04f2bce6 381};
6ad8b6a4 382
8887663f
DV
383if (DEBUG) {
384
385/**
386 * Validate all options.
387 * This requires Dygraph.OPTIONS_REFERENCE, which is only available in debug builds.
388 * @private
389 */
390DygraphOptions.prototype.validateOptions_ = function() {
391 if (typeof Dygraph.OPTIONS_REFERENCE === 'undefined') {
392 throw 'Called validateOptions_ in prod build.';
393 }
394
395 var that = this;
396 var validateOption = function(optionName) {
397 if (!Dygraph.OPTIONS_REFERENCE[optionName]) {
398 that.warnInvalidOption_(optionName);
399 }
400 };
401
402 var optionsDicts = [this.xAxis_.options,
403 this.yAxes_[0].options,
404 this.yAxes_[1] && this.yAxes_[1].options,
405 this.global_,
406 this.user_,
407 this.highlightSeries_];
408 for (var i = 0; i < optionsDicts.length; i++) {
409 var dict = optionsDicts[i];
410 if (!dict) continue;
411 for (var optionName in dict) {
412 if (dict.hasOwnProperty(optionName)) {
413 validateOption(optionName);
414 }
415 }
416 }
417};
418
419var WARNINGS = {}; // Only show any particular warning once.
420
421/**
422 * Logs a warning about invalid options.
423 * TODO: make this throw for testing
424 * @private
425 */
426DygraphOptions.prototype.warnInvalidOption_ = function(optionName) {
427 if (!WARNINGS[optionName]) {
428 var isSeries = (this.labels_.indexOf(optionName) >= 0);
429 if (isSeries) {
430 console.warn('Use new-style per-series options (saw ' + optionName + ' as top-level options key). See http://bit.ly/1tceaJs');
431 } else {
432 console.warn('Unknown option ' + optionName + ' (full list of options at dygraphs.com/options.html');
433 // throw "invalid option " + optionName;
434 }
435 WARNINGS[optionName] = true;
436 }
437};
438
439}
440
384bfa4a
DV
441return DygraphOptions;
442
443})();
8887663f 444})();