Closure tweak: variable 'd' declared twice within the same method.
[dygraphs.git] / dygraph-tickers.js
index e57e8bd..155afb0 100644 (file)
@@ -340,25 +340,6 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) {
   var ticks = [];
   var t;
 
-  var setters = {
-    ms: Date.prototype.setMilliseconds,
-    s: Date.prototype.setSeconds,
-    m: Date.prototype.setMinutes,
-    h: Date.prototype.setHours
-  };
-  var safeSet = function(d, parts) {
-    var tz = d.getTimezoneOffset();
-    for (var k in parts) {
-      if (!parts.hasOwnProperty(k)) continue;
-      var setter = setters[k];
-      if (!setter) throw "Invalid setter: " + k;
-      setter.call(d, parts[k]);
-      if (d.getTimezoneOffset() != tz) {
-        d.setTime(d.getTime() + (tz - d.getTimezoneOffset()) * 60 * 1000);
-      }
-    }
-  };
-
   if (granularity < Dygraph.MONTHLY) {
     // Generate one tick mark for every fixed interval of time.
     var spacing = Dygraph.SHORT_SPACINGS[granularity];
@@ -367,20 +348,20 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) {
     // for this granularity.
     var g = spacing / 1000;
     var d = new Date(start_time);
-    safeSet(d, {ms: 0});
+    Dygraph.setDateSameTZ(d, {ms: 0});
 
     var x;
     if (g <= 60) {  // seconds
       x = d.getSeconds();
-      safeSet(d, {s: x - x % g});
+      Dygraph.setDateSameTZ(d, {s: x - x % g});
     } else {
-      safeSet(d, {s: 0});
+      Dygraph.setDateSameTZ(d, {s: 0});
       g /= 60;
       if (g <= 60) {  // minutes
         x = d.getMinutes();
-        safeSet(d, {m: x - x % g});
+        Dygraph.setDateSameTZ(d, {m: x - x % g});
       } else {
-        safeSet(d, {m: 0});
+        Dygraph.setDateSameTZ(d, {m: 0});
         g /= 60;
 
         if (g <= 24) {  // days
@@ -398,19 +379,34 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) {
     }
     start_time = d.getTime();
 
+    // For spacings coarser than two-hourly, we want to ignore daylight
+    // savings transitions to get consistent ticks. For finer-grained ticks,
+    // it's essential to show the DST transition in all its messiness.
     var start_offset_min = new Date(start_time).getTimezoneOffset();
     var check_dst = (spacing >= Dygraph.SHORT_SPACINGS[Dygraph.TWO_HOURLY]);
 
     for (t = start_time; t <= end_time; t += spacing) {
-      var d = new Date(t);
+      d = new Date(t);
 
       // This ensures that we stay on the same hourly "rhythm" across
       // daylight savings transitions. Without this, the ticks could get off
       // by an hour. See tests/daylight-savings.html or issue 147.
       if (check_dst && d.getTimezoneOffset() != start_offset_min) {
-        t += (d.getTimezoneOffset() - start_offset_min) * 60 * 1000;
+        var delta_min = d.getTimezoneOffset() - start_offset_min;
+        t += delta_min * 60 * 1000;
         d = new Date(t);
         start_offset_min = d.getTimezoneOffset();
+
+        // Check whether we've backed into the previous timezone again.
+        // This can happen during a "spring forward" transition. In this case,
+        // it's best to skip this tick altogether (we may be shooting for a
+        // non-existent time like the 2AM that's skipped) and go to the next
+        // one.
+        if (new Date(t + spacing).getTimezoneOffset() != start_offset_min) {
+          t += spacing;
+          d = new Date(t);
+          start_offset_min = d.getTimezoneOffset();
+        }
       }
 
       ticks.push({ v:t,
@@ -463,11 +459,13 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) {
 
 // These are set here so that this file can be included after dygraph.js
 // or independently.
-Dygraph.DEFAULT_ATTRS = Dygraph.DEFAULT_ATTRS || {};
-Dygraph.DEFAULT_ATTRS['axes'] = Dygraph.DEFAULT_ATTRS['axes'] || {};
-Dygraph.DEFAULT_ATTRS['axes']['x'] = Dygraph.DEFAULT_ATTRS['axes']['x'] || {};
-Dygraph.DEFAULT_ATTRS['axes']['y'] = Dygraph.DEFAULT_ATTRS['axes']['y'] || {};
-Dygraph.DEFAULT_ATTRS['axes']['y2'] = Dygraph.DEFAULT_ATTRS['axes']['y2'] || {};
-Dygraph.DEFAULT_ATTRS['axes']['x']['ticker'] = Dygraph.dateTicker;
-Dygraph.DEFAULT_ATTRS['axes']['y']['ticker'] = Dygraph.numericTicks;
-Dygraph.DEFAULT_ATTRS['axes']['y2']['ticker'] = Dygraph.numericTicks;
+if (Dygraph &&
+    Dygraph.DEFAULT_ATTRS &&
+    Dygraph.DEFAULT_ATTRS['axes'] &&
+    Dygraph.DEFAULT_ATTRS['axes']['x'] &&
+    Dygraph.DEFAULT_ATTRS['axes']['y'] &&
+    Dygraph.DEFAULT_ATTRS['axes']['y2']) {
+  Dygraph.DEFAULT_ATTRS['axes']['x']['ticker'] = Dygraph.dateTicker;
+  Dygraph.DEFAULT_ATTRS['axes']['y']['ticker'] = Dygraph.numericTicks;
+  Dygraph.DEFAULT_ATTRS['axes']['y2']['ticker'] = Dygraph.numericTicks;
+}