| 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 | * global_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 | */ |
| 26 | var 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.global_user = this.dygraph_.user_attrs_ || {}; |
| 34 | |
| 35 | // Get a list of series names. |
| 36 | |
| 37 | var labels = this.find("labels"); |
| 38 | if (!labels) { |
| 39 | return; // -- can't do more for now, will parse after getting the labels. |
| 40 | }; |
| 41 | |
| 42 | this.reparseSeries(); |
| 43 | } |
| 44 | |
| 45 | DygraphOptions.prototype.reparseSeries = function() { |
| 46 | this.labels = this.find("labels").slice(1); |
| 47 | |
| 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]; |
| 52 | |
| 53 | var optionsForSeries = this.global_user[seriesName] || {}; |
| 54 | var yAxis = 0; |
| 55 | |
| 56 | var axis = optionsForSeries["axis"]; |
| 57 | if (typeof(axis) == 'object') { |
| 58 | yAxis = ++axisId; |
| 59 | } |
| 60 | this.series[seriesName] = { idx: idx, yAxis: yAxis, options : optionsForSeries }; |
| 61 | } |
| 62 | |
| 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"]; |
| 69 | |
| 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."); |
| 74 | return null; |
| 75 | } |
| 76 | this.series[seriesName].yAxis = this.series[axis].yAxis; |
| 77 | } |
| 78 | } |
| 79 | |
| 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; |
| 84 | |
| 85 | this.axes.push(axis_opts["y"] || {}); |
| 86 | this.axes.push(axis_opts["y2"] || {}); |
| 87 | } else { |
| 88 | this.axes.push({}); // There has to be at least one axis. |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | DygraphOptions.prototype.find = function(name) { |
| 93 | if (this.global_user.hasOwnProperty(name)) { |
| 94 | return this.global_user[name]; |
| 95 | } |
| 96 | if (this.global.hasOwnProperty(name)) { |
| 97 | return this.global[name]; |
| 98 | } |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | DygraphOptions.prototype.findForAxis = function(name, axis) { |
| 103 | |
| 104 | var axisIdx = (axis == "y2" || axis == "y2" || axis == 1) ? 1 : 0; |
| 105 | |
| 106 | var axisOptions = this.axes[axisIdx]; |
| 107 | if (axisOptions.hasOwnProperty(name)) { |
| 108 | return axisOptions[name]; |
| 109 | } |
| 110 | return this.find(name); |
| 111 | } |
| 112 | |
| 113 | DygraphOptions.prototype.findForSeries = function(name, series) { |
| 114 | // Honors indexes as series. |
| 115 | var seriesName = (typeof(series) == "number") ? this.labels[series] : series; |
| 116 | |
| 117 | if (!this.series.hasOwnProperty(seriesName)) { |
| 118 | throw "Unknown series: " + series; |
| 119 | } |
| 120 | |
| 121 | var seriesObj = this.series[seriesName]; |
| 122 | var seriesOptions = seriesObj["options"]; |
| 123 | if (seriesOptions.hasOwnProperty(name)) { |
| 124 | return seriesOptions[name]; |
| 125 | } |
| 126 | return this.findForAxis(name, seriesObj["yAxis"]); |
| 127 | } |
| 128 | |