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 // Traditionally, per-series options were specified right up there with the options. For instance
54 // labels: [ "X", "foo", "bar" ],
56 // foo : {}, // options
for foo
57 // bar : {} // options
for bar
60 // Moving forward, series really should be specified in the series element, separating them.
64 // labels: [ "X", "foo", "bar" ],
67 // foo : {}, // options
for foo
68 // bar : {} // options
for bar
72 // So, if series is found, it's expected to contain per-series data, otherwise we fall
74 var allseries
= this.user_
["series"] ? this.user_
.series
: this.user_
;
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
];
81 var optionsForSeries
= allseries
[seriesName
] || {};
84 var axis
= optionsForSeries
["axis"];
85 if (typeof(axis
) == 'object') {
87 this.axes_
[yAxis
] = axis
;
89 this.series_
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
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
];
96 var optionsForSeries
= this.series_
[seriesName
]["options"];
97 var axis
= optionsForSeries
["axis"];
99 if (typeof(axis
) == 'string') {
100 if (!this.series_
.hasOwnProperty(axis
)) {
101 this.dygraph_
.error("Series " + seriesName
+ " wants to share a y-axis with " +
102 "series " + axis
+ ", which does not define its own axis.");
105 this.series_
[seriesName
].yAxis
= this.series_
[axis
].yAxis
;
109 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
110 // Read from the global "axes" option.
111 if (this.user_
["axes"]) {
112 var axis_opts
= this.user_
.axes
;
114 if (axis_opts
.hasOwnProperty("y")) {
115 Dygraph
.update(this.axes_
[0], axis_opts
.y
);
118 if (axis_opts
.hasOwnProperty("y2")) {
119 this.axes_
[1] = this.axes_
[1] || {};
120 Dygraph
.update(this.axes_
[1], axis_opts
.y2
);
125 DygraphOptions
.prototype.find
= function(name
) {
126 if (this.user_
.hasOwnProperty(name
)) {
127 return this.user_
[name
];
129 if (this.global_
.hasOwnProperty(name
)) {
130 return this.global_
[name
];
135 DygraphOptions
.prototype.findForAxis
= function(name
, axis
) {
136 var axisIdx
= (axis
== "y2" || axis
== "y2" || axis
== 1) ? 1 : 0;
138 var axisOptions
= this.axes_
[axisIdx
];
139 if (axisOptions
.hasOwnProperty(name
)) {
140 return axisOptions
[name
];
142 return this.find(name
);
145 DygraphOptions
.prototype.findForSeries
= function(name
, series
) {
146 // Honors indexes as series.
147 var seriesName
= (typeof(series
) == "number") ? this.labels
[series
] : series
;
149 if (seriesName
=== this.dygraph_
.highlightSet_
) {
150 if (this.highlightSeries_
.hasOwnProperty(name
)) {
151 return this.highlightSeries_
[name
];
155 if (!this.series_
.hasOwnProperty(seriesName
)) {
156 throw "Unknown series: " + series
;
159 var seriesObj
= this.series_
[seriesName
];
160 var seriesOptions
= seriesObj
["options"];
161 if (seriesOptions
.hasOwnProperty(name
)) {
162 return seriesOptions
[name
];
165 return this.findForAxis(name
, seriesObj
["yAxis"]);