X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-canvas.js;h=f22f8586cecd242149baa56600d8733d03418d1b;hb=67076b22e2685706c23f56bed8ee41a584c20b5b;hp=66888ee81686b1bb5437e9a47541ddfdac539af5;hpb=ab5e5c7548f975820f6b469886d15ff5bc1b8477;p=dygraphs.git diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 66888ee..f22f858 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -30,19 +30,25 @@ DygraphLayout.prototype.addDataset = function(setname, set_xy) { this.datasets[setname] = set_xy; }; -// TODO(danvk): CONTRACT remove DygraphLayout.prototype.setAnnotations = function(ann) { // The Dygraph object's annotations aren't parsed. We parse them here and // save a copy. var parse = this.attr_('xValueParser'); for (var i = 0; i < ann.length; i++) { var a = {}; - if (!ann[i].x) { + if (!ann[i].xval && !ann[i].x) { this.dygraph_.error("Annotations must have an 'x' property"); return; } + if (ann[i].icon && + !(ann[i].hasOwnProperty('width') && + ann[i].hasOwnProperty('height'))) { + this.dygraph_.error("Must set width and height when setting " + + "annotation.icon property"); + return; + } Dygraph.update(a, ann[i]); - a.xval = parse(a.x); + if (!a.xval) a.xval = parse(a.x); this.annotations.push(a); } }; @@ -73,10 +79,13 @@ DygraphLayout.prototype._evaluateLimits = function() { this.xrange = this.maxxval - this.minxval; this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0); - this.minyval = this.options.yAxis[0]; - this.maxyval = this.options.yAxis[1]; - this.yrange = this.maxyval - this.minyval; - this.yscale = (this.yrange != 0 ? 1/this.yrange : 1.0); + for (var i = 0; i < this.options.yAxes.length; i++) { + var axis = this.options.yAxes[i]; + axis.minyval = axis.valueRange[0]; + axis.maxyval = axis.valueRange[1]; + axis.yrange = axis.maxyval - axis.minyval; + axis.yscale = (axis.yrange != 0 ? 1.0 / axis.yrange : 1.0); + } }; DygraphLayout.prototype._evaluateLineCharts = function() { @@ -86,12 +95,14 @@ DygraphLayout.prototype._evaluateLineCharts = function() { if (!this.datasets.hasOwnProperty(setName)) continue; var dataset = this.datasets[setName]; + var axis = this.options.yAxes[this.options.seriesToAxisMap[setName]]; + for (var j = 0; j < dataset.length; j++) { var item = dataset[j]; var point = { // TODO(danvk): here x: ((parseFloat(item[0]) - this.minxval) * this.xscale), - y: 1.0 - ((parseFloat(item[1]) - this.minyval) * this.yscale), + y: 1.0 - ((parseFloat(item[1]) - axis.minyval) * axis.yscale), xval: parseFloat(item[0]), yval: parseFloat(item[1]), name: setName @@ -121,12 +132,15 @@ DygraphLayout.prototype._evaluateLineTicks = function() { } this.yticks = new Array(); - for (var i = 0; i < this.options.yTicks.length; i++) { - var tick = this.options.yTicks[i]; - var label = tick.label; - var pos = 1.0 - (this.yscale * (tick.v - this.minyval)); - if ((pos >= 0.0) && (pos <= 1.0)) { - this.yticks.push([pos, label]); + for (var i = 0; i < 1; i++ ) { + var axis = this.options.yAxes[i]; + for (var j = 0; j < axis.ticks.length; j++) { + var tick = axis.ticks[j]; + var label = tick.label; + var pos = 1.0 - (axis.yscale * (tick.v - axis.minyval)); + if ((pos >= 0.0) && (pos <= 1.0)) { + this.yticks.push([pos, label]); + } } } }; @@ -246,6 +260,7 @@ DygraphCanvasRenderer = function(dygraph, element, layout, options) { this.ylabels = new Array(); this.annotations = new Array(); + // TODO(danvk): consider all axes in this computation. this.area = { x: this.options.yAxisLabelWidth + 2 * this.options.axisTickSize, y: 0 @@ -495,8 +510,7 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() { "position": "absolute", "fontSize": this.options.axisLabelFontSize + "px", "zIndex": 10, - "width": "20px", - "overflow": "hidden", + "overflow": "hidden" }; var bindEvt = function(eventName, classEventName, p, self) { @@ -514,34 +528,77 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() { var points = this.layout.annotated_points; for (var i = 0; i < points.length; i++) { var p = points[i]; + if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) { + continue; + } + + var a = p.annotation; + var tick_height = 6; + if (a.hasOwnProperty("tickHeight")) { + tick_height = a.tickHeight; + } + var div = document.createElement("div"); for (var name in annotationStyle) { if (annotationStyle.hasOwnProperty(name)) { div.style[name] = annotationStyle[name]; } } - div.className = "dygraphDefaultAnnotation"; - if (p.annotation.hasOwnProperty('cssClass')) { - div.className += " " + p.annotation.cssClass; + if (!a.hasOwnProperty('icon')) { + div.className = "dygraphDefaultAnnotation"; + } + if (a.hasOwnProperty('cssClass')) { + div.className += " " + a.cssClass; + } + + var width = a.hasOwnProperty('width') ? a.width : 16; + var height = a.hasOwnProperty('height') ? a.height : 16; + if (a.hasOwnProperty('icon')) { + var img = document.createElement("img"); + img.src = a.icon; + img.width = width; + img.height = height; + div.appendChild(img); + } else if (p.annotation.hasOwnProperty('shortText')) { + div.appendChild(document.createTextNode(p.annotation.shortText)); + } + div.style.left = (p.canvasx - width / 2) + "px"; + if (a.attachAtBottom) { + div.style.top = (this.area.h - height - tick_height) + "px"; + } else { + div.style.top = (p.canvasy - height - tick_height) + "px"; } - div.appendChild(document.createTextNode(p.annotation.shortText)); - div.style.left = (p.canvasx - 10) + "px"; - div.style.top = p.canvasy + "px"; + div.style.width = width + "px"; + div.style.height = height + "px"; div.title = p.annotation.text; div.style.color = this.colors[p.name]; div.style.borderColor = this.colors[p.name]; + a.div = div; - var self = this; - Dygraph.addEvent(div, 'click', function(p, self) { return function(e) { - if (p.annotation.hasOwnProperty('clickHandler')) { - p.annotation.clickHandler(p.annotation, p, self.dygraph_, e); - } else if (self.dygraph_.attr_('annotationClickHandler')) { - self.dygraph_.attr_('annotationClickHandler')(p.annotation, p, self.dygraph_, e); - } }; }(p, self) - ); + Dygraph.addEvent(div, 'click', + bindEvt('clickHandler', 'annotationClickHandler', p, this)); + Dygraph.addEvent(div, 'mouseover', + bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this)); + Dygraph.addEvent(div, 'mouseout', + bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this)); + Dygraph.addEvent(div, 'dblclick', + bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this)); this.container.appendChild(div); this.annotations.push(div); + + var ctx = this.element.getContext("2d"); + ctx.strokeStyle = this.colors[p.name]; + ctx.beginPath(); + if (!a.attachAtBottom) { + ctx.moveTo(p.canvasx, p.canvasy); + ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height); + } else { + ctx.moveTo(p.canvasx, this.area.h); + ctx.lineTo(p.canvasx, this.area.h - 2 - tick_height); + } + ctx.closePath(); + ctx.stroke(); } }; @@ -591,6 +648,8 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() { for (var i = 0; i < setCount; i++) { var setName = setNames[i]; + var axis = this.layout.options.yAxes[ + this.layout.options.seriesToAxisMap[setName]]; var color = this.colors[setName]; // setup graphics context @@ -598,7 +657,7 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() { var prevX = NaN; var prevY = NaN; var prevYs = [-1, -1]; - var yscale = this.layout.yscale; + var yscale = axis.yscale; // should be same color as the lines but only 15% opaque. var rgb = new RGBColor(color); var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + @@ -646,23 +705,24 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() { ctx.fill(); } } else if (fillGraph) { - var axisY = 1.0 + this.layout.minyval * this.layout.yscale; - if (axisY < 0.0) axisY = 0.0; - else if (axisY > 1.0) axisY = 1.0; - axisY = this.area.h * axisY + this.area.y; - var baseline = [] // for stacked graphs: baseline for filling // process sets in reverse order (needed for stacked graphs) for (var i = setCount - 1; i >= 0; i--) { var setName = setNames[i]; var color = this.colors[setName]; + var axis = this.layout.options.yAxes[ + this.layout.options.seriesToAxisMap[setName]]; + var axisY = 1.0 + axis.minyval * axis.yscale; + if (axisY < 0.0) axisY = 0.0; + else if (axisY > 1.0) axisY = 1.0; + axisY = this.area.h * axisY + this.area.y; // setup graphics context ctx.save(); var prevX = NaN; var prevYs = [-1, -1]; - var yscale = this.layout.yscale; + var yscale = axis.yscale; // should be same color as the lines but only 15% opaque. var rgb = new RGBColor(color); var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + @@ -707,13 +767,14 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() { for (var i = 0; i < setCount; i++) { var setName = setNames[i]; var color = this.colors[setName]; + var strokeWidth = this.dygraph_.attr_("strokeWidth", setName); // setup graphics context context.save(); var point = this.layout.points[0]; - var pointSize = this.dygraph_.attr_("pointSize"); + var pointSize = this.dygraph_.attr_("pointSize", setName); var prevX = null, prevY = null; - var drawPoints = this.dygraph_.attr_("drawPoints"); + var drawPoints = this.dygraph_.attr_("drawPoints", setName); var points = this.layout.points; for (var j = 0; j < points.length; j++) { var point = points[j]; @@ -731,17 +792,20 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() { prevX = point.canvasx; prevY = point.canvasy; } else { - ctx.beginPath(); - ctx.strokeStyle = color; - ctx.lineWidth = this.options.strokeWidth; - ctx.moveTo(prevX, prevY); - if (stepPlot) { - ctx.lineTo(point.canvasx, prevY); + // TODO(danvk): figure out why this conditional is necessary. + if (strokeWidth) { + ctx.beginPath(); + ctx.strokeStyle = color; + ctx.lineWidth = strokeWidth; + ctx.moveTo(prevX, prevY); + if (stepPlot) { + ctx.lineTo(point.canvasx, prevY); + } + prevX = point.canvasx; + prevY = point.canvasy; + ctx.lineTo(prevX, prevY); + ctx.stroke(); } - prevX = point.canvasx; - prevY = point.canvasy; - ctx.lineTo(prevX, prevY); - ctx.stroke(); } if (drawPoints || isIsolated) {