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
14 * axes_ - array of axis index to { series : [ series names ] , options : { axis-specific options. }
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_
= [ { series
: [], options
: {}} ]; // 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
] = { series
: [ seriesName
], options
: axis
};
66 // Associate series without axis options with axis 0.
67 if (!axis
) { // undefined
68 this.axes_
[0].series
.push(seriesName
);
71 this.series_
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
74 // Go through one more time and assign series to an axis defined by another
75 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
76 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
77 var seriesName
= this.labels
[idx
];
78 var optionsForSeries
= this.series_
[seriesName
]["options"];
79 var axis
= optionsForSeries
["axis"];
81 if (typeof(axis
) == 'string') {
82 if (!this.series_
.hasOwnProperty(axis
)) {
83 this.dygraph_
.error("Series " + seriesName
+ " wants to share a y-axis with " +
84 "series " + axis
+ ", which does not define its own axis.");
87 var yAxis
= this.series_
[axis
].yAxis
;
88 this.series_
[seriesName
].yAxis
= yAxis
;
89 this.axes_
[yAxis
].series
.push(seriesName
);
93 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
94 // Read from the global "axes" option.
95 if (this.user_
.hasOwnProperty("axes")) {
96 var axis_opts
= this.user_
.axes
;
98 if (axis_opts
.hasOwnProperty("y")) {
99 Dygraph
.update(this.axes_
[0].options
, axis_opts
.y
);
102 if (axis_opts
.hasOwnProperty("y2")) {
103 this.axes_
[1] = this.axes_
[1] || {}; // FIX
104 Dygraph
.update(this.axes_
[1].options
, axis_opts
.y2
);
109 DygraphOptions
.prototype.find
= function(name
) {
110 if (this.user_
.hasOwnProperty(name
)) {
111 return this.user_
[name
];
113 if (this.global_
.hasOwnProperty(name
)) {
114 return this.global_
[name
];
119 DygraphOptions
.prototype.findForAxis
= function(name
, axis
) {
120 var axisIdx
= (axis
== "y2" || axis
== "y2" || axis
== 1) ? 1 : 0;
122 var axisOptions
= this.axes_
[axisIdx
].options
;
123 if (axisOptions
.hasOwnProperty(name
)) {
124 return axisOptions
[name
];
126 return this.find(name
);
129 DygraphOptions
.prototype.findForSeries
= function(name
, series
) {
130 // Honors indexes as series.
131 var seriesName
= (typeof(series
) == "number") ? this.labels
[series
] : series
;
133 if (seriesName
=== this.dygraph_
.highlightSet_
) {
134 if (this.highlightSeries_
.hasOwnProperty(name
)) {
135 return this.highlightSeries_
[name
];
139 if (!this.series_
.hasOwnProperty(seriesName
)) {
140 throw "Unknown series: " + series
;
143 var seriesObj
= this.series_
[seriesName
];
144 var seriesOptions
= seriesObj
["options"];
145 if (seriesOptions
.hasOwnProperty(name
)) {
146 return seriesOptions
[name
];
149 return this.findForAxis(name
, seriesObj
["yAxis"]);
153 * Returns the number of y-axes on the chart.
154 * @return {Number} the number of axes.
156 DygraphOptions
.prototype.numAxes
= function() {
157 return this.axes_
.length
;
161 * Return the y-axis for a given series, specified by name.
163 DygraphOptions
.prototype.axisForSeries
= function(seriesName
) {
164 return this.series_
[seriesName
].yAxis
;
168 * Returns the options for the specified axis.
170 DygraphOptions
.prototype.axisOptions
= function(yAxis
) {
171 return this.axes_
[yAxis
].options
;
175 * Return the series associated with an axis.
177 DygraphOptions
.prototype.seriesForAxis
= function(yAxis
) {
178 return this.axes_
[yAxis
].series
;