Fix bug 436. Only restore axes that remain after an option update.
[dygraphs.git] / plugins / axes.js
index 0c81759..1e17ff2 100644 (file)
@@ -4,15 +4,27 @@
  * MIT-licensed (http://opensource.org/licenses/MIT)
  */
 
+/*global Dygraph:false */
+
 Dygraph.Plugins.Axes = (function() {
 
-/*
+"use strict";
 
+/*
 Bits of jankiness:
 - Direct layout access
 - Direct area access
 - Should include calculation of ticks, not just the drawing.
 
+Options left to make axis-friendly.
+  ('axisTickSize')
+  ('drawAxesAtZero')
+  ('xAxisHeight')
+
+These too. What is the difference between axisLablelWidth and {x,y}AxisLabelWidth?
+  ('axisLabelWidth')
+  ('xAxisLabelWidth')
+  ('yAxisLabelWidth')
 */
 
 /**
@@ -33,7 +45,7 @@ axes.prototype.activate = function(g) {
   return {
     layout: this.layout,
     clearChart: this.clearChart,
-    drawChart: this.drawChart
+    willDrawChart: this.willDrawChart
   };
 };
 
@@ -42,23 +54,26 @@ axes.prototype.layout = function(e) {
 
   if (g.getOption('drawYAxis')) {
     var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
-    var y_axis_rect = e.reserveSpaceLeft(w);
+    e.reserveSpaceLeft(w);
   }
 
   if (g.getOption('drawXAxis')) {
     var h;
+    // NOTE: I think this is probably broken now, since g.getOption() now
+    // hits the dictionary. (That is, g.getOption('xAxisHeight') now always
+    // has a value.)
     if (g.getOption('xAxisHeight')) {
       h = g.getOption('xAxisHeight');
     } else {
-      h = g.getOption('axisLabelFontSize') + 2 * g.getOption('axisTickSize');
+      h = g.getOptionForAxis('axisLabelFontSize', 'x') + 2 * g.getOption('axisTickSize');
     }
-    var x_axis_rect = e.reserveSpaceBottom(h);
+    e.reserveSpaceBottom(h);
   }
 
   if (g.numAxes() == 2) {
     // TODO(danvk): per-axis setting.
     var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
-    var y2_axis_rect = e.reserveSpaceRight(w);
+    e.reserveSpaceRight(w);
   } else if (g.numAxes() > 2) {
     g.error("Only two y-axes are supported at this time. (Trying " +
             "to use " + g.numAxes() + ")");
@@ -80,11 +95,10 @@ axes.prototype.detachLabels = function() {
 };
 
 axes.prototype.clearChart = function(e) {
-  var g = e.dygraph;
   this.detachLabels();
-}
+};
 
-axes.prototype.drawChart = function(e) {
+axes.prototype.willDrawChart = function(e) {
   var g = e.dygraph;
   if (!g.getOption('drawXAxis') && !g.getOption('drawYAxis')) return;
   
@@ -94,21 +108,39 @@ axes.prototype.drawChart = function(e) {
 
   var context = e.drawingContext;
   var containerDiv = e.canvas.parentNode;
+  var canvasWidth = e.canvas.width;
+  var canvasHeight = e.canvas.height;
 
   var label, x, y, tick, i;
 
-  var labelStyle = {
-    position: "absolute",
-    fontSize: g.getOption('axisLabelFontSize') + "px",
-    zIndex: 10,
-    color: g.getOption('axisLabelColor'),
-    width: g.getOption('axisLabelWidth') + "px",
-    // height: this.attr_('axisLabelFontSize') + 2 + "px",
-    lineHeight: "normal",  // Something other than "normal" line-height screws up label positioning.
-    overflow: "hidden"
+  var makeLabelStyle = function(axis) {
+    return {
+      position: "absolute",
+      fontSize: g.getOptionForAxis('axisLabelFontSize', axis) + "px",
+      zIndex: 10,
+      color: g.getOptionForAxis('axisLabelColor', axis),
+      width: g.getOption('axisLabelWidth') + "px",
+      // height: g.getOptionForAxis('axisLabelFontSize', 'x') + 2 + "px",
+      lineHeight: "normal",  // Something other than "normal" line-height screws up label positioning.
+      overflow: "hidden"
+    };
   };
+
+  var labelStyles = {
+    x : makeLabelStyle('x'),
+    y : makeLabelStyle('y'),
+    y2 : makeLabelStyle('y2')
+  };
+
   var makeDiv = function(txt, axis, prec_axis) {
+    /*
+     * This seems to be called with the following three sets of axis/prec_axis:
+     * x: undefined
+     * y: y1
+     * y: y2
+     */
     var div = document.createElement("div");
+    var labelStyle = labelStyles[prec_axis == 'y2' ? 'y2' : axis];
     for (var name in labelStyle) {
       if (labelStyle.hasOwnProperty(name)) {
         div.style[name] = labelStyle[name];
@@ -125,8 +157,6 @@ axes.prototype.drawChart = function(e) {
 
   // axis lines
   context.save();
-  context.strokeStyle = g.getOption('axisLineColor');
-  context.lineWidth = g.getOption('axisLineWidth');
 
   var layout = g.layout_;
   var area = e.dygraph.plotter_.area;
@@ -145,6 +175,7 @@ axes.prototype.drawChart = function(e) {
           sgn = -1;
           prec_axis = 'y2';
         }
+        var fontSize = g.getOptionForAxis('axisLabelFontSize', prec_axis);
         y = area.y + tick[1] * area.h;
 
         /* Tick marks are currently clipped, so don't bother drawing them.
@@ -156,10 +187,10 @@ axes.prototype.drawChart = function(e) {
         */
 
         label = makeDiv(tick[2], 'y', num_axes == 2 ? prec_axis : null);
-        var top = (y - g.getOption('axisLabelFontSize') / 2);
+        var top = (y - fontSize / 2);
         if (top < 0) top = 0;
 
-        if (top + g.getOption('axisLabelFontSize') + 3 > this.height) {
+        if (top + fontSize + 3 > canvasHeight) {
           label.style.bottom = "0px";
         } else {
           label.style.top = top + "px";
@@ -181,9 +212,10 @@ axes.prototype.drawChart = function(e) {
       // tick on the x-axis. Shift the bottom tick up a little bit to
       // compensate if necessary.
       var bottomTick = this.ylabels_[0];
-      var fontSize = g.getOption('axisLabelFontSize');
+      // Interested in the y2 axis also?
+      var fontSize = g.getOptionForAxis('axisLabelFontSize', "y");
       var bottom = parseInt(bottomTick.style.top, 10) + fontSize;
-      if (bottom > this.height - fontSize) {
+      if (bottom > canvasHeight - fontSize) {
         bottomTick.style.top = (parseInt(bottomTick.style.top, 10) -
             fontSize / 2) + "px";
       }
@@ -192,12 +224,16 @@ axes.prototype.drawChart = function(e) {
     // draw a vertical line on the left to separate the chart from the labels.
     var axisX;
     if (g.getOption('drawAxesAtZero')) {
-      var r = this.dygraph_.toPercentXCoord(0);
+      var r = g.toPercentXCoord(0);
       if (r > 1 || r < 0) r = 0;
       axisX = halfUp(area.x + r * area.w);
     } else {
       axisX = halfUp(area.x);
     }
+
+    context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y');
+    context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y');
+
     context.beginPath();
     context.moveTo(axisX, halfDown(area.y));
     context.lineTo(axisX, halfDown(area.y + area.h));
@@ -206,6 +242,8 @@ axes.prototype.drawChart = function(e) {
 
     // if there's a secondary y-axis, draw a vertical line for that, too.
     if (g.numAxes() == 2) {
+      context.strokeStyle = g.getOptionForAxis('axisLineColor', 'y2');
+      context.lineWidth = g.getOptionForAxis('axisLineWidth', 'y2');
       context.beginPath();
       context.moveTo(halfDown(area.x + area.w), halfDown(area.y));
       context.lineTo(halfDown(area.x + area.w), halfDown(area.y + area.h));
@@ -234,8 +272,8 @@ axes.prototype.drawChart = function(e) {
         label.style.top = (y + g.getOption('axisTickSize')) + 'px';
 
         var left = (x - g.getOption('axisLabelWidth')/2);
-        if (left + g.getOption('axisLabelWidth') > this.width) {
-          left = this.width - g.getOption('xAxisLabelWidth');
+        if (left + g.getOption('axisLabelWidth') > canvasWidth) {
+          left = canvasWidth - g.getOption('xAxisLabelWidth');
           label.style.textAlign = "right";
         }
         if (left < 0) {
@@ -250,6 +288,8 @@ axes.prototype.drawChart = function(e) {
       }
     }
 
+    context.strokeStyle = g.getOptionForAxis('axisLineColor', 'x');
+    context.lineWidth = g.getOptionForAxis('axisLineWidth', 'x');
     context.beginPath();
     var axisY;
     if (g.getOption('drawAxesAtZero')) {
@@ -266,7 +306,7 @@ axes.prototype.drawChart = function(e) {
   }
 
   context.restore();
-}
+};
 
 return axes;
 })();