X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-canvas.js;h=182f29d3e667b6ee8bbf98fbe90451eb567f30bc;hb=447254de1d05f324f802b80c8398372e5418f3bc;hp=66888ee81686b1bb5437e9a47541ddfdac539af5;hpb=ab5e5c7548f975820f6b469886d15ff5bc1b8477;p=dygraphs.git diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 66888ee..182f29d 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -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) { @@ -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); } }; @@ -97,13 +103,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); } } @@ -495,8 +494,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 +512,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(); } };