Use "legend" option with "follow" value. Hide legend on deselect.
[dygraphs.git] / plugins / legend.js
index 90803f6..9954682 100644 (file)
@@ -128,7 +128,7 @@ legend.prototype.select = function(e) {
   var xValue = e.selectedX;
   var points = e.selectedPoints;
 
-  if(e.dygraph.getOption("legendFollow")) {
+  if (e.dygraph.getOption("legend") === "follow") {
     // create floating legend div
     var area = e.dygraph.plotter_.area;
     var labelsDivWidth = e.dygraph.getOption("labelsDivWidth");
@@ -142,13 +142,14 @@ legend.prototype.select = function(e) {
 
     // if legend floats to end of the plotting area, it flips to the other
     // side of the selection point
-    if((leftLegend + labelsDivWidth + 1) > area.w) {
-      leftLegend = leftLegend - 2*20 - labelsDivWidth - (yAxisLabelWidth - area.x);
+    if ((leftLegend + labelsDivWidth + 1) > (window.scrollX + window.innerWidth)) {
+      leftLegend = leftLegend - 2 * 20 - labelsDivWidth - (yAxisLabelWidth - area.x);
     }
 
     e.dygraph.graphDiv.appendChild(this.legend_div_);
     this.legend_div_.style.left = yAxisLabelWidth + leftLegend + "px";
     this.legend_div_.style.top = topLegend + "px";
+    this.legend_div_.style.display = "block";
   }
 
   var html = generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_);
@@ -157,9 +158,8 @@ legend.prototype.select = function(e) {
 
 legend.prototype.deselect = function(e) {
 
-  if(e.dygraph.getOption("legendFollow")) {
-    // return legend to the default non-floating position
-    this.predraw(e);
+  if (e.dygraph.getOption("legend") === "follow") {
+    this.legend_div_.style.display = "none";
   }
 
   // Have to do this every time, since styles might have changed.