From: George Madrid Date: Fri, 7 Feb 2014 19:05:25 +0000 (-0500) Subject: Fix for ticks when years are < 100. X-Git-Tag: v1.1.0~66^2^2~2 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=14eff0df647bc0edd550f8bbdd5a53500c6b4b59;p=dygraphs.git Fix for ticks when years are < 100. --- diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 42c2c06..062b8b7 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -59,6 +59,7 @@ --> + diff --git a/auto_tests/tests/TwoDigitYears.js b/auto_tests/tests/TwoDigitYears.js new file mode 100644 index 0000000..aeb030e --- /dev/null +++ b/auto_tests/tests/TwoDigitYears.js @@ -0,0 +1,22 @@ +/** + * @fileoverview Test to check that years < 100 get the correct ticks. + * + * @author gmadrid@gmail.com (George Madrid) + */ +var TwoDigitYearsTestCase = TestCase("TwoDigitYears"); + +TwoDigitYearsTestCase.prototype.testTwoDigitYears = function() { + var start = new Date(9, 2, 3); + var end = new Date(11, 3, 5); + + // Javascript will automatically add 1900 to our years if they are < 100. + // Use setFullYear() to get the actual years we desire. + start.setFullYear(9); + end.setFullYear(11); + + var ticks = Dygraph.getDateAxis(start, end, Dygraph.QUARTERLY, function(x) { + return Dygraph.DEFAULT_ATTRS.axes['x'][x]; + }); + + assertEquals([{"v":-61875345600000,"label":"Apr 9"},{"v":-61867483200000,"label":"Jul 9"},{"v":-61859534400000,"label":"Oct 9"},{"v":-61851582000000,"label":"Jan 10"},{"v":-61843809600000,"label":"Apr 10"},{"v":-61835947200000,"label":"Jul 10"},{"v":-61827998400000,"label":"Oct 10"},{"v":-61820046000000,"label":"Jan 11"},{"v":-61812273600000,"label":"Apr 11"}], ticks); +}; diff --git a/dygraph-tickers.js b/dygraph-tickers.js index d2e1003..a3fe90a 100644 --- a/dygraph-tickers.js +++ b/dygraph-tickers.js @@ -456,12 +456,12 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) { var start_year = new Date(start_time).getFullYear(); var end_year = new Date(end_time).getFullYear(); - var zeropad = Dygraph.zeropad; for (var i = start_year; i <= end_year; i++) { if (i % year_mod !== 0) continue; for (var j = 0; j < months.length; j++) { - var date_str = i + "/" + zeropad(1 + months[j]) + "/01"; - t = Dygraph.dateStrToMillis(date_str); + var dt = new Date(i, months[j], 1); + dt.setFullYear(i); + t = dt.getTime(); if (t < start_time || t > end_time) continue; ticks.push({ v:t, label: formatter(new Date(t), granularity, opts, dg)