Add comment to resize test case as a follow on to pull request 260.
[dygraphs.git] / dygraph.js
index ffffa01..db9a8bf 100644 (file)
@@ -466,9 +466,11 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
       div.style.width = Dygraph.DEFAULT_WIDTH + "px";
     }
   }
-  // these will be zero if the dygraph's div is hidden.
-  this.width_ = div.clientWidth;
-  this.height_ = div.clientHeight;
+  // These will be zero if the dygraph's div is hidden. In that case,
+  // use the user-specified attributes if present. If not, use zero
+  // and assume the user will call resize to fix things later.
+  this.width_ = div.clientWidth || attrs.width || 0;
+  this.height_ = div.clientHeight || attrs.height || 0;
 
   // TODO(danvk): set fillGraph to be part of attrs_ here, not user_attrs_.
   if (attrs.stackedGraph) {
@@ -995,12 +997,12 @@ Dygraph.prototype.createInterface_ = function() {
   this.canvas_ = Dygraph.createCanvas();
   this.canvas_.style.position = "absolute";
 
+  // ... and for static parts of the chart.
+  this.hidden_ = this.createPlotKitCanvas_(this.canvas_);
+
   this.resizeElements_();
 
   this.canvas_ctx_ = Dygraph.getContext(this.canvas_);
-
-  // ... and for static parts of the chart.
-  this.hidden_ = this.createPlotKitCanvas_(this.canvas_);
   this.hidden_ctx_ = Dygraph.getContext(this.hidden_);
 
   // The interactive parts of the graph are drawn on top of the chart.
@@ -1052,6 +1054,10 @@ Dygraph.prototype.resizeElements_ = function() {
   this.canvas_.height = this.height_;
   this.canvas_.style.width = this.width_ + "px";    // for IE
   this.canvas_.style.height = this.height_ + "px";  // for IE
+  this.hidden_.width = this.width_;
+  this.hidden_.height = this.height_;
+  this.hidden_.style.width = this.width_ + "px";    // for IE
+  this.hidden_.style.height = this.height_ + "px";  // for IE
 };
 
 /**
@@ -2281,7 +2287,7 @@ Dygraph.prototype.predraw_ = function() {
  *     yval_stacked
  * }}
  */
-Dygraph.PointType;
+Dygraph.PointType = undefined;
 
 // TODO(bhs): these loops are a hot-spot for high-point-count charts. In fact,
 // on chrome+linux, they are 6 times more expensive than iterating through the
@@ -2314,8 +2320,8 @@ Dygraph.seriesToPoints_ = function(series, bars, setName, boundaryIdStart) {
     };
 
     if (bars) {
-      point.y_top = NaN,
-      point.y_bottom = NaN,
+      point.y_top = NaN;
+      point.y_bottom = NaN;
       point.yval_minus = DygraphLayout.parseFloat_(item[1][1]);
       point.yval_plus = DygraphLayout.parseFloat_(item[1][2]);
     }
@@ -2350,7 +2356,7 @@ Dygraph.stackPoints_ = function(
   var nextPointIdx = -1;
 
   // Find the next stackable point starting from the given index.
-  function updateNextPoint(idx) {
+  var updateNextPoint = function(idx) {
     // If we've previously found a non-NaN point and haven't gone past it yet,
     // just use that.
     if (nextPointIdx >= idx) return;
@@ -2439,7 +2445,7 @@ Dygraph.prototype.gatherDatasets_ = function(rolledSeries, dateWindow) {
   var points = [];
   var cumulativeYval = [];  // For stacked series.
   var extremes = {};  // series name -> [low, high]
-  var i, j, k;
+  var i, k;
   var errorBars = this.attr_("errorBars");
   var customBars = this.attr_("customBars");
   var bars = errorBars || customBars;
@@ -2455,6 +2461,7 @@ Dygraph.prototype.gatherDatasets_ = function(rolledSeries, dateWindow) {
   // Loop over the fields (series).  Go from the last to the first,
   // because if they're stacked that's how we accumulate the values.
   var num_series = rolledSeries.length - 1;
+  var series;
   for (i = num_series; i >= 1; i--) {
     if (!this.visibility()[i - 1]) continue;
 
@@ -2462,7 +2469,7 @@ Dygraph.prototype.gatherDatasets_ = function(rolledSeries, dateWindow) {
     // Because there can be lines going to points outside of the visible area,
     // we actually prune to visible points, plus one on either side.
     if (dateWindow) {
-      var series = rolledSeries[i];
+      series = rolledSeries[i];
       var low = dateWindow[0];
       var high = dateWindow[1];
 
@@ -3700,9 +3707,10 @@ Dygraph.prototype.resize = function(width, height) {
     this.height_ = this.maindiv_.clientHeight;
   }
 
-  this.resizeElements_();
-
   if (old_width != this.width_ || old_height != this.height_) {
+    // Resizing a canvas erases it, even when the size doesn't change, so
+    // any resize needs to be followed by a redraw.
+    this.resizeElements_();
     this.predraw_();
   }