/**
* Sets some PlotKit.CanvasRenderer options
* @param {Object} element The canvas to attach to
+ * @param {Object} elementContext The 2d context of the canvas (injected so it
+ * can be mocked for testing.)
* @param {Layout} layout The DygraphLayout object for this graph.
* @param {Object} options Options to pass on to CanvasRenderer
*/
-DygraphCanvasRenderer = function(dygraph, element, layout, options) {
+DygraphCanvasRenderer = function(dygraph, element, elementContext, layout,
+ options) {
// TODO(danvk): remove options, just use dygraph.attr_.
this.dygraph_ = dygraph;
this.layout = layout;
this.element = element;
+ this.elementContext = elementContext;
this.container = this.element.parentNode;
this.height = this.element.height;
// Set up a clipping area for the canvas (and the interaction canvas).
// This ensures that we don't overdraw.
- var ctx = this.dygraph_.canvas_.getContext("2d");
+ var ctx = this.dygraph_.canvas_ctx_;
ctx.beginPath();
ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
ctx.clip();
- ctx = this.dygraph_.hidden_.getContext("2d");
+ ctx = this.dygraph_.hidden_ctx_;
ctx.beginPath();
ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
ctx.clip();
this.clearDelay.cancel();
this.clearDelay = null;
}
- var context = this.element.getContext("2d");
+ var context = this.elementContext;
}
catch (e) {
// TODO(danvk): this is broken, since MochiKit.Async is gone.
}
}
- var context = this.element.getContext("2d");
+ var context = this.elementContext;
context.clearRect(0, 0, this.width, this.height);
for (var i = 0; i < this.xlabels.length; i++) {
DygraphCanvasRenderer.prototype.render = function() {
// 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");
+ var ctx = this.elementContext;
function halfUp(x){return Math.round(x)+0.5};
function halfDown(y){return Math.round(y)-0.5};
function halfUp(x){return Math.round(x)+0.5};
function halfDown(y){return Math.round(y)-0.5};
- var context = this.element.getContext("2d");
+ var context = this.elementContext;
var labelStyle = {
"position": "absolute",
this.container.appendChild(div);
this.annotations.push(div);
- var ctx = this.element.getContext("2d");
+ var ctx = this.elementContext;
ctx.strokeStyle = this.colors[p.name];
ctx.beginPath();
if (!a.attachAtBottom) {
*/
DygraphCanvasRenderer.prototype._renderLineChart = function() {
// TODO(danvk): use this.attr_ for many of these.
- var context = this.element.getContext("2d");
+ var context = this.elementContext;
var colorCount = this.options.colorScheme.length;
var colorScheme = this.options.colorScheme;
var fillAlpha = this.options.fillAlpha;
// Used for initializing annotation CSS rules only once.
Dygraph.addedAnnotationCSS = false;
+/**
+ * Return the 2d context for a dygraph canvas.
+ *
+ * This method is only exposed for the sake of replacing the function in
+ * automated tests, e.g.
+ *
+ * var oldFunc = Dygraph.getContext();
+ * Dygraph.getContext = function(canvas) {
+ * var realContext = oldFunc(canvas);
+ * return new Proxy(realContext);
+ * };
+ */
+Dygraph.getContext = function(canvas) {
+ return canvas.getContext("2d");
+};
+
Dygraph.prototype.__old_init__ = function(div, file, labels, attrs) {
// Labels is no longer a constructor parameter, since it's typically set
// directly from the data source. It also conains a name for the x-axis,
this.canvas_.style.width = this.width_ + "px"; // for IE
this.canvas_.style.height = this.height_ + "px"; // for IE
+ this.canvas_ctx_ = Dygraph.getContext(this.canvas_);
+
// ... and for static parts of the chart.
this.hidden_ = this.createPlotKitCanvas_(this.canvas_);
+ this.hidden_ctx_ = Dygraph.getContext(this.hidden_);
// The interactive parts of the graph are drawn on top of the chart.
this.graphDiv.appendChild(this.hidden_);
g.doZoomY_(Math.min(context.dragStartY, context.dragEndY),
Math.max(context.dragStartY, context.dragEndY));
} else {
- g.canvas_.getContext("2d").clearRect(0, 0,
- g.canvas_.width,
- g.canvas_.height);
+ g.canvas_ctx_.clearRect(0, 0, g.canvas_.width, g.canvas_.height);
}
context.dragStartX = null;
context.dragStartY = null;
Dygraph.prototype.drawZoomRect_ = function(direction, startX, endX, startY,
endY, prevDirection, prevEndX,
prevEndY) {
- var ctx = this.canvas_.getContext("2d");
+ var ctx = this.canvas_ctx_;
// Clean up from the previous rect if necessary
if (prevDirection == Dygraph.HORIZONTAL) {
*/
Dygraph.prototype.updateSelection_ = function() {
// Clear the previously drawn vertical, if there is one
- var ctx = this.canvas_.getContext("2d");
+ var ctx = this.canvas_ctx_;
if (this.previousVerticalX_ >= 0) {
// Determine the maximum highlight circle size.
var maxCircleSize = 0;
*/
Dygraph.prototype.clearSelection = function() {
// Get rid of the overlay data
- var ctx = this.canvas_.getContext("2d");
- ctx.clearRect(0, 0, this.width_, this.height_);
+ this.canvas_ctx_.clearRect(0, 0, this.width_, this.height_);
this.setLegendHTML_();
this.selPoints_ = [];
this.lastx_ = -1;
// Create a new plotter.
if (this.plotter_) this.plotter_.clear();
this.plotter_ = new DygraphCanvasRenderer(this,
- this.hidden_, this.layout_,
+ this.hidden_,
+ this.hidden_ctx_,
+ this.layout_,
this.renderOptions_);
// The roller sits in the bottom left corner of the chart. We don't know where