click to add annotations
authorDan Vanderkam <danvdk@gmail.com>
Tue, 14 Sep 2010 01:12:45 +0000 (18:12 -0700)
committerDan Vanderkam <danvdk@gmail.com>
Tue, 14 Sep 2010 01:12:45 +0000 (18:12 -0700)
dygraph.js
tests/annotation.html

index 8c9f2df..4d2d3d0 100644 (file)
@@ -804,8 +804,8 @@ Dygraph.prototype.createDragInterface_ = function() {
       var regionHeight = Math.abs(dragEndY - dragStartY);
 
       if (regionWidth < 2 && regionHeight < 2 && self.lastx_ != undefined) {
+        // TODO(danvk): pass along more info about the points, e.g. 'x'
         if (self.attr_('clickCallback') != null) {
-          // TODO(danvk): pass along more info about the points.
           self.attr_('clickCallback')(event, self.lastx_, self.selPoints_);
         }
         if (self.attr_('pointClickCallback')) {
@@ -823,7 +823,7 @@ Dygraph.prototype.createDragInterface_ = function() {
           }
 
           // Allow any click within two pixels of the dot.
-          var radius = this.attr_('highlightCircleSize') + 2;
+          var radius = self.attr_('highlightCircleSize') + 2;
           if (closestDistance <= 5 * 5) {
             self.attr_('pointClickCallback')(event, self.selPoints_[closestIdx]);
           }
index b87c291..60062bc 100644 (file)
       }
 
       var saveBg = '';
+      var num = 0;
       g.updateOptions( {
         annotationClickHandler: function(ann, point, dg, event) {
           eventDiv.innerHTML += "click: " + nameAnnotation(ann) + "<br/>";
         annotationMouseOutHandler: function(ann, point, dg, event) {
           document.getElementById(nameAnnotation(ann)).style.fontWeight = 'normal';
           ann.div.style.backgroundColor = saveBg;
+        },
+
+        pointClickCallback: function(event, p) {
+          // Check if the point is already annotated.
+          if (p.annotation) return;
+
+          // If not, add one.
+          var ann = {
+            series: p.name,
+            xval: p.xval,
+            shortText: num,
+            text: "Annotation #" + num
+          };
+          var anns = g.annotations();
+          anns.push(ann);
+          g.setAnnotations(anns);
+
+          num++;
         }
       });
     </script>