X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph.js;h=f05ac474c64b59c54e4124b790b5e1ff243b16d4;hb=806e1ea5f693c8e0788094faf0d2029a1fa8c836;hp=442c00ce09359c28f7ba46efbbea2b8094517355;hpb=c28088bca853c359d62bd49d29b05a238423f354;p=dygraphs.git diff --git a/dygraph.js b/dygraph.js index 442c00c..f05ac47 100644 --- a/dygraph.js +++ b/dygraph.js @@ -44,7 +44,7 @@ */ /*jshint globalstrict: true */ -/*global DygraphLayout:false, DygraphCanvasRenderer:false, DygraphOptions:false, G_vmlCanvasManager:false */ +/*global DygraphLayout:false, DygraphCanvasRenderer:false, DygraphOptions:false, G_vmlCanvasManager:false,ActiveXObject:false */ "use strict"; /** @@ -62,6 +62,12 @@ * options, see http://dygraphs.com/options.html. */ var Dygraph = function(div, data, opts, opt_fourth_param) { + // These have to go above the "Hack for IE" in __init__ since .ready() can be + // called as soon as the constructor returns. Once support for OldIE is + // dropped, this can go down with the rest of the initializers. + this.is_initial_draw_ = true; + this.readyFns_ = []; + if (opt_fourth_param !== undefined) { // Old versions of dygraphs took in the series labels as a constructor // parameter. This doesn't make sense anymore, but it's easy to continue @@ -74,7 +80,7 @@ var Dygraph = function(div, data, opts, opt_fourth_param) { }; Dygraph.NAME = "Dygraph"; -Dygraph.VERSION = "1.2"; +Dygraph.VERSION = "1.0.0"; Dygraph.__repr__ = function() { return "[" + this.NAME + " " + this.VERSION + "]"; }; @@ -439,7 +445,6 @@ Dygraph.prototype.__init__ = function(div, file, attrs) { this.fractions_ = attrs.fractions || false; this.dateWindow_ = attrs.dateWindow || null; - this.is_initial_draw_ = true; this.annotations_ = []; // Zoomed indicators - These indicate when the graph has been zoomed and on what axis. @@ -1412,7 +1417,7 @@ Dygraph.prototype.drawZoomRect_ = function(direction, startX, endX, startY, if (prevDirection == Dygraph.HORIZONTAL) { ctx.clearRect(Math.min(startX, prevEndX), this.layout_.getPlotArea().y, Math.abs(startX - prevEndX), this.layout_.getPlotArea().h); - } else if (prevDirection == Dygraph.VERTICAL){ + } else if (prevDirection == Dygraph.VERTICAL) { ctx.clearRect(this.layout_.getPlotArea().x, Math.min(startY, prevEndY), this.layout_.getPlotArea().w, Math.abs(startY - prevEndY)); } @@ -1697,7 +1702,7 @@ Dygraph.prototype.eventToDomCoords = function(event) { */ Dygraph.prototype.findClosestRow = function(domX) { var minDistX = Infinity; - var pointIdx = -1, setIdx = -1; + var closestRow = -1; var sets = this.layout_.points; for (var i = 0; i < sets.length; i++) { var points = sets[i]; @@ -1708,14 +1713,12 @@ Dygraph.prototype.findClosestRow = function(domX) { var dist = Math.abs(point.canvasx - domX); if (dist < minDistX) { minDistX = dist; - setIdx = i; - pointIdx = j; + closestRow = point.idx; } } } - // TODO(danvk): remove this function; it's trivial and has only one use. - return this.idxToRow_(setIdx, pointIdx); + return closestRow; }; /** @@ -1732,12 +1735,11 @@ Dygraph.prototype.findClosestRow = function(domX) { */ Dygraph.prototype.findClosestPoint = function(domX, domY) { var minDist = Infinity; - var idx = -1; - var dist, dx, dy, point, closestPoint, closestSeries; + var dist, dx, dy, point, closestPoint, closestSeries, closestRow; for ( var setIdx = this.layout_.points.length - 1 ; setIdx >= 0 ; --setIdx ) { var points = this.layout_.points[setIdx]; for (var i = 0; i < points.length; ++i) { - var point = points[i]; + point = points[i]; if (!Dygraph.isValidPoint(point)) continue; dx = point.canvasx - domX; dy = point.canvasy - domY; @@ -1746,13 +1748,13 @@ Dygraph.prototype.findClosestPoint = function(domX, domY) { minDist = dist; closestPoint = point; closestSeries = setIdx; - idx = i; + closestRow = point.idx; } } } var name = this.layout_.setNames[closestSeries]; return { - row: idx + this.getLeftBoundary_(), + row: closestRow, seriesName: name, point: closestPoint }; @@ -1772,10 +1774,10 @@ Dygraph.prototype.findClosestPoint = function(domX, domY) { */ Dygraph.prototype.findStackedPoint = function(domX, domY) { var row = this.findClosestRow(domX); - var boundary = this.getLeftBoundary_(); - var rowIdx = row - boundary; var closestPoint, closestSeries; for (var setIdx = 0; setIdx < this.layout_.points.length; ++setIdx) { + var boundary = this.getLeftBoundary_(setIdx); + var rowIdx = row - boundary; var points = this.layout_.points[setIdx]; if (rowIdx >= points.length) continue; var p1 = points[rowIdx]; @@ -1852,35 +1854,27 @@ Dygraph.prototype.mouseMove_ = function(event) { callback(event, this.lastx_, this.selPoints_, - this.lastRow_ + this.getLeftBoundary_(), + this.lastRow_, this.highlightSet_); } }; /** - * Fetch left offset from first defined boundaryIds record (see bug #236). + * Fetch left offset from the specified set index or if not passed, the + * first defined boundaryIds record (see bug #236). * @private */ -Dygraph.prototype.getLeftBoundary_ = function() { - for (var i = 0; i < this.boundaryIds_.length; i++) { - if (this.boundaryIds_[i] !== undefined) { - return this.boundaryIds_[i][0]; +Dygraph.prototype.getLeftBoundary_ = function(setIdx) { + if (this.boundaryIds_[setIdx]) { + return this.boundaryIds_[setIdx][0]; + } else { + for (var i = 0; i < this.boundaryIds_.length; i++) { + if (this.boundaryIds_[i] !== undefined) { + return this.boundaryIds_[i][0]; + } } + return 0; } - return 0; -}; - -/** - * Transforms layout_.points index into data row number. - * @param int layout_.points index - * @return int row number, or -1 if none could be found. - * @private - */ -Dygraph.prototype.idxToRow_ = function(setIdx, rowIdx) { - if (rowIdx < 0) return -1; - - var boundary = this.getLeftBoundary_(); - return boundary + rowIdx; }; Dygraph.prototype.animateSelection_ = function(direction) { @@ -2012,18 +2006,15 @@ Dygraph.prototype.setSelection = function(row, opt_seriesName, opt_locked) { // Extract the points we've selected this.selPoints_ = []; - if (row !== false) { - row -= this.getLeftBoundary_(); - } - var changed = false; if (row !== false && row >= 0) { if (row != this.lastRow_) changed = true; this.lastRow_ = row; for (var setIdx = 0; setIdx < this.layout_.points.length; ++setIdx) { var points = this.layout_.points[setIdx]; - if (row < points.length) { - var point = points[row]; + var setRow = row - this.getLeftBoundary_(setIdx); + if (setRow < points.length) { + var point = points[setRow]; if (point.yval !== null) this.selPoints_.push(point); } } @@ -2103,7 +2094,7 @@ Dygraph.prototype.getSelection = function() { var points = this.layout_.points[setIdx]; for (var row = 0; row < points.length; row++) { if (points[row].x == this.selPoints_[0].x) { - return row + this.getLeftBoundary_(); + return points[row].idx; } } } @@ -2226,7 +2217,7 @@ Dygraph.prototype.predraw_ = function() { this.plotter_.clear(); } - if(!this.is_initial_draw_) { + if (!this.is_initial_draw_) { this.canvas_ctx_.restore(); this.hidden_ctx_.restore(); } @@ -2296,6 +2287,7 @@ Dygraph.PointType = undefined; /** * Converts a series to a Point array. * + * @private * @param {Array.)>} series Array where * series[row] = [x,y] or [x, [y, err]] or [x, [y, yplus, yminus]]. * @param {boolean} bars True if error bars or custom bars are being drawn. @@ -2347,6 +2339,7 @@ Dygraph.seriesToPoints_ = function(series, bars, setName, boundaryIdStart) { * to reflect the stacked values. * @param {string} fillMethod Interpolation method, one of 'all', 'inside', or * 'none'. + * @private */ Dygraph.stackPoints_ = function( points, cumulativeYval, seriesExtremes, fillMethod) { @@ -2501,8 +2494,6 @@ Dygraph.prototype.gatherDatasets_ = function(rolledSeries, dateWindow) { isInvalidValue = isValueNull(series[correctedLastIdx]); } - boundaryIds[i-1] = [(firstIdx > 0) ? firstIdx - 1 : firstIdx, - (lastIdx < series.length - 1) ? lastIdx + 1 : lastIdx]; if (correctedFirstIdx!==firstIdx) { firstIdx = correctedFirstIdx; @@ -2510,6 +2501,9 @@ Dygraph.prototype.gatherDatasets_ = function(rolledSeries, dateWindow) { if (correctedLastIdx !== lastIdx) { lastIdx = correctedLastIdx; } + + boundaryIds[i-1] = [firstIdx, lastIdx]; + // .slice's end is exclusive, we want to include lastIdx. series = series.slice(firstIdx, lastIdx + 1); } else { @@ -2623,6 +2617,13 @@ Dygraph.prototype.renderGraph_ = function(is_initial_draw) { if (this.attr_("drawCallback") !== null) { this.attr_("drawCallback")(this, is_initial_draw); } + if (is_initial_draw) { + this.readyFired_ = true; + while (this.readyFns_.length > 0) { + var fn = this.readyFns_.pop(); + fn(this); + } + } }; /** @@ -2853,7 +2854,7 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { } - if(independentTicks) { + if (independentTicks) { axis.independentTicks = independentTicks; var opts = this.optionsViewForAxis_('y' + (i ? '2' : '')); var ticker = opts('ticker'); @@ -3040,7 +3041,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) { } else { // Calculate the rolling average for the first rollPeriod - 1 points where // there is not enough data to roll over the full number of points - if (!this.attr_("errorBars")){ + if (!this.attr_("errorBars")) { if (rollPeriod == 1) { return originalData; } @@ -3545,7 +3546,16 @@ Dygraph.prototype.start_ = function() { if (line_delimiter) { this.loadedEvent_(data); } else { - var req = new XMLHttpRequest(); + // REMOVE_FOR_IE + var req; + if (window.XMLHttpRequest) { + // Firefox, Opera, IE7, and other browsers will use the native object + req = new XMLHttpRequest(); + } else { + // IE 5 and 6 will use the ActiveX control + req = new ActiveXObject("Microsoft.XMLHTTP"); + } + var caller = this; req.onreadystatechange = function () { if (req.readyState == 4) { @@ -3707,9 +3717,10 @@ Dygraph.prototype.resize = function(width, height) { this.height_ = this.maindiv_.clientHeight; } - this.resizeElements_(); - if (old_width != this.width_ || old_height != this.height_) { + // Resizing a canvas erases it, even when the size doesn't change, so + // any resize needs to be followed by a redraw. + this.resizeElements_(); this.predraw_(); } @@ -3777,7 +3788,7 @@ Dygraph.prototype.setAnnotations = function(ann, suppressDraw) { this.annotations_ = ann; if (!this.layout_) { this.warn("Tried to setAnnotations before dygraph was ready. " + - "Try setting them in a drawCallback. See " + + "Try setting them in a ready() block. See " + "dygraphs.com/tests/annotation.html"); return; } @@ -3815,12 +3826,23 @@ Dygraph.prototype.indexFromSetName = function(name) { }; /** - * Get the internal dataset index given its name. These are numbered starting from 0, - * and only count visible sets. - * @private + * Trigger a callback when the dygraph has drawn itself and is ready to be + * manipulated. This is primarily useful when dygraphs has to do an XHR for the + * data (i.e. a URL is passed as the data source) and the chart is drawn + * asynchronously. If the chart has already drawn, the callback will fire + * immediately. + * + * This is a good place to call setAnnotation(). + * + * @param {function(!Dygraph)} callback The callback to trigger when the chart + * is ready. */ -Dygraph.prototype.datasetIndexFromSetName_ = function(name) { - return this.datasetIndex_[this.indexFromSetName(name)]; +Dygraph.prototype.ready = function(callback) { + if (this.is_initial_draw_) { + this.readyFns_.push(callback); + } else { + callback(this); + } }; /**