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 * 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.user_
= this.dygraph_
.user_attrs_
|| {};
35 this.highlightSeries_
= this.find("highlightSeriesOpts") || {};
36 // Get a list of series names.
38 var labels
= this.find("labels");
40 return; // -- can't do more for now, will parse after getting the labels.
46 DygraphOptions
.prototype.reparseSeries
= function() {
47 this.labels
= this.find("labels").slice(1);
49 this.axes_
= [ {} ]; // Always one axis at least.
52 var axisId
= 0; // 0-offset; there's always one.
53 // Go through once, add all the series, and for those with {} axis options, add a new axis.
54 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
55 var seriesName
= this.labels
[idx
];
57 var optionsForSeries
= this.user_
[seriesName
] || {};
60 var axis
= optionsForSeries
["axis"];
61 if (typeof(axis
) == 'object') {
63 this.axes_
[yAxis
] = axis
;
65 this.series_
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
68 // Go through one more time and assign series to an axis defined by another
69 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
70 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
71 var seriesName
= this.labels
[idx
];
72 var optionsForSeries
= this.series_
[seriesName
]["options"];
73 var axis
= optionsForSeries
["axis"];
75 if (typeof(axis
) == 'string') {
76 if (!this.series_
.hasOwnProperty(axis
)) {
77 this.dygraph_
.error("Series " + seriesName
+ " wants to share a y-axis with " +
78 "series " + axis
+ ", which does not define its own axis.");
81 this.series_
[seriesName
].yAxis
= this.series_
[axis
].yAxis
;
85 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
86 // Read from the global "axes" option.
87 if (this.user_
.hasOwnProperty("axes")) {
88 var axis_opts
= this.user_
.axes
;
90 if (axis_opts
.hasOwnProperty("y")) {
91 Dygraph
.update(this.axes_
[0], axis_opts
.y
);
94 if (axis_opts
.hasOwnProperty("y2")) {
95 this.axes_
[1] = this.axes_
[1] || {};
96 Dygraph
.update(this.axes_
[1], axis_opts
.y2
);
101 DygraphOptions
.prototype.find
= function(name
) {
102 if (this.user_
.hasOwnProperty(name
)) {
103 return this.user_
[name
];
105 if (this.global_
.hasOwnProperty(name
)) {
106 return this.global_
[name
];
111 DygraphOptions
.prototype.findForAxis
= function(name
, axis
) {
112 var axisIdx
= (axis
== "y2" || axis
== "y2" || axis
== 1) ? 1 : 0;
114 var axisOptions
= this.axes_
[axisIdx
];
115 if (axisOptions
.hasOwnProperty(name
)) {
116 return axisOptions
[name
];
118 return this.find(name
);
121 DygraphOptions
.prototype.findForSeries
= function(name
, series
) {
122 // Honors indexes as series.
123 var seriesName
= (typeof(series
) == "number") ? this.labels
[series
] : series
;
125 if (seriesName
=== this.dygraph_
.highlightSet_
) {
126 if (this.highlightSeries_
.hasOwnProperty(name
)) {
127 return this.highlightSeries_
[name
];
131 if (!this.series_
.hasOwnProperty(seriesName
)) {
132 throw "Unknown series: " + series
;
135 var seriesObj
= this.series_
[seriesName
];
136 var seriesOptions
= seriesObj
["options"];
137 if (seriesOptions
.hasOwnProperty(name
)) {
138 return seriesOptions
[name
];
141 return this.findForAxis(name
, seriesObj
["yAxis"]);