Skip to content

Commit

Permalink
Merge pull request #64 from peterschwarz/master
Browse files Browse the repository at this point in the history
Only consider entries less than now (in the subdomain) as zero, when considering null as zero
  • Loading branch information
wa0x6e committed Dec 23, 2013
2 parents da6b304 + 93d2bfc commit 0d1373b
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 8 deletions.
48 changes: 46 additions & 2 deletions cal-heatmap.js
@@ -1,4 +1,4 @@
/*! cal-heatmap v3.3.10 (Tue Dec 03 2013 19:30:01)
/*! cal-heatmap v3.3.10 (Mon Dec 09 2013 14:28:41)
* ---------------------------------------------
* Cal-Heatmap is a javascript module to create calendar heatmap to visualize time series data
* https://github.com/kamisama/cal-heatmap
Expand Down Expand Up @@ -1390,7 +1390,8 @@ CalHeatMap.prototype = {

if (d.v !== null) {
htmlClass += " " + parent.Legend.getClass(d.v, (parent.legendScale === null));
} else if (options.considerMissingDataAsZero) {
} else if (options.considerMissingDataAsZero &&
parent.dateIsLessThan(d.t, new Date())) {
htmlClass += " " + parent.Legend.getClass(0, (parent.legendScale === null));
}

Expand Down Expand Up @@ -1877,6 +1878,49 @@ CalHeatMap.prototype = {
}
},


/**
* Returns weather or not dateA is less than or equal to dateB. This function is subdomain aware.
* Performs automatic conversion of values.
* @param dateA may be a number or a Date
* @param dateB may be a number or a Date
* @returns {boolean}
*/
dateIsLessThan: function(dateA, dateB) {
"use strict";

if(!(dateA instanceof Date))
dateA = new Date(dateA);

if (!(dateB instanceof Date))
dateB = new Date(dateB);


function normalizedMillis(date, subdomain) {
switch(subdomain) {
case "x_min":
case "min":
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes()).getTime();
case "x_hour":
case "hour":
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours()).getTime();
case "x_day":
case "day":
return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
case "x_week":
case "week":
case "x_month":
case "month":
return new Date(date.getFullYear(), date.getMonth()).getTime();
default:
return date.getTime();
}
}

return normalizedMillis(dateA, this.options.subDomain) < normalizedMillis(dateB, this.options.subDomain);
},


// =========================================================================//
// DATE COMPUTATION //
// =========================================================================//
Expand Down
6 changes: 3 additions & 3 deletions cal-heatmap.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cal-heatmap.source-map.js

Large diffs are not rendered by default.

46 changes: 45 additions & 1 deletion src/cal-heatmap.js
Expand Up @@ -1382,7 +1382,8 @@ CalHeatMap.prototype = {

if (d.v !== null) {
htmlClass += " " + parent.Legend.getClass(d.v, (parent.legendScale === null));
} else if (options.considerMissingDataAsZero) {
} else if (options.considerMissingDataAsZero &&
parent.dateIsLessThan(d.t, new Date())) {
htmlClass += " " + parent.Legend.getClass(0, (parent.legendScale === null));
}

Expand Down Expand Up @@ -1869,6 +1870,49 @@ CalHeatMap.prototype = {
}
},


/**
* Returns weather or not dateA is less than or equal to dateB. This function is subdomain aware.
* Performs automatic conversion of values.
* @param dateA may be a number or a Date
* @param dateB may be a number or a Date
* @returns {boolean}
*/
dateIsLessThan: function(dateA, dateB) {
"use strict";

if(!(dateA instanceof Date))
dateA = new Date(dateA);

if (!(dateB instanceof Date))
dateB = new Date(dateB);


function normalizedMillis(date, subdomain) {
switch(subdomain) {
case "x_min":
case "min":
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes()).getTime();
case "x_hour":
case "hour":
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours()).getTime();
case "x_day":
case "day":
return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
case "x_week":
case "week":
case "x_month":
case "month":
return new Date(date.getFullYear(), date.getMonth()).getTime();
default:
return date.getTime();
}
}

return normalizedMillis(dateA, this.options.subDomain) < normalizedMillis(dateB, this.options.subDomain);
},


// =========================================================================//
// DATE COMPUTATION //
// =========================================================================//
Expand Down
71 changes: 71 additions & 0 deletions test/src/date/dateIsLessThan.js
@@ -0,0 +1,71 @@

