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.
37 var labels
= this.find("labels");
39 return; // -- can't do more for now, will parse after getting the labels.
42 this.reparseForLabels();
45 DygraphOptions
.prototype.reparseSeries
= function() {
46 this.labels
= this.find("labels").slice(1);
48 var axisId
= 0; // 0-offset; there's always one.
49 // Go through once, add all the series, and for those with {} axis options, add a new axis.
50 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
51 var seriesName
= this.labels
[idx
];
53 var optionsForSeries
= this.global_user
[seriesName
] || {};
56 var axis
= optionsForSeries
["axis"];
57 if (typeof(axis
) == 'object') {
60 this.series
[seriesName
] = { idx
: idx
, yAxis
: yAxis
, options
: optionsForSeries
};
63 // Go through one more time and assign series to an axis defined by another
64 // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } }
65 for (var idx
= 0; idx
< this.labels
.length
; idx
++) {
66 var seriesName
= this.labels
[idx
];
67 var optionsForSeries
= this.series
[seriesName
]["options"];
68 var axis
= optionsForSeries
["axis"];
70 if (typeof(axis
) == 'string') {
71 if (!this.series
.hasOwnProperty(axis
)) {
72 this.dygraph_
.error("Series " + seriesName
+ " wants to share a y-axis with " +
73 "series " + axis
+ ", which does not define its own axis.");
76 this.series
[seriesName
].yAxis
= this.series
[axis
].yAxis
;
80 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
81 // Read from the global "axes" option.
82 if (this.global_user
.hasOwnProperty("axes")) {
83 var axis_opts
= this.global_user
.axes
;
85 this.axes
.push(axis_opts
["y"] || {});
86 this.axes
.push(axis_opts
["y2"] || {});
88 this.axes
.push({}); // There has to be at least one axis.
92 DygraphOptions
.prototype.find
= function(name
) {
93 if (this.global_user
.hasOwnProperty(name
)) {
94 return this.global_user
[name
];
96 if (this.global
.hasOwnProperty(name
)) {
97 return this.global
[name
];
102 DygraphOptions
.prototype.findForAxis
= function(name
, axis
) {
104 var axisIdx
= (axis
== "y2" || axis
== 1) ? 1 : 0;
106 var axisOptions
= this.axes
[axisIdx
];
107 if (axisOptions
.hasOwnProperty(name
)) {
108 return axisOptions
[name
];
110 return this.find(name
);
113 DygraphOptions
.prototype.findForSeries
= function(name
, series
) {
114 // Honors indexes as series.
115 var seriesName
= (typeof(series
) == "number") ? this.labels
[series
] : series
;
117 if (!this.series
.hasOwnProperty(seriesName
)) {
118 throw "Unknown series: " + series
;
121 var seriesObj
= this.series
[seriesName
];
122 var seriesOptions
= seriesObj
["options"];
123 if (seriesOptions
.hasOwnProperty(name
)) {
124 return seriesOptions
[name
];
126 return this.findForAxis(name
, seriesObj
["yAxis"]);