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