/**
* 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;
// 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";
// 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",
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;
}
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;
}
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;
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;
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;