X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-canvas.js;h=123fa83a74989336478a14d7b9b47e47731ff917;hb=1e1bf7dfc2628a153f6b6b8c9b2b6b6d74390e6c;hp=3932e7125533c2fd80735621aa4d5b784912850b;hpb=9a40897e37ef50be220f252092ad13fa7cbfa8b9;p=dygraphs.git diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 3932e71..123fa83 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('iconWidth') && + ann[i].hasOwnProperty('iconHeight'))) { + this.dygraph_.error("Must set iconWidth and iconHeight 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); } }; @@ -495,7 +501,6 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() { "position": "absolute", "fontSize": this.options.axisLabelFontSize + "px", "zIndex": 10, - "width": "20px", "overflow": "hidden", }; @@ -514,9 +519,14 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() { var points = this.layout.annotated_points; for (var i = 0; i < points.length; i++) { var p = points[i]; - var tick_height = 5; - if (p.annotation.hasOwnProperty("tickHeight")) { - tick_height = p.annotation.tickHeight; + 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"); @@ -525,16 +535,32 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() { 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 = a.iconWidth; + img.height = height = a.iconHeight; + div.appendChild(img); + } else if (p.annotation.hasOwnProperty('shortText')) { + div.appendChild(document.createTextNode(p.annotation.shortText)); } - div.appendChild(document.createTextNode(p.annotation.shortText)); - div.style.left = (p.canvasx - 10) + "px"; - div.style.top = (p.canvasy - 20 - tick_height) + "px"; + div.style.left = (p.canvasx - width / 2) + "px"; + div.style.top = (p.canvasy - height - tick_height) + "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; Dygraph.addEvent(div, 'click', bindEvt('clickHandler', 'annotationClickHandler', p, this));