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