| 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 | /** |
| 13 | * Creates a new DygraphLayout object. |
| 14 | * |
| 15 | * This class contains all the data to be charted. |
| 16 | * It uses data coordinates, but also records the chart range (in data |
| 17 | * coordinates) and hence is able to calculate percentage positions ('In this |
| 18 | * view, Point A lies 25% down the x-axis.') |
| 19 | * |
| 20 | * Two things that it does not do are: |
| 21 | * 1. Record pixel coordinates for anything. |
| 22 | * 2. (oddly) determine anything about the layout of chart elements. |
| 23 | * |
| 24 | * The naming is a vestige of Dygraph's original PlotKit roots. |
| 25 | * |
| 26 | * @constructor |
| 27 | */ |
| 28 | DygraphLayout = function(dygraph) { |
| 29 | this.dygraph_ = dygraph; |
| 30 | this.datasets = new Array(); |
| 31 | this.annotations = new Array(); |
| 32 | this.yAxes_ = null; |
| 33 | |
| 34 | // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and |
| 35 | // yticks are outputs. Clean this up. |
| 36 | this.xTicks_ = null; |
| 37 | this.yTicks_ = null; |
| 38 | }; |
| 39 | |
| 40 | DygraphLayout.prototype.attr_ = function(name) { |
| 41 | return this.dygraph_.attr_(name); |
| 42 | }; |
| 43 | |
| 44 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { |
| 45 | this.datasets[setname] = set_xy; |
| 46 | }; |
| 47 | |
| 48 | DygraphLayout.prototype.getPlotArea = function() { |
| 49 | return this.computePlotArea_(); |
| 50 | } |
| 51 | |
| 52 | // Compute the box which the chart should be drawn in. This is the canvas's |
| 53 | // box, less space needed for axis and chart labels. |
| 54 | DygraphLayout.prototype.computePlotArea_ = function() { |
| 55 | var area = { |
| 56 | // TODO(danvk): per-axis setting. |
| 57 | x: 0, |
| 58 | y: 0 |
| 59 | }; |
| 60 | if (this.attr_('drawYAxis')) { |
| 61 | area.x = this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize'); |
| 62 | } |
| 63 | |
| 64 | area.w = this.dygraph_.width_ - area.x - this.attr_('rightGap'); |
| 65 | area.h = this.dygraph_.height_; |
| 66 | if (this.attr_('drawXAxis')) { |
| 67 | if (this.attr_('xAxisHeight')) { |
| 68 | area.h -= this.attr_('xAxisHeight'); |
| 69 | } else { |
| 70 | area.h -= this.attr_('axisLabelFontSize') + 2 * this.attr_('axisTickSize'); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Shrink the drawing area to accomodate additional y-axes. |
| 75 | if (this.dygraph_.numAxes() == 2) { |
| 76 | // TODO(danvk): per-axis setting. |
| 77 | area.w -= (this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize')); |
| 78 | } else if (this.dygraph_.numAxes() > 2) { |
| 79 | this.dygraph_.error("Only two y-axes are supported at this time. (Trying " + |
| 80 | "to use " + this.dygraph_.numAxes() + ")"); |
| 81 | } |
| 82 | |
| 83 | // Add space for chart labels: title, xlabel and ylabel. |
| 84 | if (this.attr_('title')) { |
| 85 | area.h -= this.attr_('titleHeight'); |
| 86 | area.y += this.attr_('titleHeight'); |
| 87 | } |
| 88 | if (this.attr_('xlabel')) { |
| 89 | area.h -= this.attr_('xLabelHeight'); |
| 90 | } |
| 91 | if (this.attr_('ylabel')) { |
| 92 | // It would make sense to shift the chart here to make room for the y-axis |
| 93 | // label, but the default yAxisLabelWidth is large enough that this results |
| 94 | // in overly-padded charts. The y-axis label should fit fine. If it |
| 95 | // doesn't, the yAxisLabelWidth option can be increased. |
| 96 | } |
| 97 | |
| 98 | // Add space for range selector, if needed. |
| 99 | if (this.attr_('showRangeSelector')) { |
| 100 | area.h -= this.attr_('rangeSelectorHeight') + 4; |
| 101 | } |
| 102 | |
| 103 | return area; |
| 104 | }; |
| 105 | |
| 106 | DygraphLayout.prototype.setAnnotations = function(ann) { |
| 107 | // The Dygraph object's annotations aren't parsed. We parse them here and |
| 108 | // save a copy. If there is no parser, then the user must be using raw format. |
| 109 | this.annotations = []; |
| 110 | var parse = this.attr_('xValueParser') || function(x) { return x; }; |
| 111 | for (var i = 0; i < ann.length; i++) { |
| 112 | var a = {}; |
| 113 | if (!ann[i].xval && !ann[i].x) { |
| 114 | this.dygraph_.error("Annotations must have an 'x' property"); |
| 115 | return; |
| 116 | } |
| 117 | if (ann[i].icon && |
| 118 | !(ann[i].hasOwnProperty('width') && |
| 119 | ann[i].hasOwnProperty('height'))) { |
| 120 | this.dygraph_.error("Must set width and height when setting " + |
| 121 | "annotation.icon property"); |
| 122 | return; |
| 123 | } |
| 124 | Dygraph.update(a, ann[i]); |
| 125 | if (!a.xval) a.xval = parse(a.x); |
| 126 | this.annotations.push(a); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | DygraphLayout.prototype.setXTicks = function(xTicks) { |
| 131 | this.xTicks_ = xTicks; |
| 132 | }; |
| 133 | |
| 134 | // TODO(danvk): add this to the Dygraph object's API or move it into Layout. |
| 135 | DygraphLayout.prototype.setYAxes = function (yAxes) { |
| 136 | this.yAxes_ = yAxes; |
| 137 | }; |
| 138 | |
| 139 | DygraphLayout.prototype.setDateWindow = function(dateWindow) { |
| 140 | this.dateWindow_ = dateWindow; |
| 141 | }; |
| 142 | |
| 143 | DygraphLayout.prototype.evaluate = function() { |
| 144 | this._evaluateLimits(); |
| 145 | this._evaluateLineCharts(); |
| 146 | this._evaluateLineTicks(); |
| 147 | this._evaluateAnnotations(); |
| 148 | }; |
| 149 | |
| 150 | DygraphLayout.prototype._evaluateLimits = function() { |
| 151 | this.minxval = this.maxxval = null; |
| 152 | if (this.dateWindow_) { |
| 153 | this.minxval = this.dateWindow_[0]; |
| 154 | this.maxxval = this.dateWindow_[1]; |
| 155 | } else { |
| 156 | for (var name in this.datasets) { |
| 157 | if (!this.datasets.hasOwnProperty(name)) continue; |
| 158 | var series = this.datasets[name]; |
| 159 | if (series.length > 1) { |
| 160 | var x1 = series[0][0]; |
| 161 | if (!this.minxval || x1 < this.minxval) this.minxval = x1; |
| 162 | |
| 163 | var x2 = series[series.length - 1][0]; |
| 164 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | this.xrange = this.maxxval - this.minxval; |
| 169 | this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0); |
| 170 | |
| 171 | for (var i = 0; i < this.yAxes_.length; i++) { |
| 172 | var axis = this.yAxes_[i]; |
| 173 | axis.minyval = axis.computedValueRange[0]; |
| 174 | axis.maxyval = axis.computedValueRange[1]; |
| 175 | axis.yrange = axis.maxyval - axis.minyval; |
| 176 | axis.yscale = (axis.yrange != 0 ? 1.0 / axis.yrange : 1.0); |
| 177 | |
| 178 | if (axis.g.attr_("logscale")) { |
| 179 | axis.ylogrange = Dygraph.log10(axis.maxyval) - Dygraph.log10(axis.minyval); |
| 180 | axis.ylogscale = (axis.ylogrange != 0 ? 1.0 / axis.ylogrange : 1.0); |
| 181 | if (!isFinite(axis.ylogrange) || isNaN(axis.ylogrange)) { |
| 182 | axis.g.error('axis ' + i + ' of graph at ' + axis.g + |
| 183 | ' can\'t be displayed in log scale for range [' + |
| 184 | axis.minyval + ' - ' + axis.maxyval + ']'); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | DygraphLayout._calcYNormal = function(axis, value) { |
| 191 | if (axis.logscale) { |
| 192 | return 1.0 - ((Dygraph.log10(value) - Dygraph.log10(axis.minyval)) * axis.ylogscale); |
| 193 | } else { |
| 194 | return 1.0 - ((value - axis.minyval) * axis.yscale); |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | DygraphLayout.prototype._evaluateLineCharts = function() { |
| 199 | // add all the rects |
| 200 | this.points = new Array(); |
| 201 | // An array to keep track of how many points will be drawn for each set. |
| 202 | // This will allow for the canvas renderer to not have to check every point |
| 203 | // for every data set since the points are added in order of the sets in |
| 204 | // datasets. |
| 205 | this.setPointsLengths = new Array(); |
| 206 | |
| 207 | for (var setName in this.datasets) { |
| 208 | if (!this.datasets.hasOwnProperty(setName)) continue; |
| 209 | |
| 210 | var dataset = this.datasets[setName]; |
| 211 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
| 212 | |
| 213 | var setPointsLength = 0; |
| 214 | |
| 215 | for (var j = 0; j < dataset.length; j++) { |
| 216 | var item = dataset[j]; |
| 217 | var xValue = parseFloat(dataset[j][0]); |
| 218 | var yValue = parseFloat(dataset[j][1]); |
| 219 | |
| 220 | // Range from 0-1 where 0 represents left and 1 represents right. |
| 221 | var xNormal = (xValue - this.minxval) * this.xscale; |
| 222 | // Range from 0-1 where 0 represents top and 1 represents bottom |
| 223 | var yNormal = DygraphLayout._calcYNormal(axis, yValue); |
| 224 | |
| 225 | var point = { |
| 226 | // TODO(danvk): here |
| 227 | x: xNormal, |
| 228 | y: yNormal, |
| 229 | xval: xValue, |
| 230 | yval: yValue, |
| 231 | name: setName |
| 232 | }; |
| 233 | this.points.push(point); |
| 234 | setPointsLength += 1; |
| 235 | } |
| 236 | this.setPointsLengths.push(setPointsLength); |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | DygraphLayout.prototype._evaluateLineTicks = function() { |
| 241 | this.xticks = new Array(); |
| 242 | for (var i = 0; i < this.xTicks_.length; i++) { |
| 243 | var tick = this.xTicks_[i]; |
| 244 | var label = tick.label; |
| 245 | var pos = this.xscale * (tick.v - this.minxval); |
| 246 | if ((pos >= 0.0) && (pos <= 1.0)) { |
| 247 | this.xticks.push([pos, label]); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | this.yticks = new Array(); |
| 252 | for (var i = 0; i < this.yAxes_.length; i++ ) { |
| 253 | var axis = this.yAxes_[i]; |
| 254 | for (var j = 0; j < axis.ticks.length; j++) { |
| 255 | var tick = axis.ticks[j]; |
| 256 | var label = tick.label; |
| 257 | var pos = this.dygraph_.toPercentYCoord(tick.v, i); |
| 258 | if ((pos >= 0.0) && (pos <= 1.0)) { |
| 259 | this.yticks.push([i, pos, label]); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Behaves the same way as PlotKit.Layout, but also copies the errors |
| 268 | * @private |
| 269 | */ |
| 270 | DygraphLayout.prototype.evaluateWithError = function() { |
| 271 | this.evaluate(); |
| 272 | if (!(this.attr_('errorBars') || this.attr_('customBars'))) return; |
| 273 | |
| 274 | // Copy over the error terms |
| 275 | var i = 0; // index in this.points |
| 276 | for (var setName in this.datasets) { |
| 277 | if (!this.datasets.hasOwnProperty(setName)) continue; |
| 278 | var j = 0; |
| 279 | var dataset = this.datasets[setName]; |
| 280 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
| 281 | for (var j = 0; j < dataset.length; j++, i++) { |
| 282 | var item = dataset[j]; |
| 283 | var xv = parseFloat(item[0]); |
| 284 | var yv = parseFloat(item[1]); |
| 285 | |
| 286 | if (xv == this.points[i].xval && |
| 287 | yv == this.points[i].yval) { |
| 288 | var errorMinus = parseFloat(item[2]); |
| 289 | var errorPlus = parseFloat(item[3]); |
| 290 | |
| 291 | var yv_minus = yv - errorMinus; |
| 292 | var yv_plus = yv + errorPlus; |
| 293 | this.points[i].y_top = DygraphLayout._calcYNormal(axis, yv_minus); |
| 294 | this.points[i].y_bottom = DygraphLayout._calcYNormal(axis, yv_plus); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | }; |
| 299 | |
| 300 | DygraphLayout.prototype._evaluateAnnotations = function() { |
| 301 | // Add the annotations to the point to which they belong. |
| 302 | // Make a map from (setName, xval) to annotation for quick lookups. |
| 303 | var annotations = {}; |
| 304 | for (var i = 0; i < this.annotations.length; i++) { |
| 305 | var a = this.annotations[i]; |
| 306 | annotations[a.xval + "," + a.series] = a; |
| 307 | } |
| 308 | |
| 309 | this.annotated_points = []; |
| 310 | |
| 311 | // Exit the function early if there are no annotations. |
| 312 | if (!this.annotations || !this.annotations.length) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | // TODO(antrob): loop through annotations not points. |
| 317 | for (var i = 0; i < this.points.length; i++) { |
| 318 | var p = this.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 | * Convenience function to remove all the data sets from a graph |
| 329 | */ |
| 330 | DygraphLayout.prototype.removeAllDatasets = function() { |
| 331 | delete this.datasets; |
| 332 | this.datasets = new Array(); |
| 333 | }; |
| 334 | |
| 335 | /** |
| 336 | * Return a copy of the point at the indicated index, with its yval unstacked. |
| 337 | * @param int index of point in layout_.points |
| 338 | */ |
| 339 | DygraphLayout.prototype.unstackPointAtIndex = function(idx) { |
| 340 | var point = this.points[idx]; |
| 341 | |
| 342 | // Clone the point since we modify it |
| 343 | var unstackedPoint = {}; |
| 344 | for (var i in point) { |
| 345 | unstackedPoint[i] = point[i]; |
| 346 | } |
| 347 | |
| 348 | if (!this.attr_("stackedGraph")) { |
| 349 | return unstackedPoint; |
| 350 | } |
| 351 | |
| 352 | // The unstacked yval is equal to the current yval minus the yval of the |
| 353 | // next point at the same xval. |
| 354 | for (var i = idx+1; i < this.points.length; i++) { |
| 355 | if (this.points[i].xval == point.xval) { |
| 356 | unstackedPoint.yval -= this.points[i].yval; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return unstackedPoint; |
| 362 | } |