X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph.js;h=87a28ad97caf2e2caac92d24b152e58b86254be1;hb=4e265dc47d5c8f2597c1320f0746bd7825741658;hp=6369f1143ee7a362dea4347724aae6105381ddd4;hpb=6a1aa64f6d22473e0357ad1cd7bd93259d899a69;p=dygraphs.git diff --git a/dygraph.js b/dygraph.js index 6369f11..87a28ad 100644 --- a/dygraph.js +++ b/dygraph.js @@ -144,7 +144,8 @@ DateGraph.prototype.__init__ = function(div, file, labels, attrs) { this.createRollInterface_(); this.createDragInterface_(); - MochiKit.DOM.addLoadEvent(this.start_()); + // connect(window, 'onload', this, function(e) { this.start_(); }); + this.start_(); }; /** @@ -257,13 +258,18 @@ DateGraph.prototype.createStatusMessage_ = function(){ */ DateGraph.prototype.createRollInterface_ = function() { var padding = this.plotter_.options.padding; + if (typeof this.attrs_.showRoller == 'undefined') { + this.attrs_.showRoller = false; + } + var display = this.attrs_.showRoller ? "block" : "none"; var textAttr = { "type": "text", "size": "2", "value": this.rollPeriod_, "style": { "position": "absolute", "zIndex": 10, "top": (this.height_ - 25 - padding.bottom) + "px", - "left": (padding.left+1) + "px" } + "left": (padding.left+1) + "px", + "display": display } }; var roller = MochiKit.DOM.INPUT(textAttr); var pa = this.graphDiv; @@ -290,8 +296,8 @@ DateGraph.prototype.createDragInterface_ = function() { var prevEndX = null; // Utility function to convert page-wide coordinates to canvas coords - var px = PlotKit.Base.findPosX(this.canvas_); - var py = PlotKit.Base.findPosY(this.canvas_); + var px = 0; + var py = 0; var getX = function(e) { return e.mouse().page.x - px }; var getY = function(e) { return e.mouse().page.y - py }; @@ -309,6 +315,8 @@ DateGraph.prototype.createDragInterface_ = function() { // Track the beginning of drag events connect(this.hidden_, 'onmousedown', function(event) { mouseDown = true; + px = PlotKit.Base.findPosX(self.canvas_); + py = PlotKit.Base.findPosY(self.canvas_); dragStartX = getX(event); dragStartY = getY(event); }); @@ -367,7 +375,9 @@ DateGraph.prototype.createDragInterface_ = function() { self.drawGraph_(self.rawData_); var minDate = self.rawData_[0][0]; var maxDate = self.rawData_[self.rawData_.length - 1][0]; - self.zoomCallback_(minDate, maxDate); + if (self.zoomCallback_) { + self.zoomCallback_(minDate, maxDate); + } }); }; @@ -426,7 +436,9 @@ DateGraph.prototype.doZoom_ = function(lowX, highX) { this.dateWindow_ = [minDate, maxDate]; this.drawGraph_(this.rawData_); - this.zoomCallback_(minDate, maxDate); + if (this.zoomCallback_) { + this.zoomCallback_(minDate, maxDate); + } }; /** @@ -841,13 +853,29 @@ DateGraph.prototype.rollingAverage = function(originalData, rollPeriod) { } } } else if (this.customBars_) { - // just ignore the rolling for now. - // TODO(danvk): do something reasonable. + var low = 0; + var mid = 0; + var high = 0; + var count = 0; for (var i = 0; i < originalData.length; i++) { var data = originalData[i][1]; var y = data[1]; rollingData[i] = [originalData[i][0], [y, y - data[0], data[2] - y]]; - } + + low += data[0]; + mid += y; + high += data[2]; + count += 1; + if (i - rollPeriod >= 0) { + var prev = originalData[i - rollPeriod]; + low -= prev[1][0]; + mid -= prev[1][1]; + high -= prev[1][2]; + count -= 1; + } + rollingData[i] = [originalData[i][0], [ 1.0 * mid / count, + 1.0 * (mid - low) / count, + 1.0 * (high - mid) / count ]]; } else { // Calculate the rolling average for the first rollPeriod - 1 points where // there is not enough data to roll over the full number of days @@ -911,6 +939,9 @@ DateGraph.prototype.dateParser = function(dateStr) { var dateStrSlashed; if (dateStr.search("-") != -1) { dateStrSlashed = dateStr.replace("-", "/", "g"); + while (dateStrSlashed.search("-") != -1) { + dateStrSlashed = dateStrSlashed.replace("-", "/"); + } } else if (dateStr.search("/") != -1) { return Date.parse(dateStr); } else {