Add legend: "never" option
authorDan Vanderkam <danvdk@gmail.com>
Sun, 23 Nov 2014 21:12:31 +0000 (16:12 -0500)
committerDan Vanderkam <danvdk@gmail.com>
Sun, 23 Nov 2014 21:12:31 +0000 (16:12 -0500)
dygraph.js
plugins/legend.js
tests/customLabelFollow.html

index b1af8de..efc9092 100644 (file)
@@ -312,8 +312,7 @@ Dygraph.DEFAULT_ATTRS = {
   stackedGraphNaNFill: 'all',
   hideOverlayOnMouseOut: true,
 
-  // TODO(danvk): support 'onmouseover' and 'never', and remove synonyms.
-  legend: 'onmouseover',  // the only relevant value at the moment is 'always'.
+  legend: 'onmouseover',
   stepPlot: false,
   avoidMinZero: false,
   xRangePad: 0,
index bb86949..65ee671 100644 (file)
@@ -125,7 +125,13 @@ legend.prototype.select = function(e) {
   var xValue = e.selectedX;
   var points = e.selectedPoints;
 
-  if (e.dygraph.getOption('legend') === 'follow') {
+  var legendMode = e.dygraph.getOption('legend');
+  if (legendMode === 'never') {
+    this.legend_div_.style.display = 'none';
+    return;
+  }
+
+  if (legendMode === 'follow') {
     // create floating legend div
     var area = e.dygraph.plotter_.area;
     var labelsDivWidth = e.dygraph.getOption('labelsDivWidth');
@@ -146,16 +152,16 @@ legend.prototype.select = function(e) {
     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 = '';
   }
 
   var html = legend.generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_);
   this.legend_div_.innerHTML = html;
+  this.legend_div_.style.display = '';
 };
 
 legend.prototype.deselect = function(e) {
-
-  if (e.dygraph.getOption("legend") === "follow") {
+  var legendMode = e.dygraph.getOption('legend');
+  if (legendMode !== 'always') {
     this.legend_div_.style.display = "none";
   }
 
index 76d3822..b2362a4 100644 (file)
         box-shadow: 4px 4px 4px #888;
         pointer-events: none;
       }
+      pre {
+        margin-top: 30px;
+      }
     </style>
   </head>
   <body>
-    <p>Legend follows highlighted points:</p>
-    <div id="div_g14" style="width:600px; height:300px;"></div>
+    <p>This page demonstrates different values for the <code><a href="http://dygraphs.com/options.html#legend">legend</a></code> option. Mouse over the charts to see the different behaviors.</p>
+    <pre>legend: "follow":</pre>
+    <div id="follow" style="width:600px; height:300px;"></div>
+
+    <pre>legend: "always":</pre>
+    <div id="always" style="width:600px; height:300px;"></div>
+
+    <pre>legend: "never":</pre>
+    <div id="never" style="width:600px; height:300px;"></div>
+
+    <pre>legend: "onmouseover" (the default):</pre>
+    <div id="default" style="width:600px; height:300px;"></div>
 
     <script type="text/javascript">
-      g14 = new Dygraph(
-            document.getElementById("div_g14"),
-            NoisyData, {
-              rollPeriod: 14,
-              errorBars: true,
-              labelsDivWidth: 100,
-              labelsSeparateLines: true,
-              legend: "follow"
-            }
-          );
+      function extend(obj1, obj2) {
+        var obj = {};
+        for (var k in obj1) {
+          obj[k] = obj1[k];
+        }
+        for (var k in obj2) {
+          obj[k] = obj2[k];
+        }
+        return obj;
+      }
+
+      var baseOpts = {
+        rollPeriod: 14,
+        errorBars: true,
+        labelsDivWidth: 100,
+        labelsSeparateLines: true
+      };
+
+      g_follow = new Dygraph(
+            document.getElementById('follow'),
+            NoisyData,
+            extend(baseOpts, {legend: 'follow'}));
+
+      g_always = new Dygraph(
+            document.getElementById('always'),
+            NoisyData,
+            extend(baseOpts, {legend: 'always'}));
+
+      g_never = new Dygraph(
+            document.getElementById('never'),
+            NoisyData,
+            extend(baseOpts, {legend: 'never'}));
+
+      g_default = new Dygraph(
+            document.getElementById('default'),
+            NoisyData,
+            baseOpts);
+
     </script>
   </body>
 </html>