diff --git a/assets/src/js/components/Chart.js b/assets/src/js/components/Chart.js index 09d5446a..e0480723 100644 --- a/assets/src/js/components/Chart.js +++ b/assets/src/js/components/Chart.js @@ -12,35 +12,49 @@ const formatMonth = d3.timeFormat("%b"), formatMonthDay = d3.timeFormat("%b %e"); const t = d3.transition().duration(600).ease(d3.easeQuadOut); -const xTickFormat = (len) => { - return { - hour: (d, i) => { - if(len <= 24 && d.getHours() == 0 || d.getHours() == 12) { - return d.getHours() + ":00"; - } - if(i === 0 || i === len-1) { +function xTickFormat(tickStep, n) { + let formatters = { + hour: (d, i) => { + if(i === 0 || i === n-1) { return formatMonthDay(d); } + if(n <= 24 && d.getHours() === 0 || d.getHours() === 12) { + return d.getHours() + ":00"; + } + return ''; }, - day: (d, i) => { - if(i === 0 || i === len-1) { - return formatMonthDay(d); + day: (d, i, o) => { + if( i === 0 || i === n-1) { + return formatMonthDay(d); } return ''; }, month: (d, i) => { - if(len>24) { + if(n>28) { return d.getFullYear(); } return d.getMonth() === 0 ? d.getFullYear() : formatMonth(d); } } + + return formatters[tickStep]; +} + + +const incrementers = { + 'hour': d => d.setHours(d.getHours() + 1), + 'day': d => d.setDate(d.getDate() + 1), + 'month': d => d.setMonth(d.getMonth() + 1) +} + +function incrementDate(date, incr) { + return incrementers[incr](date); } class Chart extends Component { @@ -98,20 +112,7 @@ class Chart extends Component { }; nextDate = new Date(currentDate) - - switch(this.state.tickStep) { - case 'hour': - nextDate.setHours(nextDate.getHours() + 1); - break; - - case 'day': - nextDate.setDate(nextDate.getDate() + 1); - break; - - case 'month': - nextDate.setMonth(nextDate.getMonth() + 1); - break; - } + nextDate = incrementDate(nextDate, this.state.tickStep) // grab data that falls between currentDate & nextDate for(let i=data.length-offset-1; i>=0; i--) { @@ -163,21 +164,25 @@ class Chart extends Component { // tooltip this.tip = d3.tip().attr('class', 'd3-tip').html((d) => { - let title = d.Date.toLocaleDateString(); + let title; if(this.state.tickStep === 'hour') { - title += ` ${d.Date.getHours()}:00 - ${d.Date.getHours() + 1}:00` - } + title = `${d.Date.toLocaleDateString()} ${d.Date.getHours()}:00 - ${d.Date.getHours() + 1}:00` + } else if(this.state.tickStep === 'day' ) { + title = `${d.Date.toLocaleDateString()} (${d3.timeFormat("%a")(d.Date)})` + } else { + title = d3.timeFormat("%B %Y")(d.Date) + } return (`