X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-canvas.js;h=8dc502cb007226f837969ebffdbc0cabaa0b9360;hb=f0f2aef666950ea136e1ede7bc286ccb07c2c6b6;hp=76f0b4417ded18899e50ee9c84ca046fec74899b;hpb=26ca7938c710424b17f217508837f0950a3e45a8;p=dygraphs.git diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 76f0b44..8dc502c 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -4,7 +4,7 @@ /** * @fileoverview Based on PlotKit, but modified to meet the needs of dygraphs. * In particular, support for: - * - grid overlays + * - grid overlays * - error bars * - dygraphs attribute system */ @@ -19,7 +19,7 @@ DygraphLayout = function(dygraph, options) { this.options = {}; // TODO(danvk): remove, use attr_ instead. Dygraph.update(this.options, options ? options : {}); this.datasets = new Array(); - this.annotations = new Array() + this.annotations = new Array(); }; DygraphLayout.prototype.attr_ = function(name) { @@ -111,13 +111,6 @@ DygraphLayout.prototype._evaluateLineCharts = function() { name: setName }; - // limit the x, y values so they do not overdraw - if (point.y <= 0.0) { - point.y = 0.0; - } - if (point.y >= 1.0) { - point.y = 1.0; - } this.points.push(point); } } @@ -308,7 +301,7 @@ DygraphCanvasRenderer = function(dygraph, element, layout, options) { this.area.w -= (this.options.yAxisLabelWidth + 2 * this.options.axisTickSize); } else if (this.dygraph_.numAxes() > 2) { this.dygraph_.error("Only two y-axes are supported at this time. (Trying " + - "to use " + this.layout.yAxes.length + ")"); + "to use " + this.dygraph_.numAxes() + ")"); } this.container.style.position = "relative"; @@ -316,17 +309,21 @@ DygraphCanvasRenderer = function(dygraph, element, layout, options) { // Set up a clipping area for the canvas (and the interaction canvas). // This ensures that we don't overdraw. - var ctx = this.element.getContext("2d"); + var ctx = this.dygraph_.canvas_.getContext("2d"); ctx.beginPath(); ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); ctx.clip(); - var ctx = this.dygraph_.hidden_.getContext("2d"); + ctx = this.dygraph_.hidden_.getContext("2d"); ctx.beginPath(); ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); ctx.clip(); }; +DygraphCanvasRenderer.prototype.attr_ = function(x) { + return this.dygraph_.attr_(x); +}; + DygraphCanvasRenderer.prototype.clear = function() { if (this.isIE) { // VML takes a while to start up, so we just poll every this.IEDelay @@ -350,15 +347,15 @@ DygraphCanvasRenderer.prototype.clear = function() { for (var i = 0; i < this.xlabels.length; i++) { var el = this.xlabels[i]; - el.parentNode.removeChild(el); + if (el.parentNode) el.parentNode.removeChild(el); } for (var i = 0; i < this.ylabels.length; i++) { var el = this.ylabels[i]; - el.parentNode.removeChild(el); + if (el.parentNode) el.parentNode.removeChild(el); } for (var i = 0; i < this.annotations.length; i++) { var el = this.annotations[i]; - el.parentNode.removeChild(el); + if (el.parentNode) el.parentNode.removeChild(el); } this.xlabels = new Array(); this.ylabels = new Array(); @@ -389,11 +386,16 @@ DygraphCanvasRenderer.isSupported = function(canvasName) { * Draw an X/Y grid on top of the existing plot */ DygraphCanvasRenderer.prototype.render = function() { - // Draw the new X/Y grid + // Draw the new X/Y grid. Lines appear crisper when pixels are rounded to + // half-integers. This prevents them from drawing in two rows/cols. var ctx = this.element.getContext("2d"); + function halfUp(x){return Math.round(x)+0.5}; + function halfDown(y){return Math.round(y)-0.5}; if (this.options.underlayCallback) { - this.options.underlayCallback(ctx, this.area, this.layout, this.dygraph_); + // NOTE: we pass the dygraph object to this callback twice to avoid breaking + // users who expect a deprecated form of this callback. + this.options.underlayCallback(ctx, this.area, this.dygraph_, this.dygraph_); } if (this.options.drawYGrid) { @@ -402,9 +404,10 @@ DygraphCanvasRenderer.prototype.render = function() { ctx.strokeStyle = this.options.gridLineColor; ctx.lineWidth = this.options.axisLineWidth; for (var i = 0; i < ticks.length; i++) { - if (ticks[i][0] != 0) continue; // TODO(danvk): per-axis property - var x = this.area.x; - var y = this.area.y + ticks[i][1] * this.area.h; + // TODO(danvk): allow secondary axes to draw a grid, too. + if (ticks[i][0] != 0) continue; + var x = halfUp(this.area.x); + var y = halfDown(this.area.y + ticks[i][1] * this.area.h); ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + this.area.w, y); @@ -419,8 +422,8 @@ DygraphCanvasRenderer.prototype.render = function() { ctx.strokeStyle = this.options.gridLineColor; ctx.lineWidth = this.options.axisLineWidth; for (var i=0; i