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) | |
13 | * global_user - attributes set by the user | |
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; | |
28 | this.axes = []; | |
29 | this.series = {}; | |
30 | ||
31 | // Once these two objects are initialized, you can call find(); | |
32 | this.global = this.dygraph_.attrs_; | |
33 | this.global_user = this.dygraph_.user_attrs_ || {}; | |
34 | ||
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 | ||
45 | DygraphOptions.prototype.reparseSeries = function() { | |
c1780ad0 RK |
46 | this.labels = this.find("labels").slice(1); |
47 | ||
eb0da59f RK |
48 | this.axes = [ {} ]; // Always one axis at least. |
49 | this.series = {}; | |
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 | ||
56 | var optionsForSeries = this.global_user[seriesName] || {}; | |
57 | var yAxis = 0; | |
58 | ||
59 | var axis = optionsForSeries["axis"]; | |
60 | if (typeof(axis) == 'object') { | |
61 | yAxis = ++axisId; | |
eb0da59f | 62 | this.axes[yAxis] = axis; |
c1780ad0 RK |
63 | } |
64 | this.series[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; | |
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]; | |
71 | var optionsForSeries = this.series[seriesName]["options"]; | |
72 | var axis = optionsForSeries["axis"]; | |
73 | ||
74 | if (typeof(axis) == 'string') { | |
75 | if (!this.series.hasOwnProperty(axis)) { | |
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 | } | |
80 | this.series[seriesName].yAxis = this.series[axis].yAxis; | |
81 | } | |
82 | } | |
83 | ||
84 | // This doesn't support reading from the 'x' axis, only 'y' and 'y2. | |
85 | // Read from the global "axes" option. | |
86 | if (this.global_user.hasOwnProperty("axes")) { | |
87 | var axis_opts = this.global_user.axes; | |
88 | ||
eb0da59f RK |
89 | if (axis_opts.hasOwnProperty("y")) { |
90 | Dygraph.update(this.axes[0], axis_opts.y); | |
91 | } | |
92 | ||
93 | if (axis_opts.hasOwnProperty("y2")) { | |
94 | this.axes[1] = this.axes[1] || {}; | |
95 | Dygraph.update(this.axes[1], axis_opts.y2); | |
96 | } | |
c1780ad0 RK |
97 | } |
98 | }; | |
99 | ||
100 | DygraphOptions.prototype.find = function(name) { | |
101 | if (this.global_user.hasOwnProperty(name)) { | |
102 | return this.global_user[name]; | |
103 | } | |
104 | if (this.global.hasOwnProperty(name)) { | |
105 | return this.global[name]; | |
106 | } | |
107 | return null; | |
108 | } | |
109 | ||
110 | DygraphOptions.prototype.findForAxis = function(name, axis) { | |
111 | ||
dd724e22 | 112 | var axisIdx = (axis == "y2" || axis == "y2" || axis == 1) ? 1 : 0; |
c1780ad0 RK |
113 | |
114 | var axisOptions = this.axes[axisIdx]; | |
115 | if (axisOptions.hasOwnProperty(name)) { | |
116 | return axisOptions[name]; | |
117 | } | |
118 | return this.find(name); | |
119 | } | |
120 | ||
121 | DygraphOptions.prototype.findForSeries = function(name, series) { | |
122 | // Honors indexes as series. | |
123 | var seriesName = (typeof(series) == "number") ? this.labels[series] : series; | |
124 | ||
125 | if (!this.series.hasOwnProperty(seriesName)) { | |
126 | throw "Unknown series: " + series; | |
127 | } | |
128 | ||
129 | var seriesObj = this.series[seriesName]; | |
130 | var seriesOptions = seriesObj["options"]; | |
131 | if (seriesOptions.hasOwnProperty(name)) { | |
132 | return seriesOptions[name]; | |
133 | } | |
134 | return this.findForAxis(name, seriesObj["yAxis"]); | |
135 | } | |
136 |