| 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2011 Dan Vanderkam (danvdk@gmail.com) |
| 4 | * MIT-licensed (http://opensource.org/licenses/MIT) |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @fileoverview DygraphOptions is responsible for parsing and returning information about options. |
| 9 | * |
| 10 | * Still tightly coupled to Dygraphs, we could remove some of that, you know. |
| 11 | */ |
| 12 | |
| 13 | var DygraphOptions = (function() { |
| 14 | |
| 15 | /*jshint sub:true */ |
| 16 | /*global Dygraph:false */ |
| 17 | "use strict"; |
| 18 | |
| 19 | /* |
| 20 | * Interesting member variables: |
| 21 | * dygraph_ - the graph. |
| 22 | * global_ - global attributes (common among all graphs, AIUI) |
| 23 | * user - attributes set by the user |
| 24 | * yAxes_ - array of axis index to { series : [ series names ] , options : { axis-specific options. } |
| 25 | * xAxis_ - { options : { axis-specific options. } |
| 26 | * series_ - { seriesName -> { idx, yAxis, options }} |
| 27 | * labels_ - used as mapping from index to series name. |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * This parses attributes into an object that can be easily queried. |
| 32 | * |
| 33 | * It doesn't necessarily mean that all options are available, specifically |
| 34 | * if labels are not yet available, since those drive details of the per-series |
| 35 | * and per-axis options. |
| 36 | * |
| 37 | * @param {Dygraph} dygraph The chart to which these options belong. |
| 38 | * @constructor |
| 39 | */ |
| 40 | var DygraphOptions = function(dygraph) { |
| 41 | this.dygraph_ = dygraph; |
| 42 | |
| 43 | /** @type {Array.<{options: Object, series: string}>} @private */ |
| 44 | this.yAxes_ = []; |
| 45 | /** @type {{options: Object}} @private */ |
| 46 | this.xAxis_ = {options: {}}; |
| 47 | /** @type {Object} @private */ |
| 48 | this.series_ = {}; |
| 49 | |
| 50 | // Once these two objects are initialized, you can call get(); |
| 51 | this.global_ = this.dygraph_.attrs_; |
| 52 | this.user_ = this.dygraph_.user_attrs_ || {}; |
| 53 | |
| 54 | this.highlightSeries_ = this.get("highlightSeriesOpts") || {}; |
| 55 | this.reparseSeries(); |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * Not optimal, but does the trick when you're only using two axes. |
| 60 | * If we move to more axes, this can just become a function. |
| 61 | */ |
| 62 | DygraphOptions.AXIS_STRING_MAPPINGS_ = { |
| 63 | 'y' : 0, |
| 64 | 'Y' : 0, |
| 65 | 'y1' : 0, |
| 66 | 'Y1' : 0, |
| 67 | 'y2' : 1, |
| 68 | 'Y2' : 1 |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * @param {string|number} axis |
| 73 | * @private |
| 74 | */ |
| 75 | DygraphOptions.axisToIndex_ = function(axis) { |
| 76 | if (typeof(axis) == "string") { |
| 77 | if (DygraphOptions.AXIS_STRING_MAPPINGS_.hasOwnProperty(axis)) { |
| 78 | return DygraphOptions.AXIS_STRING_MAPPINGS_[axis]; |
| 79 | } |
| 80 | throw "Unknown axis : " + axis; |
| 81 | } |
| 82 | if (typeof(axis) == "number") { |
| 83 | if (axis === 0 || axis === 1) { |
| 84 | return axis; |
| 85 | } |
| 86 | throw "Dygraphs only supports two y-axes, indexed from 0-1."; |
| 87 | } |
| 88 | if (axis) { |
| 89 | throw "Unknown axis : " + axis; |
| 90 | } |
| 91 | // No axis specification means axis 0. |
| 92 | return 0; |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * Reparses options that are all related to series. This typically occurs when |
| 97 | * options are either updated, or source data has been made available. |
| 98 | * |
| 99 | * TODO(konigsberg): The method name is kind of weak; fix. |
| 100 | */ |
| 101 | DygraphOptions.prototype.reparseSeries = function() { |
| 102 | var labels = this.get("labels"); |
| 103 | if (!labels) { |
| 104 | return; // -- can't do more for now, will parse after getting the labels. |
| 105 | } |
| 106 | |
| 107 | this.labels_ = labels.slice(1); |
| 108 | |
| 109 | this.yAxes_ = [ { series : [], options : {}} ]; // Always one axis at least. |
| 110 | this.xAxis_ = { options : {} }; |
| 111 | this.series_ = {}; |
| 112 | |
| 113 | // Traditionally, per-series options were specified right up there with the options. For instance |
| 114 | // { |
| 115 | // labels: [ "X", "foo", "bar" ], |
| 116 | // pointSize: 3, |
| 117 | // foo : {}, // options for foo |
| 118 | // bar : {} // options for bar |
| 119 | // } |
| 120 | // |
| 121 | // Moving forward, series really should be specified in the series element, separating them. |
| 122 | // like so: |
| 123 | // |
| 124 | // { |
| 125 | // labels: [ "X", "foo", "bar" ], |
| 126 | // pointSize: 3, |
| 127 | // series : { |
| 128 | // foo : {}, // options for foo |
| 129 | // bar : {} // options for bar |
| 130 | // } |
| 131 | // } |
| 132 | // |
| 133 | // So, if series is found, it's expected to contain per-series data, otherwise we fall |
| 134 | // back. |
| 135 | var oldStyleSeries = !this.user_["series"]; |
| 136 | |
| 137 | if (oldStyleSeries) { |
| 138 | var axisId = 0; // 0-offset; there's always one. |
| 139 | // Go through once, add all the series, and for those with {} axis options, add a new axis. |
| 140 | for (var idx = 0; idx < this.labels_.length; idx++) { |
| 141 | var seriesName = this.labels_[idx]; |
| 142 | |
| 143 | var optionsForSeries = this.user_[seriesName] || {}; |
| 144 | |
| 145 | var yAxis = 0; |
| 146 | var axis = optionsForSeries["axis"]; |
| 147 | if (typeof(axis) == 'object') { |
| 148 | yAxis = ++axisId; |
| 149 | this.yAxes_[yAxis] = { series : [ seriesName ], options : axis }; |
| 150 | } |
| 151 | |
| 152 | // Associate series without axis options with axis 0. |
| 153 | if (!axis) { // undefined |
| 154 | this.yAxes_[0].series.push(seriesName); |
| 155 | } |
| 156 | |
| 157 | this.series_[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; |
| 158 | } |
| 159 | |
| 160 | // Go through one more time and assign series to an axis defined by another |
| 161 | // series, e.g. { 'Y1: { axis: {} }, 'Y2': { axis: 'Y1' } } |
| 162 | for (var idx = 0; idx < this.labels_.length; idx++) { |
| 163 | var seriesName = this.labels_[idx]; |
| 164 | var optionsForSeries = this.series_[seriesName]["options"]; |
| 165 | var axis = optionsForSeries["axis"]; |
| 166 | |
| 167 | if (typeof(axis) == 'string') { |
| 168 | if (!this.series_.hasOwnProperty(axis)) { |
| 169 | Dygraph.error("Series " + seriesName + " wants to share a y-axis with " + |
| 170 | "series " + axis + ", which does not define its own axis."); |
| 171 | return; |
| 172 | } |
| 173 | var yAxis = this.series_[axis].yAxis; |
| 174 | this.series_[seriesName].yAxis = yAxis; |
| 175 | this.yAxes_[yAxis].series.push(seriesName); |
| 176 | } |
| 177 | } |
| 178 | } else { |
| 179 | for (var idx = 0; idx < this.labels_.length; idx++) { |
| 180 | var seriesName = this.labels_[idx]; |
| 181 | var optionsForSeries = this.user_.series[seriesName] || {}; |
| 182 | var yAxis = DygraphOptions.axisToIndex_(optionsForSeries["axis"]); |
| 183 | |
| 184 | this.series_[seriesName] = { |
| 185 | idx: idx, |
| 186 | yAxis: yAxis, |
| 187 | options : optionsForSeries }; |
| 188 | |
| 189 | if (!this.yAxes_[yAxis]) { |
| 190 | this.yAxes_[yAxis] = { series : [ seriesName ], options : {} }; |
| 191 | } else { |
| 192 | this.yAxes_[yAxis].series.push(seriesName); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | var axis_opts = this.user_["axes"] || {}; |
| 198 | Dygraph.update(this.yAxes_[0].options, axis_opts["y"] || {}); |
| 199 | if (this.yAxes_.length > 1) { |
| 200 | Dygraph.update(this.yAxes_[1].options, axis_opts["y2"] || {}); |
| 201 | } |
| 202 | Dygraph.update(this.xAxis_.options, axis_opts["x"] || {}); |
| 203 | }; |
| 204 | |
| 205 | /** |
| 206 | * Get a global value. |
| 207 | * |
| 208 | * @param {string} name the name of the option. |
| 209 | */ |
| 210 | DygraphOptions.prototype.get = function(name) { |
| 211 | var result = this.getGlobalUser_(name); |
| 212 | if (result !== null) { |
| 213 | return result; |
| 214 | } |
| 215 | return this.getGlobalDefault_(name); |
| 216 | }; |
| 217 | |
| 218 | DygraphOptions.prototype.getGlobalUser_ = function(name) { |
| 219 | if (this.user_.hasOwnProperty(name)) { |
| 220 | return this.user_[name]; |
| 221 | } |
| 222 | return null; |
| 223 | }; |
| 224 | |
| 225 | DygraphOptions.prototype.getGlobalDefault_ = function(name) { |
| 226 | if (this.global_.hasOwnProperty(name)) { |
| 227 | return this.global_[name]; |
| 228 | } |
| 229 | if (Dygraph.DEFAULT_ATTRS.hasOwnProperty(name)) { |
| 230 | return Dygraph.DEFAULT_ATTRS[name]; |
| 231 | } |
| 232 | return null; |
| 233 | }; |
| 234 | |
| 235 | /** |
| 236 | * Get a value for a specific axis. If there is no specific value for the axis, |
| 237 | * the global value is returned. |
| 238 | * |
| 239 | * @param {string} name the name of the option. |
| 240 | * @param {string|number} axis the axis to search. Can be the string representation |
| 241 | * ("y", "y2") or the axis number (0, 1). |
| 242 | */ |
| 243 | DygraphOptions.prototype.getForAxis = function(name, axis) { |
| 244 | var axisIdx; |
| 245 | var axisString; |
| 246 | |
| 247 | // Since axis can be a number or a string, straighten everything out here. |
| 248 | if (typeof(axis) == 'number') { |
| 249 | axisIdx = axis; |
| 250 | axisString = axisIdx === 0 ? "y" : "y2"; |
| 251 | } else { |
| 252 | if (axis == "y1") { axis = "y"; } // Standardize on 'y'. Is this bad? I think so. |
| 253 | if (axis == "y") { |
| 254 | axisIdx = 0; |
| 255 | } else if (axis == "y2") { |
| 256 | axisIdx = 1; |
| 257 | } else if (axis == "x") { |
| 258 | axisIdx = -1; // simply a placeholder for below. |
| 259 | } else { |
| 260 | throw "Unknown axis " + axis; |
| 261 | } |
| 262 | axisString = axis; |
| 263 | } |
| 264 | |
| 265 | var userAxis = (axisIdx == -1) ? this.xAxis_ : this.yAxes_[axisIdx]; |
| 266 | |
| 267 | // Search the user-specified axis option first. |
| 268 | if (userAxis) { // This condition could be removed if we always set up this.yAxes_ for y2. |
| 269 | var axisOptions = userAxis.options; |
| 270 | if (axisOptions.hasOwnProperty(name)) { |
| 271 | return axisOptions[name]; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // User-specified global options second. |
| 276 | var result = this.getGlobalUser_(name); |
| 277 | if (result !== null) { |
| 278 | return result; |
| 279 | } |
| 280 | |
| 281 | // Default axis options third. |
| 282 | var defaultAxisOptions = Dygraph.DEFAULT_ATTRS.axes[axisString]; |
| 283 | if (defaultAxisOptions.hasOwnProperty(name)) { |
| 284 | return defaultAxisOptions[name]; |
| 285 | } |
| 286 | |
| 287 | // Default global options last. |
| 288 | return this.getGlobalDefault_(name); |
| 289 | }; |
| 290 | |
| 291 | /** |
| 292 | * Get a value for a specific series. If there is no specific value for the series, |
| 293 | * the value for the axis is returned (and afterwards, the global value.) |
| 294 | * |
| 295 | * @param {string} name the name of the option. |
| 296 | * @param {string} series the series to search. |
| 297 | */ |
| 298 | DygraphOptions.prototype.getForSeries = function(name, series) { |
| 299 | // Honors indexes as series. |
| 300 | if (series === this.dygraph_.getHighlightSeries()) { |
| 301 | if (this.highlightSeries_.hasOwnProperty(name)) { |
| 302 | return this.highlightSeries_[name]; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (!this.series_.hasOwnProperty(series)) { |
| 307 | throw "Unknown series: " + series; |
| 308 | } |
| 309 | |
| 310 | var seriesObj = this.series_[series]; |
| 311 | var seriesOptions = seriesObj["options"]; |
| 312 | if (seriesOptions.hasOwnProperty(name)) { |
| 313 | return seriesOptions[name]; |
| 314 | } |
| 315 | |
| 316 | return this.getForAxis(name, seriesObj["yAxis"]); |
| 317 | }; |
| 318 | |
| 319 | /** |
| 320 | * Returns the number of y-axes on the chart. |
| 321 | * @return {number} the number of axes. |
| 322 | */ |
| 323 | DygraphOptions.prototype.numAxes = function() { |
| 324 | return this.yAxes_.length; |
| 325 | }; |
| 326 | |
| 327 | /** |
| 328 | * Return the y-axis for a given series, specified by name. |
| 329 | */ |
| 330 | DygraphOptions.prototype.axisForSeries = function(series) { |
| 331 | return this.series_[series].yAxis; |
| 332 | }; |
| 333 | |
| 334 | /** |
| 335 | * Returns the options for the specified axis. |
| 336 | */ |
| 337 | // TODO(konigsberg): this is y-axis specific. Support the x axis. |
| 338 | DygraphOptions.prototype.axisOptions = function(yAxis) { |
| 339 | return this.yAxes_[yAxis].options; |
| 340 | }; |
| 341 | |
| 342 | /** |
| 343 | * Return the series associated with an axis. |
| 344 | */ |
| 345 | DygraphOptions.prototype.seriesForAxis = function(yAxis) { |
| 346 | return this.yAxes_[yAxis].series; |
| 347 | }; |
| 348 | |
| 349 | /** |
| 350 | * Return the list of all series, in their columnar order. |
| 351 | */ |
| 352 | DygraphOptions.prototype.seriesNames = function() { |
| 353 | return this.labels_; |
| 354 | }; |
| 355 | |
| 356 | /* Are we using this? */ |
| 357 | /** |
| 358 | * Return the index of the specified series. |
| 359 | * @param {string} series the series name. |
| 360 | */ |
| 361 | DygraphOptions.prototype.indexOfSeries = function(series) { |
| 362 | return this.series_[series].idx; |
| 363 | }; |
| 364 | |
| 365 | return DygraphOptions; |
| 366 | |
| 367 | })(); |