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. | |
36 | this.labels = this.find("labels").slice(1); | |
37 | ||
38 | var axisId = 0; // 0-offset; there's always one. | |
39 | // Go through once, add all the series, and for those with {} axis options, add a new axis. | |
40 | for (var idx = 0; idx < this.labels.length; idx++) { | |
41 | var seriesName = this.labels[idx]; | |
42 | ||
43 | var optionsForSeries = this.global_user[seriesName] || {}; | |
44 | var yAxis = 0; | |
45 | ||
46 | var axis = optionsForSeries["axis"]; | |
47 | if (typeof(axis) == 'object') { | |
48 | yAxis = ++axisId; | |
49 | } | |
50 | this.series[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; | |
51 | } | |
52 | ||
53 | // Go through one more time and assign series to an axis defined by another | |
54 | // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } | |
55 | for (var idx = 0; idx < this.labels.length; idx++) { | |
56 | var seriesName = this.labels[idx]; | |
57 | var optionsForSeries = this.series[seriesName]["options"]; | |
58 | var axis = optionsForSeries["axis"]; | |
59 | ||
60 | if (typeof(axis) == 'string') { | |
61 | if (!this.series.hasOwnProperty(axis)) { | |
62 | this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " + | |
63 | "series " + axis + ", which does not define its own axis."); | |
64 | return null; | |
65 | } | |
66 | this.series[seriesName].yAxis = this.series[axis].yAxis; | |
67 | } | |
68 | } | |
69 | ||
70 | // This doesn't support reading from the 'x' axis, only 'y' and 'y2. | |
71 | // Read from the global "axes" option. | |
72 | if (this.global_user.hasOwnProperty("axes")) { | |
73 | var axis_opts = this.global_user.axes; | |
74 | ||
75 | this.axes.push(axis_opts["y"] || {}); | |
76 | this.axes.push(axis_opts["y2"] || {}); | |
77 | } else { | |
78 | this.axes.push(axis_opts["y"] || {}); // There has to be at least one axis. | |
79 | } | |
80 | }; | |
81 | ||
82 | DygraphOptions.prototype.find = function(name) { | |
83 | if (this.global_user.hasOwnProperty(name)) { | |
84 | return this.global_user[name]; | |
85 | } | |
86 | if (this.global.hasOwnProperty(name)) { | |
87 | return this.global[name]; | |
88 | } | |
89 | return null; | |
90 | } | |
91 | ||
92 | DygraphOptions.prototype.findForAxis = function(name, axis) { | |
93 | ||
94 | var axisIdx = (axis == "y2" || axis == 1) ? 1 : 0; | |
95 | ||
96 | var axisOptions = this.axes[axisIdx]; | |
97 | if (axisOptions.hasOwnProperty(name)) { | |
98 | return axisOptions[name]; | |
99 | } | |
100 | return this.find(name); | |
101 | } | |
102 | ||
103 | DygraphOptions.prototype.findForSeries = function(name, series) { | |
104 | // Honors indexes as series. | |
105 | var seriesName = (typeof(series) == "number") ? this.labels[series] : series; | |
106 | ||
107 | if (!this.series.hasOwnProperty(seriesName)) { | |
108 | throw "Unknown series: " + series; | |
109 | } | |
110 | ||
111 | var seriesObj = this.series[seriesName]; | |
112 | var seriesOptions = seriesObj["options"]; | |
113 | if (seriesOptions.hasOwnProperty(name)) { | |
114 | return seriesOptions[name]; | |
115 | } | |
116 | return this.findForAxis(name, seriesObj["yAxis"]); | |
117 | } | |
118 |