Add avoidMinZero option to disable the Y axis at zero heuristic.
authorNeal Nelson <neal@makalumedia.com>
Fri, 18 Jun 2010 09:58:24 +0000 (11:58 +0200)
committerNeal Nelson <neal@makalumedia.com>
Fri, 18 Jun 2010 09:58:24 +0000 (11:58 +0200)
docs/index.html
dygraph.js
tests/avoidMinZero.html [new file with mode: 0644]

index e95d540..aba5380 100644 (file)
@@ -852,6 +852,17 @@ perl -ne 'BEGIN{print "Month,Nominal,Real\n"} chomp; ($m,$cpi,$low,$close,$high)
           </td>
         </tr>
 
+        <tr>
+          <td><strong>avoidMinZero</strong></td>
+          <td><code>boolean</code></td>
+          <td><code>false</code></td>
+          <td>
+            When set, the heuristic that fixes the Y axis at zero for a data set with the minimum Y value of zero is disabled.
+            This is particularly useful for data sets that contain many zero values, especially for step plots which may otherwise have lines not visible running along the bottom axis.
+            <div class="tests">Tests: <a href="tests/avoidMinZero.html">avoidMinZero</a></div>
+          </td>
+        </tr>
+
       </tbody>
     </table>
 
index ee08802..896a0f9 100644 (file)
@@ -125,7 +125,8 @@ Dygraph.DEFAULT_ATTRS = {
   stackedGraph: false,
   hideOverlayOnMouseOut: true,
 
-  stepPlot: false
+  stepPlot: false,
+  avoidMinZero: false
 };
 
 // Various logging levels.
@@ -1639,8 +1640,10 @@ Dygraph.prototype.drawGraph_ = function(data) {
     var minAxisY = minY - 0.1 * span;
 
     // Try to include zero and make it minAxisY (or maxAxisY) if it makes sense.
-    if (minAxisY < 0 && minY >= 0) minAxisY = 0;
-    if (maxAxisY > 0 && maxY <= 0) maxAxisY = 0;
+    if (!this.attr_("avoidMinZero")) {
+      if (minAxisY < 0 && minY >= 0) minAxisY = 0;
+      if (maxAxisY > 0 && maxY <= 0) maxAxisY = 0;
+    }
 
     if (this.attr_("includeZero")) {
       if (maxY < 0) maxAxisY = 0;
diff --git a/tests/avoidMinZero.html b/tests/avoidMinZero.html
new file mode 100644 (file)
index 0000000..be33df5
--- /dev/null
@@ -0,0 +1,80 @@
+<html>
+  <head>
+    <title>dygraph</title>
+    <!--[if IE]>
+    <script type="text/javascript" src="excanvas.js"></script>
+    <![endif]-->
+    <script type="text/javascript" src="../strftime/strftime-min.js"></script>
+    <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
+    <script type="text/javascript" src="../dygraph-canvas.js"></script>
+    <script type="text/javascript" src="../dygraph.js"></script>
+  </head>
+  <body>
+    <p>1: Line chart with axis at zero problem:</p>
+    <div id="graph1"></div>
+    <script type="text/javascript">
+        new Dygraph(document.getElementById("graph1"),
+            "Date,Temperature\n" +
+            "2008-05-07,0\n" +
+            "2008-05-08,1\n" +
+            "2008-05-09,0\n" +
+            "2008-05-10,0\n" +
+            "2008-05-11,3\n" +
+            "2008-05-12,4\n"
+        )
+    </script>
+
+    <p>2: Step chart with axis at zero problem:</p>
+    <div id="graphd2"></div>
+    <script type="text/javascript">
+        new Dygraph(document.getElementById("graphd2"),
+            "Date,Temperature\n" +
+            "2008-05-07,0\n" +
+            "2008-05-08,1\n" +
+            "2008-05-09,0\n" +
+            "2008-05-10,0\n" +
+            "2008-05-11,3\n" +
+            "2008-05-12,4\n",
+            {
+               stepPlot: true
+            }
+        )
+    </script>
+
+    <p>3: Line chart with <code>avoidMinZero</code> option:</p>
+    <div id="graph3"></div>
+    <script type="text/javascript">
+        new Dygraph(document.getElementById("graph3"),
+            "Date,Temperature\n" +
+            "2008-05-07,0\n" +
+            "2008-05-08,1\n" +
+            "2008-05-09,0\n" +
+            "2008-05-10,0\n" +
+            "2008-05-11,3\n" +
+            "2008-05-12,4\n",
+            {
+                avoidMinZero: true
+            }
+        )
+    </script>
+
+    <p>4: Step chart with <code>avoidMinZero</code> option:</p>
+    <div id="graphd4"></div>
+    <script type="text/javascript">
+        new Dygraph(document.getElementById("graphd4"),
+            "Date,Temperature\n" +
+            "2008-05-07,0\n" +
+            "2008-05-08,1\n" +
+            "2008-05-09,0\n" +
+            "2008-05-10,0\n" +
+            "2008-05-11,3\n" +
+            "2008-05-12,4\n",
+            {
+               stepPlot: true,
+               avoidMinZero: true
+            }
+        )
+    </script>
+
+  </body>
+</html>