| 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2011 Dan Vanderkam (danvdk@gmail.com) |
| 4 | * MIT-licensed (http://opensource.org/licenses/MIT) |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @fileoverview Based on PlotKitLayout, but modified to meet the needs of |
| 9 | * dygraphs. |
| 10 | */ |
| 11 | |
| 12 | /*jshint globalstrict: true */ |
| 13 | /*global Dygraph:false */ |
| 14 | "use strict"; |
| 15 | |
| 16 | /** |
| 17 | * Creates a new DygraphLayout object. |
| 18 | * |
| 19 | * This class contains all the data to be charted. |
| 20 | * It uses data coordinates, but also records the chart range (in data |
| 21 | * coordinates) and hence is able to calculate percentage positions ('In this |
| 22 | * view, Point A lies 25% down the x-axis.') |
| 23 | * |
| 24 | * Two things that it does not do are: |
| 25 | * 1. Record pixel coordinates for anything. |
| 26 | * 2. (oddly) determine anything about the layout of chart elements. |
| 27 | * |
| 28 | * The naming is a vestige of Dygraph's original PlotKit roots. |
| 29 | * |
| 30 | * @constructor |
| 31 | */ |
| 32 | var DygraphLayout = function(dygraph) { |
| 33 | this.dygraph_ = dygraph; |
| 34 | /** |
| 35 | * Array of points for each series. |
| 36 | * |
| 37 | * [series index][row index in series] = |Point| structure, |
| 38 | * where series index refers to visible series only, and the |
| 39 | * point index is for the reduced set of points for the current |
| 40 | * zoom region (including one point just outside the window). |
| 41 | * All points in the same row index share the same X value. |
| 42 | * |
| 43 | * @type {Array.<Array.<Dygraph.PointType>>} |
| 44 | */ |
| 45 | this.points = []; |
| 46 | this.setNames = []; |
| 47 | this.annotations = []; |
| 48 | this.yAxes_ = null; |
| 49 | |
| 50 | // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and |
| 51 | // yticks are outputs. Clean this up. |
| 52 | this.xTicks_ = null; |
| 53 | this.yTicks_ = null; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * Add points for a single series. |
| 58 | * |
| 59 | * @param {string} setname Name of the series. |
| 60 | * @param {Array.<Dygraph.PointType>} set_xy Points for the series. |
| 61 | */ |
| 62 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { |
| 63 | this.points.push(set_xy); |
| 64 | this.setNames.push(setname); |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Returns the box which the chart should be drawn in. This is the canvas's |
| 69 | * box, less space needed for the axis and chart labels. |
| 70 | * |
| 71 | * @return {{x: number, y: number, w: number, h: number}} |
| 72 | */ |
| 73 | DygraphLayout.prototype.getPlotArea = function() { |
| 74 | return this.area_; |
| 75 | }; |
| 76 | |
| 77 | // Compute the box which the chart should be drawn in. This is the canvas's |
| 78 | // box, less space needed for axis, chart labels, and other plug-ins. |
| 79 | // NOTE: This should only be called by Dygraph.predraw_(). |
| 80 | DygraphLayout.prototype.computePlotArea = function() { |
| 81 | var area = { |
| 82 | // TODO(danvk): per-axis setting. |
| 83 | x: 0, |
| 84 | y: 0 |
| 85 | }; |
| 86 | |
| 87 | area.w = this.dygraph_.width_ - area.x - this.dygraph_.getOption('rightGap'); |
| 88 | area.h = this.dygraph_.height_; |
| 89 | |
| 90 | // Let plugins reserve space. |
| 91 | var e = { |
| 92 | chart_div: this.dygraph_.graphDiv, |
| 93 | reserveSpaceLeft: function(px) { |
| 94 | var r = { |
| 95 | x: area.x, |
| 96 | y: area.y, |
| 97 | w: px, |
| 98 | h: area.h |
| 99 | }; |
| 100 | area.x += px; |
| 101 | area.w -= px; |
| 102 | return r; |
| 103 | }, |
| 104 | reserveSpaceRight: function(px) { |
| 105 | var r = { |
| 106 | x: area.x + area.w - px, |
| 107 | y: area.y, |
| 108 | w: px, |
| 109 | h: area.h |
| 110 | }; |
| 111 | area.w -= px; |
| 112 | return r; |
| 113 | }, |
| 114 | reserveSpaceTop: function(px) { |
| 115 | var r = { |
| 116 | x: area.x, |
| 117 | y: area.y, |
| 118 | w: area.w, |
| 119 | h: px |
| 120 | }; |
| 121 | area.y += px; |
| 122 | area.h -= px; |
| 123 | return r; |
| 124 | }, |
| 125 | reserveSpaceBottom: function(px) { |
| 126 | var r = { |
| 127 | x: area.x, |
| 128 | y: area.y + area.h - px, |
| 129 | w: area.w, |
| 130 | h: px |
| 131 | }; |
| 132 | area.h -= px; |
| 133 | return r; |
| 134 | }, |
| 135 | chartRect: function() { |
| 136 | return {x:area.x, y:area.y, w:area.w, h:area.h}; |
| 137 | } |
| 138 | }; |
| 139 | this.dygraph_.cascadeEvents_('layout', e); |
| 140 | |
| 141 | this.area_ = area; |
| 142 | }; |
| 143 | |
| 144 | DygraphLayout.prototype.setAnnotations = function(ann) { |
| 145 | // The Dygraph object's annotations aren't parsed. We parse them here and |
| 146 | // save a copy. If there is no parser, then the user must be using raw format. |
| 147 | this.annotations = []; |
| 148 | var parse = this.dygraph_.getOption('xValueParser') || function(x) { return x; }; |
| 149 | for (var i = 0; i < ann.length; i++) { |
| 150 | var a = {}; |
| 151 | if (!ann[i].xval && ann[i].x === undefined) { |
| 152 | this.dygraph_.error("Annotations must have an 'x' property"); |
| 153 | return; |
| 154 | } |
| 155 | if (ann[i].icon && |
| 156 | !(ann[i].hasOwnProperty('width') && |
| 157 | ann[i].hasOwnProperty('height'))) { |
| 158 | this.dygraph_.error("Must set width and height when setting " + |
| 159 | "annotation.icon property"); |
| 160 | return; |
| 161 | } |
| 162 | Dygraph.update(a, ann[i]); |
| 163 | if (!a.xval) a.xval = parse(a.x); |
| 164 | this.annotations.push(a); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | DygraphLayout.prototype.setXTicks = function(xTicks) { |
| 169 | this.xTicks_ = xTicks; |
| 170 | }; |
| 171 | |
| 172 | // TODO(danvk): add this to the Dygraph object's API or move it into Layout. |
| 173 | DygraphLayout.prototype.setYAxes = function (yAxes) { |
| 174 | this.yAxes_ = yAxes; |
| 175 | }; |
| 176 | |
| 177 | DygraphLayout.prototype.evaluate = function() { |
| 178 | this._evaluateLimits(); |
| 179 | this._evaluateLineCharts(); |
| 180 | this._evaluateLineTicks(); |
| 181 | this._evaluateAnnotations(); |
| 182 | }; |
| 183 | |
| 184 | DygraphLayout.prototype._evaluateLimits = function() { |
| 185 | var xlimits = this.dygraph_.xAxisRange(); |
| 186 | this.minxval = xlimits[0]; |
| 187 | this.maxxval = xlimits[1]; |
| 188 | var xrange = xlimits[1] - xlimits[0]; |
| 189 | this.xscale = (xrange !== 0 ? 1 / xrange : 1.0); |
| 190 | |
| 191 | for (var i = 0; i < this.yAxes_.length; i++) { |
| 192 | var axis = this.yAxes_[i]; |
| 193 | axis.minyval = axis.computedValueRange[0]; |
| 194 | axis.maxyval = axis.computedValueRange[1]; |
| 195 | axis.yrange = axis.maxyval - axis.minyval; |
| 196 | axis.yscale = (axis.yrange !== 0 ? 1.0 / axis.yrange : 1.0); |
| 197 | |
| 198 | if (axis.g.getOption("logscale")) { |
| 199 | axis.ylogrange = Dygraph.log10(axis.maxyval) - Dygraph.log10(axis.minyval); |
| 200 | axis.ylogscale = (axis.ylogrange !== 0 ? 1.0 / axis.ylogrange : 1.0); |
| 201 | if (!isFinite(axis.ylogrange) || isNaN(axis.ylogrange)) { |
| 202 | axis.g.error('axis ' + i + ' of graph at ' + axis.g + |
| 203 | ' can\'t be displayed in log scale for range [' + |
| 204 | axis.minyval + ' - ' + axis.maxyval + ']'); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | DygraphLayout._calcYNormal = function(axis, value, logscale) { |
| 211 | if (logscale) { |
| 212 | var x = 1.0 - ((Dygraph.log10(value) - Dygraph.log10(axis.minyval)) * axis.ylogscale); |
| 213 | return isFinite(x) ? x : NaN; // shim for v8 issue; see pull request 276 |
| 214 | } else { |
| 215 | return 1.0 - ((value - axis.minyval) * axis.yscale); |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | DygraphLayout.prototype._evaluateLineCharts = function() { |
| 220 | var isStacked = this.dygraph_.getOption("stackedGraph"); |
| 221 | |
| 222 | for (var setIdx = 0; setIdx < this.points.length; setIdx++) { |
| 223 | var points = this.points[setIdx]; |
| 224 | var setName = this.setNames[setIdx]; |
| 225 | var connectSeparated = this.dygraph_.getOption('connectSeparatedPoints', setName); |
| 226 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
| 227 | // TODO (konigsberg): use optionsForAxis instead. |
| 228 | var logscale = this.dygraph_.attributes_.getForSeries("logscale", setName); |
| 229 | |
| 230 | for (var j = 0; j < points.length; j++) { |
| 231 | var point = points[j]; |
| 232 | |
| 233 | // Range from 0-1 where 0 represents left and 1 represents right. |
| 234 | point.x = (point.xval - this.minxval) * this.xscale; |
| 235 | // Range from 0-1 where 0 represents top and 1 represents bottom |
| 236 | var yval = point.yval; |
| 237 | if (isStacked) { |
| 238 | point.y_stacked = DygraphLayout._calcYNormal( |
| 239 | axis, point.yval_stacked, logscale); |
| 240 | if (yval !== null && !isNaN(yval)) { |
| 241 | yval = point.yval_stacked; |
| 242 | } |
| 243 | } |
| 244 | if (yval === null) { |
| 245 | yval = NaN; |
| 246 | if (!connectSeparated) { |
| 247 | point.yval = NaN; |
| 248 | } |
| 249 | } |
| 250 | point.y = DygraphLayout._calcYNormal(axis, yval, logscale); |
| 251 | } |
| 252 | |
| 253 | this.dygraph_.dataHandler_.onLineEvaluated(points, axis, logscale); |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | /** |
| 258 | * Optimized replacement for parseFloat, which was way too slow when almost |
| 259 | * all values were type number, with few edge cases, none of which were strings. |
| 260 | */ |
| 261 | DygraphLayout.parseFloat_ = function(val) { |
| 262 | // parseFloat(null) is NaN |
| 263 | if (val === null) { |
| 264 | return NaN; |
| 265 | } |
| 266 | |
| 267 | // Assume it's a number or NaN. If it's something else, I'll be shocked. |
| 268 | return val; |
| 269 | }; |
| 270 | |
| 271 | DygraphLayout.prototype._evaluateLineTicks = function() { |
| 272 | var i, tick, label, pos; |
| 273 | this.xticks = []; |
| 274 | for (i = 0; i < this.xTicks_.length; i++) { |
| 275 | tick = this.xTicks_[i]; |
| 276 | label = tick.label; |
| 277 | pos = this.xscale * (tick.v - this.minxval); |
| 278 | if ((pos >= 0.0) && (pos < 1.0)) { |
| 279 | this.xticks.push([pos, label]); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | this.yticks = []; |
| 284 | for (i = 0; i < this.yAxes_.length; i++ ) { |
| 285 | var axis = this.yAxes_[i]; |
| 286 | for (var j = 0; j < axis.ticks.length; j++) { |
| 287 | tick = axis.ticks[j]; |
| 288 | label = tick.label; |
| 289 | pos = this.dygraph_.toPercentYCoord(tick.v, i); |
| 290 | if ((pos > 0.0) && (pos <= 1.0)) { |
| 291 | this.yticks.push([i, pos, label]); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | DygraphLayout.prototype._evaluateAnnotations = function() { |
| 298 | // Add the annotations to the point to which they belong. |
| 299 | // Make a map from (setName, xval) to annotation for quick lookups. |
| 300 | var i; |
| 301 | var annotations = {}; |
| 302 | for (i = 0; i < this.annotations.length; i++) { |
| 303 | var a = this.annotations[i]; |
| 304 | annotations[a.xval + "," + a.series] = a; |
| 305 | } |
| 306 | |
| 307 | this.annotated_points = []; |
| 308 | |
| 309 | // Exit the function early if there are no annotations. |
| 310 | if (!this.annotations || !this.annotations.length) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | // TODO(antrob): loop through annotations not points. |
| 315 | for (var setIdx = 0; setIdx < this.points.length; setIdx++) { |
| 316 | var points = this.points[setIdx]; |
| 317 | for (i = 0; i < points.length; i++) { |
| 318 | var p = points[i]; |
| 319 | var k = p.xval + "," + p.name; |
| 320 | if (k in annotations) { |
| 321 | p.annotation = annotations[k]; |
| 322 | this.annotated_points.push(p); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | /** |
| 329 | * Convenience function to remove all the data sets from a graph |
| 330 | */ |
| 331 | DygraphLayout.prototype.removeAllDatasets = function() { |
| 332 | delete this.points; |
| 333 | delete this.setNames; |
| 334 | delete this.setPointsLengths; |
| 335 | delete this.setPointsOffsets; |
| 336 | this.points = []; |
| 337 | this.setNames = []; |
| 338 | this.setPointsLengths = []; |
| 339 | this.setPointsOffsets = []; |
| 340 | }; |