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