more x-axis time resolutions
authorDan Vanderkam <danvdk@gmail.com>
Thu, 5 Nov 2009 21:48:52 +0000 (21:48 +0000)
committerDan Vanderkam <danvdk@gmail.com>
Thu, 5 Nov 2009 21:48:52 +0000 (21:48 +0000)
dygraph.js
tests/hourly.html

index 9cec98e..9a948de 100644 (file)
@@ -648,23 +648,33 @@ DateGraph.prototype.addXTicks_ = function() {
 
 // Time granularity enumeration
 DateGraph.SECONDLY = 0;
-DateGraph.MINUTELY = 1;
-DateGraph.HOURLY = 2;
-DateGraph.DAILY = 3;
-DateGraph.WEEKLY = 4;
-DateGraph.MONTHLY = 5;
-DateGraph.QUARTERLY = 6;
-DateGraph.BIANNUAL = 7;
-DateGraph.ANNUAL = 8;
-DateGraph.DECADAL = 9;
-DateGraph.NUM_GRANULARITIES = 10;
+DateGraph.TEN_SECONDLY = 1;
+DateGraph.THIRTY_SECONDLY  = 2;
+DateGraph.MINUTELY = 3;
+DateGraph.TEN_MINUTELY = 4;
+DateGraph.THIRTY_MINUTELY = 5;
+DateGraph.HOURLY = 6;
+DateGraph.SIX_HOURLY = 7;
+DateGraph.DAILY = 8;
+DateGraph.WEEKLY = 9;
+DateGraph.MONTHLY = 10;
+DateGraph.QUARTERLY = 11;
+DateGraph.BIANNUAL = 12;
+DateGraph.ANNUAL = 13;
+DateGraph.DECADAL = 14;
+DateGraph.NUM_GRANULARITIES = 15;
 
 DateGraph.SHORT_SPACINGS = [];
-DateGraph.SHORT_SPACINGS[DateGraph.SECONDLY] = 1000 * 1;
-DateGraph.SHORT_SPACINGS[DateGraph.MINUTELY] = 1000 * 60;
-DateGraph.SHORT_SPACINGS[DateGraph.HOURLY]   = 1000 * 3600;
-DateGraph.SHORT_SPACINGS[DateGraph.DAILY]    = 1000 * 86400;
-DateGraph.SHORT_SPACINGS[DateGraph.WEEKLY]   = 1000 * 604800;
+DateGraph.SHORT_SPACINGS[DateGraph.SECONDLY]        = 1000 * 1;
+DateGraph.SHORT_SPACINGS[DateGraph.TEN_SECONDLY]    = 1000 * 10;
+DateGraph.SHORT_SPACINGS[DateGraph.THIRTY_SECONDLY] = 1000 * 30;
+DateGraph.SHORT_SPACINGS[DateGraph.MINUTELY]        = 1000 * 60;
+DateGraph.SHORT_SPACINGS[DateGraph.TEN_MINUTELY]    = 1000 * 60 * 10;
+DateGraph.SHORT_SPACINGS[DateGraph.THIRTY_MINUTELY] = 1000 * 60 * 30;
+DateGraph.SHORT_SPACINGS[DateGraph.HOURLY]          = 1000 * 3600;
+DateGraph.SHORT_SPACINGS[DateGraph.HOURLY]          = 1000 * 3600 * 6;
+DateGraph.SHORT_SPACINGS[DateGraph.DAILY]           = 1000 * 86400;
+DateGraph.SHORT_SPACINGS[DateGraph.WEEKLY]          = 1000 * 604800;
 
 // NumXTicks()
 //
@@ -703,6 +713,10 @@ DateGraph.prototype.GetXAxis = function(start_time, end_time, granularity) {
     // Generate one tick mark for every fixed interval of time.
     var spacing = DateGraph.SHORT_SPACINGS[granularity];
     var format = '%d%b';  // e.g. "1 Jan"
+    // TODO(danvk): be smarter about making sure this really hits a "nice" time.
+    if (granularity < DateGraph.HOURLY) {
+      start_time = spacing * Math.floor(0.5 + start_time / spacing);
+    }
     for (var t = start_time; t <= end_time; t += spacing) {
       var d = new Date(t);
       var frac = d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds();
@@ -720,7 +734,6 @@ DateGraph.prototype.GetXAxis = function(start_time, end_time, granularity) {
     var months;
     var year_mod = 1;  // e.g. to only print one point every 10 years.
 
-    // TODO(danvk): use CachingRoundTime where appropriate to get boundaries.
     if (granularity == DateGraph.MONTHLY) {
       months = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ];
     } else if (granularity == DateGraph.QUARTERLY) {
index eb84705..f1aff26 100644 (file)
@@ -14,6 +14,9 @@
     <p>Minutely data:</p>
     <div id="gm" style="width:600px; height:300px;"></div>
 
+    <p>Secondly data:</p>
+    <div id="gs" style="width:600px; height:300px;"></div>
+
     <script type="text/javascript">
       g = new DateGraph(
             document.getElementById("g"),
               return ret;
             }, null, {}
           );
+
+      gs = new DateGraph(
+            document.getElementById("gs"),
+            function() {
+              var ret = "Date,Minutes,Seconds\n";
+              var zp = function(x) { if (x<10) return "0"+x; else return ""+x;};
+              for (var h = 0; h <= 1; h++) {
+                for (var m = 0; m < 60; m++) {
+                  for (var s = 0; s < 60; s++) {
+                    ret += "2008/07/01 " + zp(h) + ":" + zp(m) + ":" + zp(s) +
+                        "," + (h*60 + m) + "," + s + "\n";
+                  }
+                }
+              }
+              return ret;
+            }, null, {}
+          );
     </script>
   </body>
 </html>