Add per-series tests that validate new-style options and how they relate to old-style...
[dygraphs.git] / dygraph-options.js
... / ...
CommitLineData
1/**
2 * @fileoverview DygraphOptions is responsible for parsing and returning information about options.
3 *
4 * Still tightly coupled to Dygraphs, we could remove some of that, you know.
5 */
6
7"use strict";
8
9/*
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
15 * series - { seriesName -> { idx, yAxis, options }
16 * labels - used as mapping from index to series name.
17 */
18
19/**
20 * @constructor
21 *
22 * This parses attributes into an object that can be easily queried.
23 *
24 * @param {Dyraph} dygraph The chart to which these options belong.
25 */
26var DygraphOptions = function(dygraph) {
27 this.dygraph_ = dygraph;
28 this.axes_ = [];
29 this.series_ = {};
30
31 // Once these two objects are initialized, you can call find();
32 this.global_ = this.dygraph_.attrs_;
33 this.user_ = this.dygraph_.user_attrs_ || {};
34
35 this.highlightSeries_ = this.find("highlightSeriesOpts") || {};
36 // Get a list of series names.
37
38 var labels = this.find("labels");
39 if (!labels) {
40 return; // -- can't do more for now, will parse after getting the labels.
41 };
42
43 this.reparseSeries();
44}
45
46DygraphOptions.prototype.reparseSeries = function() {
47 this.labels = this.find("labels").slice(1);
48
49 this.axes_ = [ {} ]; // Always one axis at least.
50 this.series_ = {};
51
52 // Traditionally, per-series options were specified right up there with the options. For instance
53 // {
54 // labels: [ "X", "foo", "bar" ],
55 // pointSize: 3,
56 // foo : {}, // options for foo
57 // bar : {} // options for bar
58 // }
59 //
60 // Moving forward, series really should be specified in the series element, separating them.
61 // like so:
62 //
63 // {
64 // labels: [ "X", "foo", "bar" ],
65 // pointSize: 3,
66 // series : {
67 // foo : {}, // options for foo
68 // bar : {} // options for bar
69 // }
70 // }
71 //
72 // So, if series is found, it's expected to contain per-series data, otherwise we fall
73 // back.
74 var allseries = this.user_["series"] ? this.user_.series : this.user_;
75
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];
80
81 var optionsForSeries = allseries[seriesName] || {};
82 var yAxis = 0;
83
84 var axis = optionsForSeries["axis"];
85 if (typeof(axis) == 'object') {
86 yAxis = ++axisId;
87 this.axes_[yAxis] = axis;
88 }
89 this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries };
90 }
91
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"];
98
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.");
103 return null;
104 }
105 this.series_[seriesName].yAxis = this.series_[axis].yAxis;
106 }
107 }
108
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;
113
114 if (axis_opts.hasOwnProperty("y")) {
115 Dygraph.update(this.axes_[0], axis_opts.y);
116 }
117
118 if (axis_opts.hasOwnProperty("y2")) {
119 this.axes_[1] = this.axes_[1] || {};
120 Dygraph.update(this.axes_[1], axis_opts.y2);
121 }
122 }
123};
124
125DygraphOptions.prototype.find = function(name) {
126 if (this.user_.hasOwnProperty(name)) {
127 return this.user_[name];
128 }
129 if (this.global_.hasOwnProperty(name)) {
130 return this.global_[name];
131 }
132 return null;
133}
134
135DygraphOptions.prototype.findForAxis = function(name, axis) {
136 var axisIdx = (axis == "y2" || axis == "y2" || axis == 1) ? 1 : 0;
137
138 var axisOptions = this.axes_[axisIdx];
139 if (axisOptions.hasOwnProperty(name)) {
140 return axisOptions[name];
141 }
142 return this.find(name);
143}
144
145DygraphOptions.prototype.findForSeries = function(name, series) {
146 // Honors indexes as series.
147 var seriesName = (typeof(series) == "number") ? this.labels[series] : series;
148
149 if (seriesName === this.dygraph_.highlightSet_) {
150 if (this.highlightSeries_.hasOwnProperty(name)) {
151 return this.highlightSeries_[name];
152 }
153 }
154
155 if (!this.series_.hasOwnProperty(seriesName)) {
156 throw "Unknown series: " + series;
157 }
158
159 var seriesObj = this.series_[seriesName];
160 var seriesOptions = seriesObj["options"];
161 if (seriesOptions.hasOwnProperty(name)) {
162 return seriesOptions[name];
163 }
164
165 return this.findForAxis(name, seriesObj["yAxis"]);
166}
167