module("Date computation : dateLessThan()");


var min = 60 * 1000; // one min millis
var hour = 60 * min; // one hour millis
var day = 24 * hour; // one hour millis
var month = 30 * day; // one (average month);


test("date is less than now in the domain hour, subdomain min", function() {
expect(6);

var cal = createCalendar({});

var now = new Date(2013, 12, 9, 12, 30, 30, 0); // 12:30:30, 2013-12-9


ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - min), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * 1000), now)); // still within the min
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * 1000), now)); // still within the min
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + min), now));
});

test("date is less than now in the domain day, subdomain hour", function() {
expect(6);

var cal = createCalendar({domain: "day", subDomain: "hour"});

var now = new Date(2013, 12, 9, 12, 30, 0, 0); // 12:30, 2013-12-9

ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - hour), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * 60 * 1000), now)); // still within the hour
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * 60 * 1000), now)); // still within the hour
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + hour), now));
});

test("date is less than now in the domain month, subdomain day", function() {
expect(6);

var cal = createCalendar({domain: "month", subDomain: "day"});

var now = new Date(2013, 12, 9, 12, 30, 0, 0); // 12:30, 2013-12-9

ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - day), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * hour), now)); // still within the day
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * hour), now)); // still within the day
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + day), now));
});


test("date is less than now in the domain month, subdomain day", function() {
expect(6);

var cal = createCalendar({domain: "year", subDomain: "month"});

var now = new Date(2013, 12, 9, 12, 30, 0, 0); // 12:30, 2013-12-9

ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - month), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * day), now)); // still within the month
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * day), now)); // still within the month
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + month), now));
});
74 changes: 73 additions & 1 deletion test/test.js
@@ -1,4 +1,4 @@
/*! cal-heatmap v3.3.9 (Sun Nov 24 2013 15:00:55)
/*! cal-heatmap v3.3.10 (Mon Dec 09 2013 14:28:41)
* ---------------------------------------------
* Cal-Heatmap is a javascript module to create calendar heatmap to visualize time series data
* https://github.com/kamisama/cal-heatmap
Expand Down Expand Up @@ -1574,6 +1574,78 @@ asyncTest("Parsing a TXT file", function() {
), false);
});


module("Date computation : dateLessThan()");


var min = 60 * 1000; // one min millis
var hour = 60 * min; // one hour millis
var day = 24 * hour; // one hour millis
var month = 30 * day; // one (average month);


test("date is less than now in the domain hour, subdomain min", function() {
expect(6);

var cal = createCalendar({});

var now = new Date(2013, 12, 9, 12, 30, 30, 0); // 12:30:30, 2013-12-9


ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - min), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * 1000), now)); // still within the min
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * 1000), now)); // still within the min
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + min), now));
});

test("date is less than now in the domain day, subdomain hour", function() {
expect(6);

var cal = createCalendar({domain: "day", subDomain: "hour"});

var now = new Date(2013, 12, 9, 12, 30, 0, 0); // 12:30, 2013-12-9

ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - hour), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * 60 * 1000), now)); // still within the hour
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * 60 * 1000), now)); // still within the hour
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + hour), now));
});

test("date is less than now in the domain month, subdomain day", function() {
expect(6);

var cal = createCalendar({domain: "month", subDomain: "day"});

var now = new Date(2013, 12, 9, 12, 30, 0, 0); // 12:30, 2013-12-9

ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - day), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * hour), now)); // still within the day
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * hour), now)); // still within the day
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + day), now));
});


test("date is less than now in the domain month, subdomain day", function() {
expect(6);

var cal = createCalendar({domain: "year", subDomain: "month"});

var now = new Date(2013, 12, 9, 12, 30, 0, 0); // 12:30, 2013-12-9

ok(cal.dateIsLessThan(new Date(0), now));
ok(cal.dateIsLessThan(new Date(now.getTime() - month), now));
ok(!cal.dateIsLessThan(new Date(now.getTime() - 5 * day), now)); // still within the month
ok(!cal.dateIsLessThan(new Date(now.getTime() + 5 * day), now)); // still within the month
ok(!cal.dateIsLessThan(now, now));
ok(!cal.dateIsLessThan(new Date(now.getTime() + month), now));
});

/*
-----------------------------------------------------------------
DATE
Expand Down

0 comments on commit 0d1373b

Please sign in to comment.