| 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 - map of options specific to the axis. |
| 15 | * series - { seriesName -> { idx, yAxis, options } |
| 16 | * labels - used as mapping from index to series name. |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * This parses attributes into an object that can be easily queried. |
| 21 | * |
| 22 | * It doesn't necessarily mean that all options are available, specifically |
| 23 | * if labels are not yet available, since those drive details of the per-series |
| 24 | * and per-axis options. |
| 25 | * |
| 26 | * @param {Dyraph} dygraph The chart to which these options belong. |
| 27 | * @constructor |
| 28 | */ |
| 29 | var DygraphOptions = function(dygraph) { |
| 30 | this.dygraph_ = dygraph; |
| 31 | this.axes_ = []; |
| 32 | this.series_ = {}; |
| 33 | |
| 34 | // Once these two objects are initialized, you can call get(); |
| 35 | this.global_ = this.dygraph_.attrs_; |
| 36 | this.user_ = this.dygraph_.user_attrs_ || {}; |
| 37 | |
| 38 | this.highlightSeries_ = this.get("highlightSeriesOpts") || {}; |
| 39 | // Get a list of series names. |
| 40 | |
| 41 | var labels = this.get("labels"); |
| 42 | if (!labels) { |
| 43 | return; // -- can't do more for now, will parse after getting the labels. |
| 44 | } |
| 45 | |
| 46 | this.reparseSeries(); |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * Reparses options that are all related to series. This typically occurs when |
| 51 | * options are either updated, or source data has been made avaialble. |
| 52 | * |
| 53 | * TODO(konigsberg): The method name is kind of weak; fix. |
| 54 | */ |
| 55 | DygraphOptions.prototype.reparseSeries = function() { |
| 56 | this.labels = this.get("labels").slice(1); |
| 57 | |
| 58 | this.axes_ = [ {} ]; // Always one axis at least. |
| 59 | this.series_ = {}; |
| 60 | |
| 61 | var axisId = 0; // 0-offset; there's always one. |
| 62 | // Go through once, add all the series, and for those with {} axis options, add a new axis. |
| 63 | for (var idx = 0; idx < this.labels.length; idx++) { |
| 64 | var seriesName = this.labels[idx]; |
| 65 | |
| 66 | var optionsForSeries = this.user_[seriesName] || {}; |
| 67 | var yAxis = 0; |
| 68 | |
| 69 | var axis = optionsForSeries["axis"]; |
| 70 | if (typeof(axis) == 'object') { |
| 71 | yAxis = ++axisId; |
| 72 | this.axes_[yAxis] = axis; |
| 73 | } |
| 74 | this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; |
| 75 | } |
| 76 | |
| 77 | // Go through one more time and assign series to an axis defined by another |
| 78 | // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } |
| 79 | for (var idx = 0; idx < this.labels.length; idx++) { |
| 80 | var seriesName = this.labels[idx]; |
| 81 | var optionsForSeries = this.series_[seriesName]["options"]; |
| 82 | var axis = optionsForSeries["axis"]; |
| 83 | |
| 84 | if (typeof(axis) == 'string') { |
| 85 | if (!this.series_.hasOwnProperty(axis)) { |
| 86 | this.dygraph_.error("Series " + seriesName + " wants to share a y-axis with " + |
| 87 | "series " + axis + ", which does not define its own axis."); |
| 88 | return null; |
| 89 | } |
| 90 | this.series_[seriesName].yAxis = this.series_[axis].yAxis; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // This doesn't support reading from the 'x' axis, only 'y' and 'y2. |
| 95 | // Read from the global "axes" option. |
| 96 | if (this.user_.hasOwnProperty("axes")) { |
| 97 | var axis_opts = this.user_.axes; |
| 98 | |
| 99 | if (axis_opts.hasOwnProperty("y")) { |
| 100 | Dygraph.update(this.axes_[0], axis_opts.y); |
| 101 | } |
| 102 | |
| 103 | if (axis_opts.hasOwnProperty("y2")) { |
| 104 | this.axes_[1] = this.axes_[1] || {}; |
| 105 | Dygraph.update(this.axes_[1], axis_opts.y2); |
| 106 | } |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * Get a global value. |
| 112 | * |
| 113 | * @param {String} name the name of the option. |
| 114 | */ |
| 115 | DygraphOptions.prototype.get = function(name) { |
| 116 | if (this.user_.hasOwnProperty(name)) { |
| 117 | return this.user_[name]; |
| 118 | } |
| 119 | if (this.global_.hasOwnProperty(name)) { |
| 120 | return this.global_[name]; |
| 121 | } |
| 122 | return null; |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * Get a value for a specific axis. If there is no specific value for the axis, |
| 127 | * the global value is returned. |
| 128 | * |
| 129 | * @param {String} name the name of the option. |
| 130 | * @param {String|number} axis the axis to search. Can be the string representation |
| 131 | * ("y", "y2") or the axis number (0, 1). |
| 132 | */ |
| 133 | DygraphOptions.prototype.getForAxis = function(name, axis) { |
| 134 | var axisIdx = 0; |
| 135 | if (typeof(axis) == 'number') { |
| 136 | axisIdx = axis; |
| 137 | } else { |
| 138 | // TODO(konigsberg): Accept only valid axis strings? |
| 139 | axisIdx = (axis == "y2") ? 1 : 0; |
| 140 | } |
| 141 | |
| 142 | var axisOptions = this.axes_[axisIdx]; |
| 143 | if (axisOptions.hasOwnProperty(name)) { |
| 144 | return axisOptions[name]; |
| 145 | } |
| 146 | return this.get(name); |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * Get a value for a specific series. If there is no specific value for the series, |
| 151 | * the value for the axis is returned (and afterwards, the global value.) |
| 152 | * |
| 153 | * @param {String} name the name of the option. |
| 154 | * @param {String|number} series the series to search. Can be the string representation |
| 155 | * or 0-offset series number. |
| 156 | */ |
| 157 | DygraphOptions.prototype.getForSeries = function(name, series) { |
| 158 | // Honors indexes as series. |
| 159 | var seriesName = (typeof(series) == "number") ? this.labels[series] : series; |
| 160 | |
| 161 | if (seriesName === this.dygraph_.highlightSet_) { |
| 162 | if (this.highlightSeries_.hasOwnProperty(name)) { |
| 163 | return this.highlightSeries_[name]; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (!this.series_.hasOwnProperty(seriesName)) { |
| 168 | throw "Unknown series: " + series; |
| 169 | } |
| 170 | |
| 171 | var seriesObj = this.series_[seriesName]; |
| 172 | var seriesOptions = seriesObj["options"]; |
| 173 | if (seriesOptions.hasOwnProperty(name)) { |
| 174 | return seriesOptions[name]; |
| 175 | } |
| 176 | |
| 177 | return this.getForAxis(name, seriesObj["yAxis"]); |
| 178 | }; |
| 179 | |