allow dateWindow to be set explicitly in layout (previously it was deduced from the...
authorDan Vanderkam <danvdk@gmail.com>
Mon, 14 Dec 2009 13:35:51 +0000 (05:35 -0800)
committerDan Vanderkam <danvdk@gmail.com>
Mon, 14 Dec 2009 13:35:51 +0000 (05:35 -0800)
dygraph-canvas.js
dygraph.js
tests/dateWindow.html [new file with mode: 0644]

index a848001..c3a8296 100644 (file)
@@ -37,14 +37,19 @@ DygraphLayout.prototype.evaluate = function() {
 
 DygraphLayout.prototype._evaluateLimits = function() {
   this.minxval = this.maxxval = null;
-  for (var name in this.datasets) {
-    if (!this.datasets.hasOwnProperty(name)) continue;
-    var series = this.datasets[name];
-    var x1 = series[0][0];
-    if (!this.minxval || x1 < this.minxval) this.minxval = x1;
-
-    var x2 = series[series.length - 1][0];
-    if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2;
+  if (this.options.dateWindow) {
+    this.minxval = this.options.dateWindow[0];
+    this.maxxval = this.options.dateWindow[1];
+  } else {
+    for (var name in this.datasets) {
+      if (!this.datasets.hasOwnProperty(name)) continue;
+      var series = this.datasets[name];
+      var x1 = series[0][0];
+      if (!this.minxval || x1 < this.minxval) this.minxval = x1;
+
+      var x2 = series[series.length - 1][0];
+      if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2;
+    }
   }
   this.xrange = this.maxxval - this.minxval;
   this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0);
index 8895a39..386781d 100644 (file)
@@ -1248,11 +1248,14 @@ Dygraph.prototype.drawGraph_ = function(data) {
   this.addXTicks_();
 
   // Tell PlotKit to use this new data and render itself
+  if (this.dateWindow_) {
+    this.layout_.updateOptions({dateWindow: this.dateWindow_});
+  }
   this.layout_.evaluateWithError();
   this.plotter_.clear();
   this.plotter_.render();
-  this.canvas_.getContext('2d').clearRect(0, 0,
-                                         this.canvas_.width, this.canvas_.height);
+  this.canvas_.getContext('2d').clearRect(0, 0, this.canvas_.width,
+                                         this.canvas_.height);
 };
 
 /**
diff --git a/tests/dateWindow.html b/tests/dateWindow.html
new file mode 100644 (file)
index 0000000..14ad893
--- /dev/null
@@ -0,0 +1,31 @@
+<html>
+  <head>
+    <title>dateWindow</title>
+    <!--[if IE]>
+    <script type="text/javascript" src="excanvas.js"></script>
+    <![endif]-->
+    <script type="text/javascript" src="../dygraph-combined.js"></script>
+    <script type="text/javascript" src="../dygraph-canvas.js"></script>
+    <script type="text/javascript" src="../dygraph.js"></script>
+  </head>
+  <body>
+    <p>dateWindow is set to something other than an x-value in the data set.
+    Grid lines should still go through data points.</p>
+
+    <div id="div_g" style="width:600px; height:300px;"></div>
+
+    <script type="text/javascript">
+      var data = [];
+      for (var i = 0; i < 10; i++) {
+        data.push([i, i%2, i%3]);
+      }
+
+      g = new Dygraph(
+            document.getElementById("div_g"),
+            data, {
+              dateWindow: [ -15, 15 ]
+            }
+          );
+    </script>
+  </body>
+</html>