2 * @fileoverview DygraphOptions is responsible for parsing and returning information about options.
4 * Still tightly coupled to Dygraphs, we could remove some of that, you know.
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
15 * series - { seriesName -> { idx, yAxis, options }
16 * labels - used as mapping from index to series name.
22 * This parses attributes into an object that can be easily queried.
24 * @param {Dyraph} dygraph The chart to which these options belong.
26 var DygraphOptions
= function(dygraph
) {
27 this.dygraph_
= dygraph
;
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_
|| {};
35 // Get a list of series names.
36 this.labels
= this.find("labels").slice(1);
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
];
43 var optionsForSeries
= this.global_user
[seriesName
] || {};
46 var axis
= optionsForSeries
["axis"];
47 if (typeof(axis
) == 'object') {
50 this.series
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
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"];
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.");
66 this.series
[seriesName
].yAxis
= this.series
[axis
].yAxis
;
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
;
75 this.axes
.push(axis_opts
["y"] || {});
76 this.axes
.push(axis_opts
["y2"] || {});
78 this.axes
.push(axis_opts
["y"] || {}); // There has to be at least one axis.
82 DygraphOptions
.prototype.find
= function(name
) {
83 if (this.global_user
.hasOwnProperty(name
)) {
84 return this.global_user
[name
];
86 if (this.global
.hasOwnProperty(name
)) {
87 return this.global
[name
];
92 DygraphOptions
.prototype.findForAxis
= function(name
, axis
) {
94 var axisIdx
= (axis
== "y2" || axis
== 1) ? 1 : 0;
96 var axisOptions
= this.axes
[axisIdx
];
97 if (axisOptions
.hasOwnProperty(name
)) {
98 return axisOptions
[name
];
100 return this.find(name
);
103 DygraphOptions
.prototype.findForSeries
= function(name
, series
) {
104 // Honors indexes as series.
105 var seriesName
= (typeof(series
) == "number") ? this.labels
[series
] : series
;
107 if (!this.series
.hasOwnProperty(seriesName
)) {
108 throw "Unknown series: " + series
;
111 var seriesObj
= this.series
[seriesName
];
112 var seriesOptions
= seriesObj
["options"];
113 if (seriesOptions
.hasOwnProperty(name
)) {
114 return seriesOptions
[name
];
116 return this.findForAxis(name
, seriesObj
["yAxis"]);