Add numAxes function.
[dygraphs.git] / dygraph-options.js
CommitLineData
c1780ad0
RK
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.
673a3b87
RK
12 * global_ - global attributes (common among all graphs, AIUI)
13 * user_ - attributes set by the user
14 * axes_ - array of axis index to axis-specific options.
15 * series_ - { seriesName -> { idx, yAxis, options }
16 * labels_ - used as mapping from index to series name.
c1780ad0
RK
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;
ed30673a
RK
28 this.axes_ = [];
29 this.series_ = {};
c1780ad0
RK
30
31 // Once these two objects are initialized, you can call find();
ed30673a
RK
32 this.global_ = this.dygraph_.attrs_;
33 this.user_ = this.dygraph_.user_attrs_ || {};
c1780ad0 34
ed30673a 35 this.highlightSeries_ = this.find("highlightSeriesOpts") || {};
c1780ad0 36 // Get a list of series names.
34825ef5
RK
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
dd724e22 43 this.reparseSeries();
34825ef5
RK
44}
45
46DygraphOptions.prototype.reparseSeries = function() {
c1780ad0
RK
47 this.labels = this.find("labels").slice(1);
48
ed30673a
RK
49 this.axes_ = [ {} ]; // Always one axis at least.
50 this.series_ = {};
eb0da59f 51
c1780ad0
RK
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];
56
ed30673a 57 var optionsForSeries = this.user_[seriesName] || {};
c1780ad0
RK
58 var yAxis = 0;
59
60 var axis = optionsForSeries["axis"];
61 if (typeof(axis) == 'object') {
62 yAxis = ++axisId;
ed30673a 63 this.axes_[yAxis] = axis;
c1780ad0 64 }
ed30673a 65 this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries };
c1780ad0
RK
66 }
67
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];
ed30673a 72 var optionsForSeries = this.series_[seriesName]["options"];
c1780ad0
RK
73 var axis = optionsForSeries["axis"];
74
75 if (typeof(axis) == 'string') {
ed30673a 76 if (!this.series_.hasOwnProperty(axis)) {
c1780ad0
RK
77 this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " +
78 "series " + axis + ", which does not define its own axis.");
79 return null;
80 }
ed30673a 81 this.series_[seriesName].yAxis = this.series_[axis].yAxis;
c1780ad0
RK
82 }
83 }
84
85 // This doesn't support reading from the 'x' axis, only 'y' and 'y2.
86 // Read from the global "axes" option.
ed30673a
RK
87 if (this.user_.hasOwnProperty("axes")) {
88 var axis_opts = this.user_.axes;
c1780ad0 89
eb0da59f 90 if (axis_opts.hasOwnProperty("y")) {
ed30673a 91 Dygraph.update(this.axes_[0], axis_opts.y);
eb0da59f
RK
92 }
93
94 if (axis_opts.hasOwnProperty("y2")) {
ed30673a
RK
95 this.axes_[1] = this.axes_[1] || {};
96 Dygraph.update(this.axes_[1], axis_opts.y2);
eb0da59f 97 }
c1780ad0
RK
98 }
99};
100
101DygraphOptions.prototype.find = function(name) {
ed30673a
RK
102 if (this.user_.hasOwnProperty(name)) {
103 return this.user_[name];
c1780ad0 104 }
ed30673a
RK
105 if (this.global_.hasOwnProperty(name)) {
106 return this.global_[name];
c1780ad0
RK
107 }
108 return null;
109}
110
111DygraphOptions.prototype.findForAxis = function(name, axis) {
dd724e22 112 var axisIdx = (axis == "y2" || axis == "y2" || axis == 1) ? 1 : 0;
c1780ad0 113
ed30673a 114 var axisOptions = this.axes_[axisIdx];
c1780ad0
RK
115 if (axisOptions.hasOwnProperty(name)) {
116 return axisOptions[name];
117 }
118 return this.find(name);
119}
120
121DygraphOptions.prototype.findForSeries = function(name, series) {
122 // Honors indexes as series.
123 var seriesName = (typeof(series) == "number") ? this.labels[series] : series;
124
ed30673a
RK
125 if (seriesName === this.dygraph_.highlightSet_) {
126 if (this.highlightSeries_.hasOwnProperty(name)) {
127 return this.highlightSeries_[name];
128 }
129 }
130
131 if (!this.series_.hasOwnProperty(seriesName)) {
c1780ad0
RK
132 throw "Unknown series: " + series;
133 }
134
ed30673a 135 var seriesObj = this.series_[seriesName];
c1780ad0
RK
136 var seriesOptions = seriesObj["options"];
137 if (seriesOptions.hasOwnProperty(name)) {
138 return seriesOptions[name];
139 }
ed30673a 140
c1780ad0
RK
141 return this.findForAxis(name, seriesObj["yAxis"]);
142}
143
673a3b87
RK
144/**
145 * Returns the number of y-axes on the chart.
146 * @return {Number} the number of axes.
147 */
148DygraphOptions.prototype.numAxes = function() {
149 return this.axes_.length;
150}