Annotation date parser was failing, but not providing the correct
[dygraphs.git] / dygraph-canvas.js
index c2b2e24..d6570d1 100644 (file)
@@ -70,7 +70,7 @@ DygraphLayout.prototype.setAnnotations = function(ann) {
       return;
     }
     Dygraph.update(a, ann[i]);
-    if (!a.xval) a.xval = parse(a.x);
+    if (!a.xval) a.xval = parse(a.x, this.dygraph_);
     this.annotations.push(a);
   }
 };
@@ -330,15 +330,22 @@ DygraphCanvasRenderer.prototype.attr_ = function(x) {
 
 // Compute the box which the chart should be drawn in. This is the canvas's
 // box, less space needed for axis and chart labels.
+// TODO(danvk): this belongs in DygraphLayout.
 DygraphCanvasRenderer.prototype.computeArea_ = function() {
   var area = {
     // TODO(danvk): per-axis setting.
-    x: this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize'),
+    x: 0,
     y: 0
   };
+  if (this.attr_('drawYAxis')) {
+   area.x = this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize');
+  }
+
   area.w = this.width - area.x - this.attr_('rightGap');
-  area.h = this.height - this.attr_('axisLabelFontSize') -
-                2 * this.attr_('axisTickSize');
+  area.h = this.height;
+  if (this.attr_('drawXAxis')) {
+    area.h -= this.attr_('axisLabelFontSize') + 2 * this.attr_('axisTickSize');
+  }
 
   // Shrink the drawing area to accomodate additional y-axes.
   if (this.dygraph_.numAxes() == 2) {
@@ -432,6 +439,14 @@ DygraphCanvasRenderer.isSupported = function(canvasName) {
 };
 
 /**
+ * @param { [String] } colors Array of color strings. Should have one entry for
+ * each series to be rendered.
+ */
+DygraphCanvasRenderer.prototype.setColors = function(colors) {
+  this.colorScheme_ = colors;
+};
+
+/**
  * Draw an X/Y grid on top of the existing plot
  */
 DygraphCanvasRenderer.prototype.render = function() {
@@ -451,7 +466,7 @@ DygraphCanvasRenderer.prototype.render = function() {
     var ticks = this.layout.yticks;
     ctx.save();
     ctx.strokeStyle = this.attr_('gridLineColor');
-    ctx.lineWidth = this.attr_('axisLineWidth');
+    ctx.lineWidth = this.attr_('gridLineWidth');
     for (var i = 0; i < ticks.length; i++) {
       // TODO(danvk): allow secondary axes to draw a grid, too.
       if (ticks[i][0] != 0) continue;
@@ -469,7 +484,7 @@ DygraphCanvasRenderer.prototype.render = function() {
     var ticks = this.layout.xticks;
     ctx.save();
     ctx.strokeStyle = this.attr_('gridLineColor');
-    ctx.lineWidth = this.attr_('axisLineWidth');
+    ctx.lineWidth = this.attr_('gridLineWidth');
     for (var i=0; i<ticks.length; i++) {
       var x = halfUp(this.area.x + ticks[i][0] * this.area.w);
       var y = halfDown(this.area.y + this.area.h);
@@ -506,14 +521,18 @@ DygraphCanvasRenderer.prototype._renderAxis = function() {
     width: this.attr_('axisLabelWidth') + "px",
     overflow: "hidden"
   };
-  var makeDiv = function(txt) {
+  var makeDiv = function(txt, axis) {
     var div = document.createElement("div");
     for (var name in labelStyle) {
       if (labelStyle.hasOwnProperty(name)) {
         div.style[name] = labelStyle[name];
       }
     }
-    div.appendChild(document.createTextNode(txt));
+    var inner_div = document.createElement("div");
+    // TODO(danvk): separate class for secondary y-axis
+    inner_div.className = 'dygraph-axis-label dygraph-axis-label-' + axis;
+    inner_div.appendChild(document.createTextNode(txt));
+    div.appendChild(inner_div);
     return div;
   };
 
@@ -536,11 +555,11 @@ DygraphCanvasRenderer.prototype._renderAxis = function() {
         var y = this.area.y + tick[1] * this.area.h;
         context.beginPath();
         context.moveTo(halfUp(x), halfDown(y));
-        context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize'), halfDown(y));
+        context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
         context.closePath();
         context.stroke();
 
-        var label = makeDiv(tick[2]);
+        var label = makeDiv(tick[2], 'y');
         var top = (y - this.attr_('axisLabelFontSize') / 2);
         if (top < 0) top = 0;
 
@@ -605,7 +624,7 @@ DygraphCanvasRenderer.prototype._renderAxis = function() {
         context.closePath();
         context.stroke();
 
-        var label = makeDiv(tick[1]);
+        var label = makeDiv(tick[1], 'x');
         label.style.textAlign = "center";
         label.style.top = (y + this.attr_('axisTickSize')) + 'px';
 
@@ -836,10 +855,8 @@ DygraphCanvasRenderer.prototype._renderAnnotations = function() {
 DygraphCanvasRenderer.prototype._renderLineChart = function() {
   // TODO(danvk): use this.attr_ for many of these.
   var context = this.elementContext;
-  var colorCount = this.options.colorScheme.length;
-  var colorScheme = this.options.colorScheme;
   var fillAlpha = this.attr_('fillAlpha');
-  var errorBars = this.attr_("errorBars");
+  var errorBars = this.attr_("errorBars") || this.attr_("customBars");
   var fillGraph = this.attr_("fillGraph");
   var stackedGraph = this.attr_("stackedGraph");
   var stepPlot = this.attr_("stepPlot");
@@ -852,9 +869,10 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() {
   }
   var setCount = setNames.length;
 
+  // TODO(danvk): Move this mapping into Dygraph and get it out of here.
   this.colors = {}
   for (var i = 0; i < setCount; i++) {
-    this.colors[setNames[i]] = colorScheme[i % colorCount];
+    this.colors[setNames[i]] = this.colorScheme_[i % this.colorScheme_.length];
   }
 
   // Update Points
@@ -1021,7 +1039,7 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() {
           var isIsolated = (!prevX && (j == points.length - 1 ||
                                        !Dygraph.isOK(points[j+1].canvasy)));
 
-          if (!prevX) {
+          if (prevX === null) {
             prevX = point.canvasx;
             prevY = point.canvasy;
           } else {