remove dependence on PlotKit.Base as well
[dygraphs.git] / dygraph.js
index fd8a41d..df41fdc 100644 (file)
@@ -153,7 +153,6 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
   this.dateWindow_ = attrs.dateWindow || null;
   this.valueRange_ = attrs.valueRange || null;
   this.wilsonInterval_ = attrs.wilsonInterval || true;
-  this.customBars_ = attrs.customBars || false;
 
   // Clear the div. This ensure that, if multiple dygraphs are passed the same
   // div, then only one will be drawn.
@@ -193,12 +192,12 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
   // Create the PlotKit grapher
   // TODO(danvk): why does the Layout need its own set of options?
   this.layoutOptions_ = { 'errorBars': (this.attr_("errorBars") ||
-                                        this.customBars_),
+                                        this.attr_("customBars")),
                           'xOriginIsZero': false };
   MochiKit.Base.update(this.layoutOptions_, this.attrs_);
   MochiKit.Base.update(this.layoutOptions_, this.user_attrs_);
 
-  this.layout_ = new DygraphLayout(this.layoutOptions_);
+  this.layout_ = new DygraphLayout(this, this.layoutOptions_);
 
   // TODO(danvk): why does the Renderer need its own set of options?
   this.renderOptions_ = { colorScheme: this.colors_,
@@ -206,7 +205,8 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
                           axisLineWidth: Dygraph.AXIS_LINE_WIDTH };
   MochiKit.Base.update(this.renderOptions_, this.attrs_);
   MochiKit.Base.update(this.renderOptions_, this.user_attrs_);
