Add drawAxis option, deprecating drawXAxis and drawYAxis. Added tests for that and...
[dygraphs.git] / plugins / axes.js
index e74f289..6623a0b 100644 (file)
@@ -4,6 +4,8 @@
  * MIT-licensed (http://opensource.org/licenses/MIT)
  */
 
+/*global Dygraph:false */
+
 Dygraph.Plugins.Axes = (function() {
 
 "use strict";
@@ -50,12 +52,12 @@ axes.prototype.activate = function(g) {
 axes.prototype.layout = function(e) {
   var g = e.dygraph;
 
-  if (g.getOption('drawYAxis')) {
+  if (g.getOptionForAxis('drawAxis', 'y')) {
     var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
-    var y_axis_rect = e.reserveSpaceLeft(w);
+    e.reserveSpaceLeft(w);
   }
 
-  if (g.getOption('drawXAxis')) {
+  if (g.getOptionForAxis('drawAxis', 'x')) {
     var h;
     // NOTE: I think this is probably broken now, since g.getOption() now
     // hits the dictionary. (That is, g.getOption('xAxisHeight') now always
@@ -65,13 +67,16 @@ axes.prototype.layout = function(e) {
     } else {
       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);
+    // TODO(danvk): introduce a 'drawAxis' per-axis property.
+    if (g.getOptionForAxis('drawAxis', 'y')) {
+      // TODO(danvk): per-axis setting.
+      var w = g.getOption('yAxisLabelWidth') + 2 * g.getOption('axisTickSize');
+      e.reserveSpaceRight(w);
+    }
   } else if (g.numAxes() > 2) {
     g.error("Only two y-axes are supported at this time. (Trying " +
             "to use " + g.numAxes() + ")");
@@ -93,13 +98,13 @@ axes.prototype.detachLabels = function() {
 };
 
 axes.prototype.clearChart = function(e) {
-  var g = e.dygraph;
   this.detachLabels();
 };
 
 axes.prototype.willDrawChart = function(e) {
   var g = e.dygraph;
-  if (!g.getOption('drawXAxis') && !g.getOption('drawYAxis')) return;
+
+  if (!g.getOptionForAxis('drawAxis', 'x') && !g.getOptionForAxis('drawAxis', 'y')) return;
   
   // Round pixels to half-integer boundaries for crisper drawing.
   function halfUp(x)  { return Math.round(x) + 0.5; }
@@ -123,12 +128,12 @@ axes.prototype.willDrawChart = function(e) {
       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'),
+    y2 : makeLabelStyle('y2')
   };
 
   var makeDiv = function(txt, axis, prec_axis) {
@@ -160,7 +165,7 @@ axes.prototype.willDrawChart = function(e) {
   var layout = g.layout_;
   var area = e.dygraph.plotter_.area;
 
-  if (g.getOption('drawYAxis')) {
+  if (g.getOptionForAxis('drawAxis', 'y')) {
     if (layout.yticks && layout.yticks.length > 0) {
       var num_axes = g.numAxes();
       for (i = 0; i < layout.yticks.length; i++) {
@@ -224,7 +229,7 @@ axes.prototype.willDrawChart = function(e) {
     var axisX;
     if (g.getOption('drawAxesAtZero')) {
       var r = g.toPercentXCoord(0);
-      if (r > 1 || r < 0) r = 0;
+      if (r > 1 || r < 0 || isNaN(r)) r = 0;
       axisX = halfUp(area.x + r * area.w);
     } else {
       axisX = halfUp(area.x);
@@ -251,7 +256,7 @@ axes.prototype.willDrawChart = function(e) {
     }
   }
 
-  if (g.getOption('drawXAxis')) {
+  if (g.getOptionForAxis('drawAxis', 'x')) {
     if (layout.xticks) {
       for (i = 0; i < layout.xticks.length; i++) {
         tick = layout.xticks[i];