parse dates in preparedata function, compare using local date objects. #39

This commit is contained in:
Danny 2018-05-25 05:53:17 -08:00
parent f70a492f09
commit 5a89bc6f48

View File

@ -13,21 +13,21 @@ function padZero(s) {
} }
const timeFormats = [ const timeFormats = [
[() => '', function(d, n) { [() => '', function(d, n) {
return true; return true;
}], }],
[d3.timeFormat("%Y"), function (d, i, n) { [d3.timeFormat("%Y"), function (d, i, n) {
return d.getUTCMonth() === 0 && d.getUTCDate() === 1;; return d.getMonth() === 0 && d.getDate() === 1;;
}], }],
[d3.timeFormat("%b"), function (d, i, n) { [d3.timeFormat("%b"), function (d, i, n) {
return ( d.getUTCMonth() > 0 && d.getUTCDate() === 1 ); return ( d.getMonth() > 0 && d.getDate() === 1 );
}], }],
[d3.timeFormat("%d"), function (d, i, n) { [d3.timeFormat("%d"), function (d, i, n) {
return ( d.getUTCDate() > 1 ) && n < 32; return ( d.getDate() > 1 ) && n < 32;
}], }],
[d3.timeFormat("%b %d"), function (d, i, n) { [d3.timeFormat("%b %d"), function (d, i, n) {
return i === 0 && d.getUTCDate() > 1; return i === 0 && d.getDate() > 1;
}] }]
] ]
var timeFormatPicker = function (formats, len) { var timeFormatPicker = function (formats, len) {
@ -48,8 +48,11 @@ function prepareData(startUnix, endUnix, data) {
// create keyed array for quick date access // create keyed array for quick date access
data.forEach((d) => { data.forEach((d) => {
d.Date = d.Date.substring(0, 10); // replace date with actual date object & store in datamap
datamap[d.Date] = d; let date = new Date(d.Date);
let key = date.getFullYear() + "-" + padZero(date.getMonth() + 1) + "-" + padZero(date.getDate());
d.Date = date;
datamap[key] = d;
}); });
// make sure we have values for each date // make sure we have values for each date
@ -59,14 +62,14 @@ function prepareData(startUnix, endUnix, data) {
let data = datamap[key] ? datamap[key] : { let data = datamap[key] ? datamap[key] : {
"Pageviews": 0, "Pageviews": 0,
"Visitors": 0, "Visitors": 0,
"Date": new Date(currentDate),
}; };
// replace Date property with actual date object
data.Date = new Date(currentDate);
newData.push(data); newData.push(data);
currentDate.setDate(currentDate.getDate() + 1); currentDate.setDate(currentDate.getDate() + 1);
} }
return newData; return newData;
} }
@ -137,7 +140,7 @@ class Chart extends Component {
// hide all "day" ticks if we're watching more than 100 days of data // hide all "day" ticks if we're watching more than 100 days of data
xTicks.selectAll('g').style('display', (d, i) => { xTicks.selectAll('g').style('display', (d, i) => {
if(data.length > 100 && d.getUTCDate() > 1 ) { if(data.length > 100 && d.getDate() > 1 ) {
return 'none'; return 'none';
} }