Commit | Line | Data |
---|---|---|
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 | /** | |
20 | * @constructor | |
21 | * | |
22 | * This parses attributes into an object that can be easily queried. | |
23 | * | |
24 | * @param {Dyraph} dygraph The chart to which these options belong. | |
25 | */ | |
26 | var DygraphOptions = function(dygraph) { | |
27 | this.dygraph_ = dygraph; | |
ed30673a RK |
28 | this.axes_ = []; |
29 | this.series_ = {}; | |
c1780ad0 RK |
30 | |
31 | // Once these two objects are initialized, you can call find(); | |
ed30673a RK |
32 | this.global_ = this.dygraph_.attrs_; |
33 | this.user_ = this.dygraph_.user_attrs_ || {}; | |
c1780ad0 | 34 | |
ed30673a | 35 | this.highlightSeries_ = this.find("highlightSeriesOpts") || {}; |
c1780ad0 | 36 | // Get a list of series names. |
34825ef5 RK |
37 | |
38 | var labels = this.find("labels"); | |
39 | if (!labels) { | |
40 | return; // -- can't do more for now, will parse after getting the labels. | |
41 | }; | |
42 | ||
dd724e22 | 43 | this.reparseSeries(); |
34825ef5 RK |
44 | } |
45 | ||
46 | DygraphOptions.prototype.reparseSeries = function() { | |
c1780ad0 RK |
47 | this.labels = this.find("labels").slice(1); |
48 | ||
ed30673a RK |
49 | this.axes_ = [ {} ]; // Always one axis at least. |
50 | this.series_ = {}; | |
eb0da59f | 51 | |
73e953cd RK |
52 | // Traditionally, per-series options were specified right up there with the options. For instance |
53 | // { | |
54 | // labels: [ "X", "foo", "bar" ], | |
55 | // pointSize: 3, | |
56 | // foo : {}, // options for foo | |
57 | // bar : {} // options for bar | |
58 | // } | |
59 | // | |
60 | // Moving forward, series really should be specified in the series element, separating them. | |
61 | // like so: | |
62 | // | |
63 | // { | |
64 | // labels: [ "X", "foo", "bar" ], | |
65 | // pointSize: 3, | |
66 | // series : { | |
67 | // foo : {}, // options for foo | |
68 | // bar : {} // options for bar | |
69 | // } | |
70 | // } | |
71 | // | |
72 | // So, if series is found, it's expected to contain per-series data, otherwise we fall | |
73 | // back. | |
74 | var allseries = this.user_.hasOwnProperty("series") ? this.user_.series : this.user_; | |
75 | ||
c1780ad0 RK |
76 | var axisId = 0; // 0-offset; there's always one. |
77 | // Go through once, add all the series, and for those with {} axis options, add a new axis. | |
78 | for (var idx = 0; idx < this.labels.length; idx++) { | |
79 | var seriesName = this.labels[idx]; | |
80 | ||
73e953cd | 81 | var optionsForSeries = allseries[seriesName] || {}; |
c1780ad0 RK |
82 | var yAxis = 0; |
83 | ||
84 | var axis = optionsForSeries["axis"]; | |
85 | if (typeof(axis) == 'object') { | |
86 | yAxis = ++axisId; | |
ed30673a | 87 | this.axes_[yAxis] = axis; |
c1780ad0 | 88 | } |
ed30673a | 89 | this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; |
c1780ad0 RK |
90 | } |
91 | ||
92 | // Go through one more time and assign series to an axis defined by another | |
93 | // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } | |
94 | for (var idx = 0; idx < this.labels.length; idx++) { | |
95 | var seriesName = this.labels[idx]; | |
ed30673a | 96 | var optionsForSeries = this.series_[seriesName]["options"]; |
c1780ad0 RK |
97 | var axis = optionsForSeries["axis"]; |
98 | ||
99 | if (typeof(axis) == 'string') { | |
ed30673a | 100 | if (!this.series_.hasOwnProperty(axis)) { |
c1780ad0 RK |
101 | this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " + |
102 | "series " + axis + ", which does not define its own axis."); | |
103 | return null; | |
104 | } | |
ed30673a | 105 | this.series_[seriesName].yAxis = this.series_[axis].yAxis; |
c1780ad0 RK |
106 | } |
107 | } | |
108 | ||
109 | // This doesn't support reading from the 'x' axis, only 'y' and 'y2. | |
110 | // Read from the global "axes" option. | |
ed30673a RK |
111 | if (this.user_.hasOwnProperty("axes")) { |
112 | var axis_opts = this.user_.axes; | |
c1780ad0 | 113 | |
eb0da59f | 114 | if (axis_opts.hasOwnProperty("y")) { |
ed30673a | 115 | Dygraph.update(this.axes_[0], axis_opts.y); |
eb0da59f RK |
116 | } |
117 | ||
118 | if (axis_opts.hasOwnProperty("y2")) { | |
ed30673a RK |
119 | this.axes_[1] = this.axes_[1] || {}; |
120 | Dygraph.update(this.axes_[1], axis_opts.y2); | |
eb0da59f | 121 | } |
c1780ad0 RK |
122 | } |
123 | }; | |
124 | ||
125 | DygraphOptions.prototype.find = function(name) { | |
ed30673a RK |
126 | if (this.user_.hasOwnProperty(name)) { |
127 | return this.user_[name]; | |
c1780ad0 | 128 | } |
ed30673a RK |
129 | if (this.global_.hasOwnProperty(name)) { |
130 | return this.global_[name]; | |
c1780ad0 RK |
131 | } |
132 | return null; | |
133 | } | |
134 | ||
135 | DygraphOptions.prototype.findForAxis = function(name, axis) { | |
dd724e22 | 136 | var axisIdx = (axis == "y2" || axis == "y2" || axis == 1) ? 1 : 0; |
c1780ad0 | 137 | |
ed30673a | 138 | var axisOptions = this.axes_[axisIdx]; |
c1780ad0 RK |
139 | if (axisOptions.hasOwnProperty(name)) { |
140 | return axisOptions[name]; | |
141 | } | |
142 | return this.find(name); | |
143 | } | |
144 | ||
145 | DygraphOptions.prototype.findForSeries = function(name, series) { | |
146 | // Honors indexes as series. | |
147 | var seriesName = (typeof(series) == "number") ? this.labels[series] : series; | |
148 | ||
ed30673a RK |
149 | if (seriesName === this.dygraph_.highlightSet_) { |
150 | if (this.highlightSeries_.hasOwnProperty(name)) { | |
151 | return this.highlightSeries_[name]; | |
152 | } | |
153 | } | |
154 | ||
155 | if (!this.series_.hasOwnProperty(seriesName)) { | |
c1780ad0 RK |
156 | throw "Unknown series: " + series; |
157 | } | |
158 | ||
ed30673a | 159 | var seriesObj = this.series_[seriesName]; |
c1780ad0 RK |
160 | var seriesOptions = seriesObj["options"]; |
161 | if (seriesOptions.hasOwnProperty(name)) { | |
162 | return seriesOptions[name]; | |
163 | } | |
ed30673a | 164 | |
c1780ad0 RK |
165 | return this.findForAxis(name, seriesObj["yAxis"]); |
166 | } | |
167 |