exclude externs from lint
[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.
12 * global - global attributes (common among all graphs, AIUI)
ed30673a 13 * user - attributes set by the user
c1780ad0
RK
14 * axes
15 * series - { seriesName -> { idx, yAxis, options }
16 * labels - used as mapping from index to series name.
17 */
18
19/**
c1780ad0
RK
20 * This parses attributes into an object that can be easily queried.
21 *
22 * @param {Dyraph} dygraph The chart to which these options belong.
d67a4279 23 * @constructor
c1780ad0
RK
24 */
25var DygraphOptions = function(dygraph) {
26 this.dygraph_ = dygraph;
ed30673a
RK
27 this.axes_ = [];
28 this.series_ = {};
c1780ad0
RK
29
30 // Once these two objects are initialized, you can call find();
ed30673a
RK
31 this.global_ = this.dygraph_.attrs_;
32 this.user_ = this.dygraph_.user_attrs_ || {};
c1780ad0 33
ed30673a 34 this.highlightSeries_ = this.find("highlightSeriesOpts") || {};
c1780ad0 35 // Get a list of series names.
34825ef5
RK
36
37 var labels = this.find("labels");
38 if (!labels) {
39 return; // -- can't do more for now, will parse after getting the labels.
40 };
41
dd724e22 42 this.reparseSeries();
34825ef5
RK
43}
44
45DygraphOptions.prototype.reparseSeries = function() {
c1780ad0
RK
46 this.labels = this.find("labels").slice(1);
47
ed30673a
RK
48 this.axes_ = [ {} ]; // Always one axis at least.
49 this.series_ = {};
eb0da59f 50
c1780ad0
RK
51 var axisId = 0; // 0-offset; there's always one.
52 // Go through once, add all the series, and for those with {} axis options, add a new axis.
53 for (var idx = 0; idx < this.labels.length; idx++) {
54 var seriesName = this.labels[idx];
55
ed30673a 56 var optionsForSeries = this.user_[seriesName] || {};
c1780ad0
RK
57 var yAxis = 0;
58
59 var axis = optionsForSeries["axis"];
60 if (typeof(axis) == 'object') {
61 yAxis = ++axisId;
ed30673a 62 this.axes_[yAxis] = axis;
c1780ad0 63 }
ed30673a 64 this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries };
c1780ad0
RK
65 }
66
67 // Go through one more time and assign series to an axis defined by another
68 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
69 for (var idx = 0; idx < this.labels.length; idx++) {
70 var seriesName = this.labels[idx];
ed30673a 71 var optionsForSeries = this.series_[seriesName]["options"];
c1780ad0
RK
72 var axis = optionsForSeries["axis"];
73
74 if (typeof(axis) == 'string') {
ed30673a 75 if (!this.series_.hasOwnProperty(axis)) {
c1780ad0
RK
76 this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " +
77 "series " + axis + ", which does not define its own axis.");
78 return null;
79 }
ed30673a 80 this.series_[seriesName].yAxis = this.series_[axis].yAxis;
c1780ad0
RK
81 }
82 }
83
84 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
85 // Read from the global "axes" option.
ed30673a
RK
86 if (this.user_.hasOwnProperty("axes")) {
87 var axis_opts = this.user_.axes;
c1780ad0 88
eb0da59f 89 if (axis_opts.hasOwnProperty("y")) {
ed30673a 90 Dygraph.update(this.axes_[0], axis_opts.y);
eb0da59f
RK
91 }
92
93 if (axis_opts.hasOwnProperty("y2")) {
ed30673a
RK
94 this.axes_[1] = this.axes_[1] || {};
95 Dygraph.update(this.axes_[1], axis_opts.y2);
eb0da59f 96 }
c1780ad0
RK
97 }
98};
99
100DygraphOptions.prototype.find = function(name) {
ed30673a
RK
101 if (this.user_.hasOwnProperty(name)) {
102 return this.user_[name];
c1780ad0 103 }
ed30673a
RK
104 if (this.global_.hasOwnProperty(name)) {
105 return this.global_[name];
c1780ad0
RK
106 }
107 return null;
108}
109
110DygraphOptions.prototype.findForAxis = function(name, axis) {
dd724e22 111 var axisIdx = (axis == "y2" || axis == "y2" || axis == 1) ? 1 : 0;
c1780ad0 112
ed30673a 113 var axisOptions = this.axes_[axisIdx];
c1780ad0
RK
114 if (axisOptions.hasOwnProperty(name)) {
115 return axisOptions[name];
116 }
117 return this.find(name);
118}
119
120DygraphOptions.prototype.findForSeries = function(name, series) {
121 // Honors indexes as series.
122 var seriesName = (typeof(series) == "number") ? this.labels[series] : series;
123
ed30673a
RK
124 if (seriesName === this.dygraph_.highlightSet_) {
125 if (this.highlightSeries_.hasOwnProperty(name)) {
126 return this.highlightSeries_[name];
127 }
128 }
129
130 if (!this.series_.hasOwnProperty(seriesName)) {
c1780ad0
RK
131 throw "Unknown series: " + series;
132 }
133
ed30673a 134 var seriesObj = this.series_[seriesName];
c1780ad0
RK
135 var seriesOptions = seriesObj["options"];
136 if (seriesOptions.hasOwnProperty(name)) {
137 return seriesOptions[name];
138 }
ed30673a 139
c1780ad0
RK
140 return this.findForAxis(name, seriesObj["yAxis"]);
141}
142