Expose function that can be replaced during tests for mocking out the HTML5 canvas...
[dygraphs.git] / dygraph-canvas.js
index 18cd8f6..2874990 100644 (file)
@@ -277,10 +277,13 @@ DygraphLayout.prototype.unstackPointAtIndex = function(idx) {
 /**
  * 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;
 
@@ -306,6 +309,7 @@ DygraphCanvasRenderer = function(dygraph, element, layout, options) {
 
   this.layout = layout;
   this.element = element;
+  this.elementContext = elementContext;
   this.container = this.element.parentNode;
 
   this.height = this.element.height;
@@ -342,18 +346,17 @@ DygraphCanvasRenderer = function(dygraph, element, layout, options) {
 
   // Add space for chart labels: title, xlabel and ylabel.
   if (this.attr_('title')) {
-    // TODO(danvk): make this a parameter
     this.area.h -= this.attr_('titleHeight');
     this.area.y += this.attr_('titleHeight');
   }
   if (this.attr_('xlabel')) {
-    // TODO(danvk): make this a parameter
     this.area.h -= this.attr_('xLabelHeight');
   }
   if (this.attr_('ylabel')) {
-    var yLabelWidth = 16;
-    this.area.x += this.attr_('yLabelWidth');
-    this.area.w -= this.attr_('yLabelWidth');
+    // It would make sense to shift the chart here to make room for the y-axis
+    // label, but the default yAxisLabelWidth is large enough that this results
+    // in overly-padded charts. The y-axis label should fit fine. If it
+    // doesn't, the yAxisLabelWidth option can be increased.
   }
 
   this.container.style.position = "relative";
@@ -361,12 +364,12 @@ 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.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();
@@ -384,7 +387,7 @@ DygraphCanvasRenderer.prototype.clear = function() {
         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.
@@ -394,7 +397,7 @@ DygraphCanvasRenderer.prototype.clear = function() {
     }
   }
 
-  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++) {
@@ -446,7 +449,7 @@ DygraphCanvasRenderer.isSupported = function(canvasName) {
 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};
 
@@ -506,7 +509,7 @@ DygraphCanvasRenderer.prototype._renderAxis = function() {
   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",
@@ -659,10 +662,12 @@ DygraphCanvasRenderer.prototype._renderChartLabels = function() {
     div.style.width = this.area.w + 'px';
     div.style.height = this.attr_('titleHeight') + 'px';
     div.style.textAlign = 'center';
-    div.style.fontSize = (this.attr_('titleHeight') - 2) + 'px';
+    div.style.fontSize = (this.attr_('titleHeight') - 8) + 'px';
     div.style.fontWeight = 'bold';
-    // div.style.border = '1px solid black';
-    div.innerHTML = this.attr_('title');
+    var class_div = document.createElement("div");
+    class_div.className = 'dygraph-label dygraph-title';
+    class_div.innerHTML = this.attr_('title');
+    div.appendChild(class_div);
     this.container.appendChild(div);
     this.chartLabels.title = div;
   }
@@ -676,8 +681,11 @@ DygraphCanvasRenderer.prototype._renderChartLabels = function() {
     div.style.height = this.attr_('xLabelHeight') + 'px';
     div.style.textAlign = 'center';
     div.style.fontSize = (this.attr_('xLabelHeight') - 2) + 'px';
-    // div.style.border = '1px solid black';
-    div.innerHTML = this.attr_('xlabel');
+
+    var class_div = document.createElement("div");
+    class_div.className = 'dygraph-label dygraph-xlabel';
+    class_div.innerHTML = this.attr_('xlabel');
+    div.appendChild(class_div);
     this.container.appendChild(div);
     this.chartLabels.xlabel = div;
   }
@@ -689,6 +697,7 @@ DygraphCanvasRenderer.prototype._renderChartLabels = function() {
       width: this.attr_('yLabelWidth'),
       height: this.area.h
     };
+    // TODO(danvk): is this outer div actually necessary?
     var div = document.createElement("div");
     div.style.position = 'absolute';
     div.style.left = box.left;
@@ -696,24 +705,39 @@ DygraphCanvasRenderer.prototype._renderChartLabels = function() {
     div.style.width = box.width + 'px';
     div.style.height = box.height + 'px';
     div.style.fontSize = (this.attr_('yLabelWidth') - 2) + 'px';
-    // div.style.border = '1px solid black';
 
     var inner_div = document.createElement("div");
     inner_div.style.position = 'absolute';
-    // inner_div.style.border = '1px solid red';
     inner_div.style.width = box.height + 'px';
     inner_div.style.height = box.width + 'px';
     inner_div.style.top = (box.height / 2 - box.width / 2) + 'px';
     inner_div.style.left = (box.width / 2 - box.height / 2) + 'px';
     inner_div.style.textAlign = 'center';
+
+    // CSS rotation is an HTML5 feature which is not standardized. Hence every
+    // browser has its own name for the CSS style.
     inner_div.style.transform = 'rotate(-90deg)';        // HTML5
     inner_div.style.WebkitTransform = 'rotate(-90deg)';  // Safari/Chrome
     inner_div.style.MozTransform = 'rotate(-90deg)';     // Firefox
     inner_div.style.OTransform = 'rotate(-90deg)';       // Opera
-    inner_div.style.filter =
-     'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';
-    inner_div.innerHTML = this.attr_('ylabel');
+    inner_div.style.msTransform = 'rotate(-90deg)';      // IE9
+
+    if (typeof(document.documentMode) !== 'undefined' &&
+        document.documentMode < 9) {
+      // We're dealing w/ an old version of IE, so we have to rotate the text
+      // using a BasicImage transform. This uses a different origin of rotation
+      // than HTML5 rotation (top left of div vs. its center).
+      inner_div.style.filter =
+       'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';
+      inner_div.style.left = '0px';
+      inner_div.style.top = '0px';
+    }
+
+    var class_div = document.createElement("div");
+    class_div.className = 'dygraph-label dygraph-ylabel';
+    class_div.innerHTML = this.attr_('ylabel');
 
+    inner_div.appendChild(class_div);
     div.appendChild(inner_div);
     this.container.appendChild(div);
     this.chartLabels.ylabel = div;
@@ -803,7 +827,7 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
     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) {
@@ -824,7 +848,7 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
  */
 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;