-  this.plotter_ = new DygraphCanvasRenderer(this.hidden_, this.layout_,
+  this.plotter_ = new DygraphCanvasRenderer(this,
+                                            this.hidden_, this.layout_,
                                             this.renderOptions_);
 
   this.createStatusMessage_();
@@ -353,6 +353,34 @@ Dygraph.prototype.setColors_ = function() {
   MochiKit.Base.update(this.layoutOptions_, this.attrs_);
 }
 
+// The following functions are from quirksmode.org
+// http://www.quirksmode.org/js/findpos.html
+Dygraph.findPosX = function(obj) {
+  var curleft = 0;
+  if (obj.offsetParent) {
+    while (obj.offsetParent) {
+      curleft += obj.offsetLeft;
+      obj = obj.offsetParent;
+    }
+  }
+  else if (obj.x)
+    curleft += obj.x;
+  return curleft;
+};
+                   
+Dygraph.findPosY = function(obj) {
+  var curtop = 0;
+  if (obj.offsetParent) {
+    while (obj.offsetParent) {
+      curtop += obj.offsetTop;
+      obj = obj.offsetParent;
+    }
+  }
+  else if (obj.y)
+    curtop += obj.y;
+  return curtop;
+};
+
 /**
  * Create the div that contains information on the selected point(s)
  * This goes in the top right of the canvas, unless an external div has already
@@ -439,8 +467,8 @@ Dygraph.prototype.createDragInterface_ = function() {
   // Track the beginning of drag events
   connect(this.hidden_, 'onmousedown', function(event) {
     mouseDown = true;
-    px = PlotKit.Base.findPosX(self.canvas_);
-    py = PlotKit.Base.findPosY(self.canvas_);
+    px = Dygraph.findPosX(self.canvas_);
+    py = Dygraph.findPosY(self.canvas_);
     dragStartX = getX(event);
     dragStartY = getY(event);
   });
@@ -574,7 +602,7 @@ Dygraph.prototype.doZoom_ = function(lowX, highX) {
  * @private
  */
 Dygraph.prototype.mouseMove_ = function(event) {
-  var canvasx = event.mouse().page.x - PlotKit.Base.findPosX(this.hidden_);
+  var canvasx = event.mouse().page.x - Dygraph.findPosX(this.hidden_);
   var points = this.layout_.points;
 
   var lastx = -1;
@@ -969,7 +997,7 @@ Dygraph.prototype.addYTicks_ = function(minY, maxY) {
 Dygraph.prototype.extremeValues_ = function(series) {
   var minY = null, maxY = null;
 
-  var bars = this.attr_("errorBars") || this.customBars_;
+  var bars = this.attr_("errorBars") || this.attr_("customBars");
   if (bars) {
     // With custom bars, maxY is the max of the high values.
     for (var j = 0; j < series.length; j++) {
@@ -1014,6 +1042,7 @@ Dygraph.prototype.drawGraph_ = function(data) {
   var minY = null, maxY = null;
   this.layout_.removeAllDatasets();
   this.setColors_();
+  this.attrs_['pointSize'] = 0.5 * this.attr_('highlightCircleSize');
 
   // Loop over all fields in the dataset
   for (var i = 1; i < data[0].length; i++) {
@@ -1025,7 +1054,7 @@ Dygraph.prototype.drawGraph_ = function(data) {
     series = this.rollingAverage(series, this.rollPeriod_);
 
     // Prune down to the desired range, if necessary (for zooming)
-    var bars = this.attr_("errorBars") || this.customBars_;
+    var bars = this.attr_("errorBars") || this.attr_("customBars");
     if (this.dateWindow_) {
       var low = this.dateWindow_[0];
       var high= this.dateWindow_[1];
@@ -1142,7 +1171,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
         rollingData[i] = [date, mult * value];
       }
     }
-  } else if (this.customBars_) {
+  } else if (this.attr_("customBars")) {
     var low = 0;
     var mid = 0;
     var high = 0;
@@ -1176,49 +1205,28 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
         return originalData;
       }
 
-      for (var i = 0; i < num_init_points; i++) {
-        var sum = 0;
-        for (var j = 0; j < i + 1; j++)
-          sum += originalData[j][1];
-        rollingData[i] = [originalData[i][0], sum / (i + 1)];
-      }
-      // Calculate the rolling average for the remaining points
-      for (var i = Math.min(rollPeriod - 1, originalData.length - 2);
-          i < originalData.length;
-          i++) {
+      for (var i = 0; i < originalData.length; i++) {
         var sum = 0;
-        for (var j = i - rollPeriod + 1; j < i + 1; j++)
-          sum += originalData[j][1];
-        rollingData[i] = [originalData[i][0], sum / rollPeriod];
-      }
-    } else {
-      for (var i = 0; i < num_init_points; i++) {
-        var sum = 0;
-        var variance = 0;
         var num_ok = 0;
-        for (var j = 0; j < i + 1; j++) {
-          var y = originalData[j][1][0];
+        for (var j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
+          var y = originalData[j][1];
           if (!y || isNaN(y)) continue;
           num_ok++;
-          sum += y;
-          variance += Math.pow(originalData[j][1][1], 2);
+          sum += originalData[j][1];
         }
         if (num_ok) {
-          var stddev = Math.sqrt(variance)/num_ok;
-          rollingData[i] = [originalData[i][0],
-                            [sum/num_ok, sigma * stddev, sigma * stddev]];
+          rollingData[i] = [originalData[i][0], sum / num_ok];
         } else {
-          rollingData[i] = [originalData[i][0], [null, null, null]];
+          rollingData[i] = [originalData[i][0], null];
         }
       }
-      // Calculate the rolling average for the remaining points
-      for (var i = Math.min(rollPeriod - 1, originalData.length - 2);
-          i < originalData.length;
-          i++) {
+
+    } else {
+      for (var i = 0; i < originalData.length; i++) {
         var sum = 0;
         var variance = 0;
         var num_ok = 0;
-        for (var j = i - rollPeriod + 1; j < i + 1; j++) {
+        for (var j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
           var y = originalData[j][1][0];
           if (!y || isNaN(y)) continue;
           num_ok++;
@@ -1356,7 +1364,7 @@ Dygraph.prototype.parseCSV_ = function(data) {
       for (var j = 1; j < inFields.length; j += 2)
         fields[(j + 1) / 2] = [parseFloat(inFields[j]),
                                parseFloat(inFields[j + 1])];
-    } else if (this.customBars_) {
+    } else if (this.attr_("customBars")) {
       // Bars are a low;center;high tuple
       for (var j = 1; j < inFields.length; j++) {
         var vals = inFields[j].split(";");
@@ -1538,9 +1546,6 @@ Dygraph.prototype.start_ = function() {
  */
 Dygraph.prototype.updateOptions = function(attrs) {
   // TODO(danvk): this is a mess. Rethink this function.
-  if (attrs.customBars) {
-    this.customBars_ = attrs.customBars;
-  }
   if (attrs.rollPeriod) {
     this.rollPeriod_ = attrs.rollPeriod;
   }