calendar-heatmap-graph
Advanced tools
Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "calendar-heatmap-graph", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "d3.js calendar heatmap graph.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/g1eb/calendar-heatmap", |
@@ -1,1 +0,2 @@ | ||
"use strict";var calendarHeatmap={settings:{gutter:5,item_gutter:1,width:1e3,height:200,item_size:10,label_padding:40,max_block_height:20,transition_duration:500,tooltip_width:250,tooltip_padding:15},init:function(a,t,e,n){calendarHeatmap.data=a,calendarHeatmap.color=t||"#ff4500",calendarHeatmap.overview=e||"year",calendarHeatmap.history=["year"],calendarHeatmap.selected={},calendarHeatmap.handler=n,calendarHeatmap.in_transition=!1,calendarHeatmap.createElements(),calendarHeatmap.parseData(),calendarHeatmap.drawChart()},createElements:function(){var a=document.createElement("div");a.className="calendar-heatmap",document.body.appendChild(a);var t=d3.select(a).append("svg").attr("class","svg");calendarHeatmap.items=t.append("g"),calendarHeatmap.labels=t.append("g"),calendarHeatmap.buttons=t.append("g"),calendarHeatmap.tooltip=d3.select(a).append("div").attr("class","heatmap-tooltip").style("opacity",0);var e=function(){var e=Math.round((moment()-moment().subtract(1,"year").startOf("week"))/864e5),n=Math.trunc(e/7),r=n+1;calendarHeatmap.settings.width=a.offsetWidth<1e3?1e3:a.offsetWidth,calendarHeatmap.settings.item_size=(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/r-calendarHeatmap.settings.gutter,calendarHeatmap.settings.height=calendarHeatmap.settings.label_padding+7*(calendarHeatmap.settings.item_size+calendarHeatmap.settings.gutter),t.attr({width:calendarHeatmap.settings.width,height:calendarHeatmap.settings.height}),calendarHeatmap.data&&calendarHeatmap.data[0].summary&&calendarHeatmap.drawChart()};e(),window.onresize=function(a){e()}},parseData:function(){calendarHeatmap.data&&(calendarHeatmap.data[0].summary||calendarHeatmap.data.map(function(a){var t=a.details.reduce(function(a,t){return a[t.name]?a[t.name].value+=t.value:a[t.name]={value:t.value},a},{}),e=Object.keys(t).map(function(a){return{name:a,value:t[a].value}});return a.summary=e.sort(function(a,t){return t.value-a.value}),a}))},drawChart:function(){"year"===calendarHeatmap.overview?calendarHeatmap.drawYearOverview():"month"===calendarHeatmap.overview?calendarHeatmap.drawMonthOverview():"week"===calendarHeatmap.overview?calendarHeatmap.drawWeekOverview():"day"===calendarHeatmap.overview&&calendarHeatmap.drawDayOverview()},drawYearOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview);var a=moment().startOf("day").subtract(1,"year"),t=d3.max(calendarHeatmap.data,function(a){return a.total}),e=d3.scale.linear().range(["#ffffff",calendarHeatmap.color||"#ff4500"]).domain([-.15*t,t]),n=function(t){var e=moment(t.date),n=Math.round((e-moment(a).startOf("week"))/864e5),r=Math.trunc(n/7);return r*(calendarHeatmap.settings.item_size+calendarHeatmap.settings.gutter)+calendarHeatmap.settings.label_padding},r=function(a){return calendarHeatmap.settings.label_padding+moment(a.date).weekday()*(calendarHeatmap.settings.item_size+calendarHeatmap.settings.gutter)},i=function(a){return t<=0?calendarHeatmap.settings.item_size:.75*calendarHeatmap.settings.item_size+calendarHeatmap.settings.item_size*a.total/t*.25};calendarHeatmap.items.selectAll(".item-circle").remove(),calendarHeatmap.items.selectAll(".item-circle").data(calendarHeatmap.data).enter().append("rect").attr("class","item item-circle").style("opacity",0).attr("x",function(a){return n(a)+(calendarHeatmap.settings.item_size-i(a))/2}).attr("y",function(a){return r(a)+(calendarHeatmap.settings.item_size-i(a))/2}).attr("rx",function(a){return i(a)}).attr("ry",function(a){return i(a)}).attr("width",function(a){return i(a)}).attr("height",function(a){return i(a)}).attr("fill",function(a){return a.total>0?e(a.total):"transparent"}).on("click",function(a){calendarHeatmap.in_transition||0!==a.total&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeYearOverview(),calendarHeatmap.overview="day",calendarHeatmap.drawChart())}).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var t=d3.select(this);!function s(){t=t.transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").attr("x",function(a){return n(a)-(1.1*calendarHeatmap.settings.item_size-calendarHeatmap.settings.item_size)/2}).attr("y",function(a){return r(a)-(1.1*calendarHeatmap.settings.item_size-calendarHeatmap.settings.item_size)/2}).attr("width",1.1*calendarHeatmap.settings.item_size).attr("height",1.1*calendarHeatmap.settings.item_size).transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").attr("x",function(a){return n(a)+(calendarHeatmap.settings.item_size-i(a))/2}).attr("y",function(a){return r(a)+(calendarHeatmap.settings.item_size-i(a))/2}).attr("width",function(a){return i(a)}).attr("height",function(a){return i(a)}).each("end",s)}();var e="";e+='<div class="header"><strong>'+(a.total?calendarHeatmap.formatTime(a.total):"No time")+" tracked</strong></div>",e+="<div>on "+moment(a.date).format("dddd, MMM Do YYYY")+"</div><br>";for(var l=0;l<a.summary.length;l++)e+="<div><span><strong>"+a.summary[l].name+"</strong></span>",e+="<span>"+calendarHeatmap.formatTime(a.summary[l].value)+"</span></div>";var d=n(a)+calendarHeatmap.settings.item_size;calendarHeatmap.settings.width-d<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding&&(d-=calendarHeatmap.settings.tooltip_width+2*calendarHeatmap.settings.tooltip_padding);var o=r(a)+calendarHeatmap.settings.item_size;calendarHeatmap.tooltip.html(e).style("left",d+"px").style("top",o+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||(d3.select(this).transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").attr("x",function(a){return n(a)+(calendarHeatmap.settings.item_size-i(a))/2}).attr("y",function(a){return r(a)+(calendarHeatmap.settings.item_size-i(a))/2}).attr("width",function(a){return i(a)}).attr("height",function(a){return i(a)}),calendarHeatmap.hideTooltip())}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",1).call(function(a,t){a.empty()&&t();var e=0;a.each(function(){++e}).each("end",function(){--e||t.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1});var l=moment().endOf("day"),d=moment().startOf("day").subtract(1,"year"),o=d3.time.months(d.startOf("month"),l),s=d3.scale.linear().range([0,calendarHeatmap.settings.width]).domain([0,o.length]);calendarHeatmap.labels.selectAll(".label-month").remove(),calendarHeatmap.labels.selectAll(".label-month").data(o).enter().append("text").attr("class","label label-month").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return a.toLocaleDateString("en-us",{month:"short"})}).attr("x",function(a,t){return s(t)+(s(t)-s(t-1))/2}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var t=moment(a);calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).isSame(t,"month")?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}).on("click",function(a){if(!calendarHeatmap.in_transition){var t=calendarHeatmap.data.filter(function(t){return moment(a).startOf("month")<=moment(t.date)&&moment(t.date)<moment(a).endOf("month")});t.length&&(calendarHeatmap.selected={date:a},calendarHeatmap.in_transition=!0,calendarHeatmap.hideTooltip(),calendarHeatmap.removeYearOverview(),calendarHeatmap.overview="month",calendarHeatmap.drawChart())}});var c=d3.time.days(moment().startOf("week"),moment().endOf("week")),m=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(c.map(function(a){return moment(a).weekday()}));calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-day").data(c).enter().append("text").attr("class","label label-day").attr("x",calendarHeatmap.settings.label_padding/3).attr("y",function(a,t){return m(t)+m.rangeBand()/1.75}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("dddd")[0]}).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var t=moment(a);calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).day()===t.day()?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)})},drawMonthOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview);for(var a=moment(calendarHeatmap.selected.date).startOf("month"),t=moment(calendarHeatmap.selected.date).endOf("month"),e=calendarHeatmap.data.filter(function(e){return a<=moment(e.date)&&moment(e.date)<t}),n=d3.max(e,function(a){return d3.max(a.summary,function(a){return a.value})}),r=d3.time.days(moment().startOf("week"),moment().endOf("week")),i=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(r.map(function(a){return moment(a).weekday()})),l=[a.clone()];a.week()!==t.week();)l.push(a.add(1,"week").clone());var d=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width],.05).domain(l.map(function(a){return a.week()}));calendarHeatmap.items.selectAll(".item-block-month").remove();var o=calendarHeatmap.items.selectAll(".item-block-month").data(e).enter().append("g").attr("class","item item-block-month").attr("width",function(){return(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("transform",function(a){return"translate("+d(moment(a.date).week())+","+(i(moment(a.date).weekday())+i.rangeBand()/1.75-15)+")"}).attr("total",function(a){return a.total}).attr("date",function(a){return a.date}).attr("offset",0).on("click",function(a){calendarHeatmap.in_transition||0!==a.total&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeMonthOverview(),calendarHeatmap.overview="day",calendarHeatmap.drawChart())}),s=(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter,c=d3.scale.linear().rangeRound([0,s]);o.selectAll(".item-block-rect").data(function(a){return a.summary}).enter().append("rect").attr("class","item item-block-rect").attr("x",function(a){var t=parseInt(d3.select(this.parentNode).attr("total")),e=parseInt(d3.select(this.parentNode).attr("offset"));return c.domain([0,t]),d3.select(this.parentNode).attr("offset",e+c(a.value)),e}).attr("width",function(a){var t=parseInt(d3.select(this.parentNode).attr("total"));return c.domain([0,t]),Math.max(c(a.value)-calendarHeatmap.settings.item_gutter,1)}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("fill",function(a){var t=d3.scale.linear().range(["#ffffff",calendarHeatmap.color||"#ff4500"]).domain([-.15*n,n]);return t(a.value)||"#ff4500"}).style("opacity",0).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var t=new Date(d3.select(this.parentNode).attr("date")),e="";e+='<div class="header"><strong>'+a.name+"</strong></div><br>",e+="<div><strong>"+(a.value?calendarHeatmap.formatTime(a.value):"No time")+" tracked</strong></div>",e+="<div>on "+moment(t).format("dddd, MMM Do YYYY")+"</div>";for(var n=d(moment(t).week())+calendarHeatmap.settings.tooltip_padding;calendarHeatmap.settings.width-n<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding;)n-=10;var r=i(moment(t).weekday())+2*calendarHeatmap.settings.tooltip_padding;calendarHeatmap.tooltip.html(e).style("left",n+"px").style("top",r+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.hideTooltip()}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",1).call(function(a,t){a.empty()&&t();var e=0;a.each(function(){++e}).each("end",function(){--e||t.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1}),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.labels.selectAll(".label-week").data(l).enter().append("text").attr("class","label label-week").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return"Week "+a.week()}).attr("x",function(a){return d(a.week())}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(t){return moment(t.date).week()===a.week()?1:.1})}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}).on("click",function(a){if(!calendarHeatmap.in_transition){var t=calendarHeatmap.data.filter(function(t){return a.startOf("week")<=moment(t.date)&&moment(t.date)<a.endOf("week")});t.length&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected={date:a},calendarHeatmap.hideTooltip(),calendarHeatmap.removeMonthOverview(),calendarHeatmap.overview="week",calendarHeatmap.drawChart())}}),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-day").data(r).enter().append("text").attr("class","label label-day").attr("x",calendarHeatmap.settings.label_padding/3).attr("y",function(a,t){return i(t)+i.rangeBand()/1.75}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("dddd")[0]}).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var t=moment(a);calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).day()===t.day()?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}),calendarHeatmap.drawButton()},drawWeekOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview);var a=moment(calendarHeatmap.selected.date).startOf("week"),t=moment(calendarHeatmap.selected.date).endOf("week"),e=calendarHeatmap.data.filter(function(e){return a<=moment(e.date)&&moment(e.date)<t}),n=d3.max(e,function(a){return d3.max(a.summary,function(a){return a.value})}),r=d3.time.days(moment().startOf("week"),moment().endOf("week")),i=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(r.map(function(a){return moment(a).weekday()})),l=[a],d=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width],.01).domain(l.map(function(a){return a.week()}));calendarHeatmap.items.selectAll(".item-block-week").remove();var o=calendarHeatmap.items.selectAll(".item-block-week").data(e).enter().append("g").attr("class","item item-block-week").attr("width",function(){return(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("transform",function(a){return"translate("+d(moment(a.date).week())+","+(i(moment(a.date).weekday())+i.rangeBand()/1.75-15)+")"}).attr("total",function(a){return a.total}).attr("date",function(a){return a.date}).attr("offset",0).on("click",function(a){calendarHeatmap.in_transition||0!==a.total&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeWeekOverview(),calendarHeatmap.overview="day",calendarHeatmap.drawChart())}),s=(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter,c=d3.scale.linear().rangeRound([0,s]);o.selectAll(".item-block-rect").data(function(a){return a.summary}).enter().append("rect").attr("class","item item-block-rect").attr("x",function(a){var t=parseInt(d3.select(this.parentNode).attr("total")),e=parseInt(d3.select(this.parentNode).attr("offset"));return c.domain([0,t]),d3.select(this.parentNode).attr("offset",e+c(a.value)),e}).attr("width",function(a){var t=parseInt(d3.select(this.parentNode).attr("total"));return c.domain([0,t]),Math.max(c(a.value)-calendarHeatmap.settings.item_gutter,1)}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("fill",function(a){var t=d3.scale.linear().range(["#ffffff",calendarHeatmap.color||"#ff4500"]).domain([-.15*n,n]);return t(a.value)||"#ff4500"}).style("opacity",0).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var t=new Date(d3.select(this.parentNode).attr("date")),e="";e+='<div class="header"><strong>'+a.name+"</strong></div><br>",e+="<div><strong>"+(a.value?calendarHeatmap.formatTime(a.value):"No time")+" tracked</strong></div>",e+="<div>on "+moment(t).format("dddd, MMM Do YYYY")+"</div>";var n=parseInt(d3.select(this.parentNode).attr("total"));c.domain([0,n]);for(var r=parseInt(d3.select(this).attr("x"))+c(a.value)/4+calendarHeatmap.settings.tooltip_width/4;calendarHeatmap.settings.width-r<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding;)r-=10;var l=i(moment(t).weekday())+1.5*calendarHeatmap.settings.tooltip_padding;calendarHeatmap.tooltip.html(e).style("left",r+"px").style("top",l+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.hideTooltip()}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",1).call(function(a,t){a.empty()&&t();var e=0;a.each(function(){++e}).each("end",function(){--e||t.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1}),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.labels.selectAll(".label-week").data(l).enter().append("text").attr("class","label label-week").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return"Week "+a.week()}).attr("x",function(a){return d(a.week())}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(t){return moment(t.date).week()===a.week()?1:.1})}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-day").data(r).enter().append("text").attr("class","label label-day").attr("x",calendarHeatmap.settings.label_padding/3).attr("y",function(a,t){return i(t)+i.rangeBand()/1.75}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("dddd")[0]}).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var t=moment(a);calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).day()===t.day()?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}),calendarHeatmap.drawButton()},drawDayOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview),Object.keys(calendarHeatmap.selected).length||(calendarHeatmap.selected=calendarHeatmap.data[calendarHeatmap.data.length-1]);var a=calendarHeatmap.selected.summary.map(function(a){return a.name}),t=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(a),e=d3.time.scale().range([2*calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width]).domain([moment(calendarHeatmap.selected.date).startOf("day"),moment(calendarHeatmap.selected.date).endOf("day")]);calendarHeatmap.items.selectAll(".item-block").remove(),calendarHeatmap.items.selectAll(".item-block").data(calendarHeatmap.selected.details).enter().append("rect").attr("class","item item-block").attr("x",function(a){return e(moment(a.date))}).attr("y",function(a){return t(a.name)+t.rangeBand()/2-15}).attr("width",function(a){var t=e(d3.time.second.offset(moment(a.date),a.value));return Math.max(t-e(moment(a.date)),1)}).attr("height",function(){return Math.min(t.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("fill",function(){return calendarHeatmap.color||"#ff4500"}).style("opacity",0).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var n="";n+='<div class="header"><strong>'+a.name+"</strong><div><br>",n+="<div><strong>"+(a.value?calendarHeatmap.formatTime(a.value):"No time")+" tracked</strong></div>",n+="<div>on "+moment(a.date).format("dddd, MMM Do YYYY HH:mm")+"</div>";for(var r=100*a.value/86400+e(moment(a.date));calendarHeatmap.settings.width-r<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding;)r-=10;var i=t(a.name)+t.rangeBand()/2+calendarHeatmap.settings.tooltip_padding/2;calendarHeatmap.tooltip.html(n).style("left",r+"px").style("top",i+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.hideTooltip()}).on("click",function(a){calendarHeatmap.handler&&"function"==typeof calendarHeatmap.handler&&calendarHeatmap.handler(a)}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",.5).call(function(a,t){a.empty()&&t();var e=0;a.each(function(){++e}).each("end",function(){--e||t.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1});var n=d3.time.hours(moment(calendarHeatmap.selected.date).startOf("day"),moment(calendarHeatmap.selected.date).endOf("day")),r=d3.time.scale().range([2*calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width]).domain([0,n.length]);calendarHeatmap.labels.selectAll(".label-time").remove(),calendarHeatmap.labels.selectAll(".label-time").data(n).enter().append("text").attr("class","label label-time").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("HH:mm")}).attr("x",function(a,t){return r(t)}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var t=e(moment(a));calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){var n=e(moment(a.date)),r=e(moment(a.date).add(a.value,"seconds"));return t>=n&&t<=r?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",.5)}),calendarHeatmap.labels.selectAll(".label-project").remove(),calendarHeatmap.labels.selectAll(".label-project").data(a).enter().append("text").attr("class","label label-project").attr("x",calendarHeatmap.settings.gutter).attr("y",function(a){return t(a)+t.rangeBand()/2}).attr("min-height",function(){return t.rangeBand()}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return a}).each(function(){for(var a=d3.select(this),t=a.node().getComputedTextLength(),e=a.text();t>1.5*calendarHeatmap.settings.label_padding&&e.length>0;)e=e.slice(0,-1),a.text(e+"..."),t=a.node().getComputedTextLength()}).on("mouseenter",function(a){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(t){return t.name===a?1:.1})}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",.5)}),calendarHeatmap.drawButton()},drawButton:function(){calendarHeatmap.buttons.selectAll(".button").remove();var a=calendarHeatmap.buttons.append("g").attr("class","button button-back").style("opacity",0).on("click",function(){calendarHeatmap.in_transition||(calendarHeatmap.in_transition=!0,"month"===calendarHeatmap.overview?calendarHeatmap.removeMonthOverview():"week"===calendarHeatmap.overview?calendarHeatmap.removeWeekOverview():"day"===calendarHeatmap.overview&&calendarHeatmap.removeDayOverview(),calendarHeatmap.history.pop(),calendarHeatmap.overview=calendarHeatmap.history.pop(),calendarHeatmap.drawChart())});a.append("circle").attr("cx",calendarHeatmap.settings.label_padding/2.25).attr("cy",calendarHeatmap.settings.label_padding/2.5).attr("r",calendarHeatmap.settings.item_size/2),a.append("text").attr("x",calendarHeatmap.settings.label_padding/2.25).attr("y",calendarHeatmap.settings.label_padding/2.75).attr("dy",function(){return Math.floor(calendarHeatmap.settings.width/100)/2.5}).attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).html("←"),a.transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)},removeYearOverview:function(){calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease").style("opacity",0).remove(),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-month").remove()},removeMonthOverview:function(){calendarHeatmap.items.selectAll(".item-block-month").selectAll(".item-block-rect").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",0).attr("x",function(a,t){return t%2===0?-calendarHeatmap.settings.width/3:calendarHeatmap.settings.width/3}).remove(),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.hideBackButton()},removeWeekOverview:function(){calendarHeatmap.items.selectAll(".item-block-week").selectAll(".item-block-rect").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",0).attr("x",function(a,t){return t%2===0?-calendarHeatmap.settings.width/3:calendarHeatmap.settings.width/3}).remove(),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.hideBackButton()},removeDayOverview:function(){calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",0).attr("x",function(a,t){return t%2===0?-calendarHeatmap.settings.width/3:calendarHeatmap.settings.width/3}).remove(),calendarHeatmap.labels.selectAll(".label-time").remove(),calendarHeatmap.labels.selectAll(".label-project").remove(),calendarHeatmap.hideBackButton()},hideTooltip:function(){calendarHeatmap.tooltip.transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",0)},hideBackButton:function(){calendarHeatmap.buttons.selectAll(".button").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease").style("opacity",0).remove()},formatTime:function(a){var t=parseInt(a,10),e=Math.floor(t/3600),n=Math.floor((t-3600*e)/60),r="";return e>0&&(r+=1===e?"1 hour ":e+" hours "),n>0&&(r+=1===n?"1 minute":n+" minutes"),0===e&&0===n&&(r=a+" seconds"),r}}; | ||
"use strict";var calendarHeatmap={settings:{gutter:5,item_gutter:1,width:1e3,height:200,item_size:10,label_padding:40,max_block_height:20,transition_duration:500,tooltip_width:250,tooltip_padding:15},init:function(a,e,t,n){calendarHeatmap.data=a,calendarHeatmap.color=e||"#ff4500",calendarHeatmap.overview=t||"global",calendarHeatmap.history=["global"],calendarHeatmap.selected={},calendarHeatmap.handler=n,calendarHeatmap.in_transition=!1,calendarHeatmap.createElements(),calendarHeatmap.parseData(),calendarHeatmap.drawChart()},createElements:function(){var a=document.createElement("div");a.className="calendar-heatmap",document.body.appendChild(a);var e=d3.select(a).append("svg").attr("class","svg");calendarHeatmap.items=e.append("g"),calendarHeatmap.labels=e.append("g"),calendarHeatmap.buttons=e.append("g"),calendarHeatmap.tooltip=d3.select(a).append("div").attr("class","heatmap-tooltip").style("opacity",0);var t=function(){var t=Math.round((moment()-moment().subtract(1,"year").startOf("week"))/864e5),n=Math.trunc(t/7),r=n+1;calendarHeatmap.settings.width=a.offsetWidth<1e3?1e3:a.offsetWidth,calendarHeatmap.settings.item_size=(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/r-calendarHeatmap.settings.gutter,calendarHeatmap.settings.height=calendarHeatmap.settings.label_padding+7*(calendarHeatmap.settings.item_size+calendarHeatmap.settings.gutter),e.attr({width:calendarHeatmap.settings.width,height:calendarHeatmap.settings.height}),calendarHeatmap.data&&calendarHeatmap.data[0].summary&&calendarHeatmap.drawChart()};t(),window.onresize=function(a){t()}},parseData:function(){calendarHeatmap.data&&(calendarHeatmap.data[0].summary||calendarHeatmap.data.map(function(a){var e=a.details.reduce(function(a,e){return a[e.name]?a[e.name].value+=e.value:a[e.name]={value:e.value},a},{}),t=Object.keys(e).map(function(a){return{name:a,value:e[a].value}});return a.summary=t.sort(function(a,e){return e.value-a.value}),a}))},drawChart:function(){"global"===calendarHeatmap.overview?calendarHeatmap.drawGlobalOverview():"year"===calendarHeatmap.overview?calendarHeatmap.drawYearOverview():"month"===calendarHeatmap.overview?calendarHeatmap.drawMonthOverview():"week"===calendarHeatmap.overview?calendarHeatmap.drawWeekOverview():"day"===calendarHeatmap.overview&&calendarHeatmap.drawDayOverview()},drawGlobalOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview);var a=moment(calendarHeatmap.data[0].date).startOf("year"),e=moment(calendarHeatmap.data[calendarHeatmap.data.length-1].date).endOf("year"),t=d3.time.years(a,e).map(function(a){var e=moment(a);return{date:e,total:calendarHeatmap.data.reduce(function(a,t){return moment(t.date).year()===e.year()&&(a+=t.total),a},0),summary:function(){var a=calendarHeatmap.data.reduce(function(a,t){if(moment(t.date).year()===e.year())for(var n=0;n<t.summary.length;n++)a[t.summary[n].name]?a[t.summary[n].name].value+=t.summary[n].value:a[t.summary[n].name]={value:t.summary[n].value};return a},{}),t=Object.keys(a).map(function(e){return{name:e,value:a[e].value}});return t.sort(function(a,e){return e.value-a.value})}()}}),n=d3.max(t,function(a){return a.total}),r=d3.time.years(a,e).map(function(a){return moment(a)}),i=d3.scale.ordinal().rangeRoundBands([0,calendarHeatmap.settings.width],.05).domain(r.map(function(a){return a.year()}));calendarHeatmap.items.selectAll(".item-block-year").remove();calendarHeatmap.items.selectAll(".item-block-year").data(t).enter().append("rect").attr("class","item item-block-year").attr("width",function(){return(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/r.length-5*calendarHeatmap.settings.gutter}).attr("height",function(){return calendarHeatmap.settings.height-calendarHeatmap.settings.label_padding}).attr("transform",function(a){return"translate("+i(a.date.year())+","+2*calendarHeatmap.settings.tooltip_padding+")"}).attr("fill",function(a){var e=d3.scale.linear().range(["#ffffff",calendarHeatmap.color||"#ff4500"]).domain([-.15*n,n]);return e(a.total)||"#ff4500"}).on("click",function(a){calendarHeatmap.in_transition||(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeGlobalOverview(),calendarHeatmap.overview="year",calendarHeatmap.drawChart())}).style("opacity",0).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var e="";e+='<div class="header"><strong>'+(a.total?calendarHeatmap.formatTime(a.total):"No time")+" tracked</strong></div>",e+="<div>in "+a.date.year()+"</div><br>";for(var t=0;t<a.summary.length;t++)e+="<div><span><strong>"+a.summary[t].name+"</strong></span>",e+="<span>"+calendarHeatmap.formatTime(a.summary[t].value)+"</span></div>";for(var n=i(a.date.year())+calendarHeatmap.settings.tooltip_padding;calendarHeatmap.settings.width-n<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding;)n-=10;var r=3*calendarHeatmap.settings.tooltip_padding;calendarHeatmap.tooltip.html(e).style("left",n+"px").style("top",r+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.hideTooltip()}).transition().delay(function(a,e){return calendarHeatmap.settings.transition_duration*(e+1)/10}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",1).call(function(a,e){a.empty()&&e();var t=0;a.each(function(){++t}).each("end",function(){--t||e.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1});calendarHeatmap.labels.selectAll(".label-year").remove(),calendarHeatmap.labels.selectAll(".label-year").data(r).enter().append("text").attr("class","label label-year").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return a.year()}).attr("x",function(a){return i(a.year())}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-year").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(e){return moment(e.date).year()===a.year()?1:.1})}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-year").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}).on("click",function(a){calendarHeatmap.in_transition||(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeGlobalOverview(),calendarHeatmap.overview="year",calendarHeatmap.drawChart())})},drawYearOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview);var a=moment(calendarHeatmap.selected.date).startOf("year"),e=moment(calendarHeatmap.selected.date).endOf("year"),t=calendarHeatmap.data.filter(function(t){return a<=moment(t.date)&&moment(t.date)<e}),n=d3.max(t,function(a){return a.total}),r=d3.scale.linear().range(["#ffffff",calendarHeatmap.color||"#ff4500"]).domain([-.15*n,n]),i=function(e){var t=moment(e.date),n=Math.round((t-moment(a).startOf("week"))/864e5),r=Math.trunc(n/7);return r*(calendarHeatmap.settings.item_size+calendarHeatmap.settings.gutter)+calendarHeatmap.settings.label_padding},l=function(a){return calendarHeatmap.settings.label_padding+moment(a.date).weekday()*(calendarHeatmap.settings.item_size+calendarHeatmap.settings.gutter)},d=function(a){return n<=0?calendarHeatmap.settings.item_size:.75*calendarHeatmap.settings.item_size+calendarHeatmap.settings.item_size*a.total/n*.25};calendarHeatmap.items.selectAll(".item-circle").remove(),calendarHeatmap.items.selectAll(".item-circle").data(t).enter().append("rect").attr("class","item item-circle").style("opacity",0).attr("x",function(a){return i(a)+(calendarHeatmap.settings.item_size-d(a))/2}).attr("y",function(a){return l(a)+(calendarHeatmap.settings.item_size-d(a))/2}).attr("rx",function(a){return d(a)}).attr("ry",function(a){return d(a)}).attr("width",function(a){return d(a)}).attr("height",function(a){return d(a)}).attr("fill",function(a){return a.total>0?r(a.total):"transparent"}).on("click",function(a){calendarHeatmap.in_transition||0!==a.total&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeYearOverview(),calendarHeatmap.overview="day",calendarHeatmap.drawChart())}).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var e=d3.select(this);!function s(){e=e.transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").attr("x",function(a){return i(a)-(1.1*calendarHeatmap.settings.item_size-calendarHeatmap.settings.item_size)/2}).attr("y",function(a){return l(a)-(1.1*calendarHeatmap.settings.item_size-calendarHeatmap.settings.item_size)/2}).attr("width",1.1*calendarHeatmap.settings.item_size).attr("height",1.1*calendarHeatmap.settings.item_size).transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").attr("x",function(a){return i(a)+(calendarHeatmap.settings.item_size-d(a))/2}).attr("y",function(a){return l(a)+(calendarHeatmap.settings.item_size-d(a))/2}).attr("width",function(a){return d(a)}).attr("height",function(a){return d(a)}).each("end",s)}();var t="";t+='<div class="header"><strong>'+(a.total?calendarHeatmap.formatTime(a.total):"No time")+" tracked</strong></div>",t+="<div>on "+moment(a.date).format("dddd, MMM Do YYYY")+"</div><br>";for(var n=0;n<a.summary.length;n++)t+="<div><span><strong>"+a.summary[n].name+"</strong></span>",t+="<span>"+calendarHeatmap.formatTime(a.summary[n].value)+"</span></div>";var r=i(a)+calendarHeatmap.settings.item_size;calendarHeatmap.settings.width-r<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding&&(r-=calendarHeatmap.settings.tooltip_width+2*calendarHeatmap.settings.tooltip_padding);var o=l(a)+calendarHeatmap.settings.item_size;calendarHeatmap.tooltip.html(t).style("left",r+"px").style("top",o+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||(d3.select(this).transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").attr("x",function(a){return i(a)+(calendarHeatmap.settings.item_size-d(a))/2}).attr("y",function(a){return l(a)+(calendarHeatmap.settings.item_size-d(a))/2}).attr("width",function(a){return d(a)}).attr("height",function(a){return d(a)}),calendarHeatmap.hideTooltip())}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",1).call(function(a,e){a.empty()&&e();var t=0;a.each(function(){++t}).each("end",function(){--t||e.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1});var o=d3.time.months(a,e),s=d3.scale.linear().range([0,calendarHeatmap.settings.width]).domain([0,o.length]);calendarHeatmap.labels.selectAll(".label-month").remove(),calendarHeatmap.labels.selectAll(".label-month").data(o).enter().append("text").attr("class","label label-month").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return a.toLocaleDateString("en-us",{month:"short"})}).attr("x",function(a,e){return s(e)+(s(e)-s(e-1))/2}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var e=moment(a);calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).isSame(e,"month")?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}).on("click",function(a){if(!calendarHeatmap.in_transition){var e=calendarHeatmap.data.filter(function(e){return moment(a).startOf("month")<=moment(e.date)&&moment(e.date)<moment(a).endOf("month")});e.length&&(calendarHeatmap.selected={date:a},calendarHeatmap.in_transition=!0,calendarHeatmap.hideTooltip(),calendarHeatmap.removeYearOverview(),calendarHeatmap.overview="month",calendarHeatmap.drawChart())}});var c=d3.time.days(moment().startOf("week"),moment().endOf("week")),m=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(c.map(function(a){return moment(a).weekday()}));calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-day").data(c).enter().append("text").attr("class","label label-day").attr("x",calendarHeatmap.settings.label_padding/3).attr("y",function(a,e){return m(e)+m.rangeBand()/1.75}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("dddd")[0]}).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var e=moment(a);calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).day()===e.day()?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}),calendarHeatmap.drawButton()},drawMonthOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview);for(var a=moment(calendarHeatmap.selected.date).startOf("month"),e=moment(calendarHeatmap.selected.date).endOf("month"),t=calendarHeatmap.data.filter(function(t){return a<=moment(t.date)&&moment(t.date)<e}),n=d3.max(t,function(a){return d3.max(a.summary,function(a){return a.value})}),r=d3.time.days(moment().startOf("week"),moment().endOf("week")),i=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(r.map(function(a){return moment(a).weekday()})),l=[a.clone()];a.week()!==e.week();)l.push(a.add(1,"week").clone());var d=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width],.05).domain(l.map(function(a){return a.week()}));calendarHeatmap.items.selectAll(".item-block-month").remove();var o=calendarHeatmap.items.selectAll(".item-block-month").data(t).enter().append("g").attr("class","item item-block-month").attr("width",function(){return(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("transform",function(a){return"translate("+d(moment(a.date).week())+","+(i(moment(a.date).weekday())+i.rangeBand()/1.75-15)+")"}).attr("total",function(a){return a.total}).attr("date",function(a){return a.date}).attr("offset",0).on("click",function(a){calendarHeatmap.in_transition||0!==a.total&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeMonthOverview(),calendarHeatmap.overview="day",calendarHeatmap.drawChart())}),s=(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter,c=d3.scale.linear().rangeRound([0,s]);o.selectAll(".item-block-rect").data(function(a){return a.summary}).enter().append("rect").attr("class","item item-block-rect").attr("x",function(a){var e=parseInt(d3.select(this.parentNode).attr("total")),t=parseInt(d3.select(this.parentNode).attr("offset"));return c.domain([0,e]),d3.select(this.parentNode).attr("offset",t+c(a.value)),t}).attr("width",function(a){var e=parseInt(d3.select(this.parentNode).attr("total"));return c.domain([0,e]),Math.max(c(a.value)-calendarHeatmap.settings.item_gutter,1)}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("fill",function(a){var e=d3.scale.linear().range(["#ffffff",calendarHeatmap.color||"#ff4500"]).domain([-.15*n,n]);return e(a.value)||"#ff4500"}).style("opacity",0).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var e=new Date(d3.select(this.parentNode).attr("date")),t="";t+='<div class="header"><strong>'+a.name+"</strong></div><br>",t+="<div><strong>"+(a.value?calendarHeatmap.formatTime(a.value):"No time")+" tracked</strong></div>",t+="<div>on "+moment(e).format("dddd, MMM Do YYYY")+"</div>";for(var n=d(moment(e).week())+calendarHeatmap.settings.tooltip_padding;calendarHeatmap.settings.width-n<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding;)n-=10;var r=i(moment(e).weekday())+2*calendarHeatmap.settings.tooltip_padding;calendarHeatmap.tooltip.html(t).style("left",n+"px").style("top",r+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.hideTooltip()}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",1).call(function(a,e){a.empty()&&e();var t=0;a.each(function(){++t}).each("end",function(){--t||e.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1}),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.labels.selectAll(".label-week").data(l).enter().append("text").attr("class","label label-week").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return"Week "+a.week()}).attr("x",function(a){return d(a.week())}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(e){return moment(e.date).week()===a.week()?1:.1})}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}).on("click",function(a){if(!calendarHeatmap.in_transition){var e=calendarHeatmap.data.filter(function(e){return a.startOf("week")<=moment(e.date)&&moment(e.date)<a.endOf("week")});e.length&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected={date:a},calendarHeatmap.hideTooltip(),calendarHeatmap.removeMonthOverview(),calendarHeatmap.overview="week",calendarHeatmap.drawChart())}}),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-day").data(r).enter().append("text").attr("class","label label-day").attr("x",calendarHeatmap.settings.label_padding/3).attr("y",function(a,e){return i(e)+i.rangeBand()/1.75}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("dddd")[0]}).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var e=moment(a);calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).day()===e.day()?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-month").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}),calendarHeatmap.drawButton()},drawWeekOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview);var a=moment(calendarHeatmap.selected.date).startOf("week"),e=moment(calendarHeatmap.selected.date).endOf("week"),t=calendarHeatmap.data.filter(function(t){return a<=moment(t.date)&&moment(t.date)<e}),n=d3.max(t,function(a){return d3.max(a.summary,function(a){return a.value})}),r=d3.time.days(moment().startOf("week"),moment().endOf("week")),i=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(r.map(function(a){return moment(a).weekday()})),l=[a],d=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width],.01).domain(l.map(function(a){return a.week()}));calendarHeatmap.items.selectAll(".item-block-week").remove();var o=calendarHeatmap.items.selectAll(".item-block-week").data(t).enter().append("g").attr("class","item item-block-week").attr("width",function(){return(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("transform",function(a){return"translate("+d(moment(a.date).week())+","+(i(moment(a.date).weekday())+i.rangeBand()/1.75-15)+")"}).attr("total",function(a){return a.total}).attr("date",function(a){return a.date}).attr("offset",0).on("click",function(a){calendarHeatmap.in_transition||0!==a.total&&(calendarHeatmap.in_transition=!0,calendarHeatmap.selected=a,calendarHeatmap.hideTooltip(),calendarHeatmap.removeWeekOverview(),calendarHeatmap.overview="day",calendarHeatmap.drawChart())}),s=(calendarHeatmap.settings.width-calendarHeatmap.settings.label_padding)/l.length-5*calendarHeatmap.settings.gutter,c=d3.scale.linear().rangeRound([0,s]);o.selectAll(".item-block-rect").data(function(a){return a.summary}).enter().append("rect").attr("class","item item-block-rect").attr("x",function(a){var e=parseInt(d3.select(this.parentNode).attr("total")),t=parseInt(d3.select(this.parentNode).attr("offset"));return c.domain([0,e]),d3.select(this.parentNode).attr("offset",t+c(a.value)),t}).attr("width",function(a){var e=parseInt(d3.select(this.parentNode).attr("total"));return c.domain([0,e]),Math.max(c(a.value)-calendarHeatmap.settings.item_gutter,1)}).attr("height",function(){return Math.min(i.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("fill",function(a){var e=d3.scale.linear().range(["#ffffff",calendarHeatmap.color||"#ff4500"]).domain([-.15*n,n]);return e(a.value)||"#ff4500"}).style("opacity",0).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var e=new Date(d3.select(this.parentNode).attr("date")),t="";t+='<div class="header"><strong>'+a.name+"</strong></div><br>",t+="<div><strong>"+(a.value?calendarHeatmap.formatTime(a.value):"No time")+" tracked</strong></div>",t+="<div>on "+moment(e).format("dddd, MMM Do YYYY")+"</div>";var n=parseInt(d3.select(this.parentNode).attr("total"));c.domain([0,n]);for(var r=parseInt(d3.select(this).attr("x"))+c(a.value)/4+calendarHeatmap.settings.tooltip_width/4;calendarHeatmap.settings.width-r<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding;)r-=10;var l=i(moment(e).weekday())+1.5*calendarHeatmap.settings.tooltip_padding;calendarHeatmap.tooltip.html(t).style("left",r+"px").style("top",l+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.hideTooltip()}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",1).call(function(a,e){a.empty()&&e();var t=0;a.each(function(){++t}).each("end",function(){--t||e.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1}),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.labels.selectAll(".label-week").data(l).enter().append("text").attr("class","label label-week").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return"Week "+a.week()}).attr("x",function(a){return d(a.week())}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(e){return moment(e.date).week()===a.week()?1:.1})}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-day").data(r).enter().append("text").attr("class","label label-day").attr("x",calendarHeatmap.settings.label_padding/3).attr("y",function(a,e){return i(e)+i.rangeBand()/1.75}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("dddd")[0]}).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var e=moment(a);calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){return moment(a.date).day()===e.day()?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block-week").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)}),calendarHeatmap.drawButton()},drawDayOverview:function(){calendarHeatmap.history[calendarHeatmap.history.length-1]!==calendarHeatmap.overview&&calendarHeatmap.history.push(calendarHeatmap.overview),Object.keys(calendarHeatmap.selected).length||(calendarHeatmap.selected=calendarHeatmap.data[calendarHeatmap.data.length-1]);var a=calendarHeatmap.selected.summary.map(function(a){return a.name}),e=d3.scale.ordinal().rangeRoundBands([calendarHeatmap.settings.label_padding,calendarHeatmap.settings.height]).domain(a),t=d3.time.scale().range([2*calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width]).domain([moment(calendarHeatmap.selected.date).startOf("day"),moment(calendarHeatmap.selected.date).endOf("day")]);calendarHeatmap.items.selectAll(".item-block").remove(),calendarHeatmap.items.selectAll(".item-block").data(calendarHeatmap.selected.details).enter().append("rect").attr("class","item item-block").attr("x",function(a){return t(moment(a.date))}).attr("y",function(a){return e(a.name)+e.rangeBand()/2-15}).attr("width",function(a){var e=t(d3.time.second.offset(moment(a.date),a.value));return Math.max(e-t(moment(a.date)),1)}).attr("height",function(){return Math.min(e.rangeBand(),calendarHeatmap.settings.max_block_height)}).attr("fill",function(){return calendarHeatmap.color||"#ff4500"}).style("opacity",0).on("mouseover",function(a){if(!calendarHeatmap.in_transition){var n="";n+='<div class="header"><strong>'+a.name+"</strong><div><br>",n+="<div><strong>"+(a.value?calendarHeatmap.formatTime(a.value):"No time")+" tracked</strong></div>",n+="<div>on "+moment(a.date).format("dddd, MMM Do YYYY HH:mm")+"</div>";for(var r=100*a.value/86400+t(moment(a.date));calendarHeatmap.settings.width-r<calendarHeatmap.settings.tooltip_width+3*calendarHeatmap.settings.tooltip_padding;)r-=10;var i=e(a.name)+e.rangeBand()/2+calendarHeatmap.settings.tooltip_padding/2;calendarHeatmap.tooltip.html(n).style("left",r+"px").style("top",i+"px").transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",1)}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.hideTooltip()}).on("click",function(a){calendarHeatmap.handler&&"function"==typeof calendarHeatmap.handler&&calendarHeatmap.handler(a)}).transition().delay(function(){return(Math.cos(Math.PI*Math.random())+1)*calendarHeatmap.settings.transition_duration}).duration(function(){return calendarHeatmap.settings.transition_duration}).ease("ease-in").style("opacity",.5).call(function(a,e){a.empty()&&e();var t=0;a.each(function(){++t}).each("end",function(){--t||e.apply(this,arguments)})},function(){calendarHeatmap.in_transition=!1});var n=d3.time.hours(moment(calendarHeatmap.selected.date).startOf("day"),moment(calendarHeatmap.selected.date).endOf("day")),r=d3.time.scale().range([2*calendarHeatmap.settings.label_padding,calendarHeatmap.settings.width]).domain([0,n.length]);calendarHeatmap.labels.selectAll(".label-time").remove(),calendarHeatmap.labels.selectAll(".label-time").data(n).enter().append("text").attr("class","label label-time").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return moment(a).format("HH:mm")}).attr("x",function(a,e){return r(e)}).attr("y",calendarHeatmap.settings.label_padding/2).on("mouseenter",function(a){if(!calendarHeatmap.in_transition){var e=t(moment(a));calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(a){var n=t(moment(a.date)),r=t(moment(a.date).add(a.value,"seconds"));return e>=n&&e<=r?1:.1})}}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",.5)}),calendarHeatmap.labels.selectAll(".label-project").remove(),calendarHeatmap.labels.selectAll(".label-project").data(a).enter().append("text").attr("class","label label-project").attr("x",calendarHeatmap.settings.gutter).attr("y",function(a){return e(a)+e.rangeBand()/2}).attr("min-height",function(){return e.rangeBand()}).style("text-anchor","left").attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).text(function(a){return a}).each(function(){for(var a=d3.select(this),e=a.node().getComputedTextLength(),t=a.text();e>1.5*calendarHeatmap.settings.label_padding&&t.length>0;)t=t.slice(0,-1),a.text(t+"..."),e=a.node().getComputedTextLength()}).on("mouseenter",function(a){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",function(e){return e.name===a?1:.1})}).on("mouseout",function(){calendarHeatmap.in_transition||calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",.5)}),calendarHeatmap.drawButton()},drawButton:function(){calendarHeatmap.buttons.selectAll(".button").remove();var a=calendarHeatmap.buttons.append("g").attr("class","button button-back").style("opacity",0).on("click",function(){calendarHeatmap.in_transition||(calendarHeatmap.in_transition=!0,"year"===calendarHeatmap.overview?calendarHeatmap.removeYearOverview():"month"===calendarHeatmap.overview?calendarHeatmap.removeMonthOverview():"week"===calendarHeatmap.overview?calendarHeatmap.removeWeekOverview():"day"===calendarHeatmap.overview&&calendarHeatmap.removeDayOverview(),calendarHeatmap.history.pop(),calendarHeatmap.overview=calendarHeatmap.history.pop(),calendarHeatmap.drawChart())});a.append("circle").attr("cx",calendarHeatmap.settings.label_padding/2.25).attr("cy",calendarHeatmap.settings.label_padding/2.5).attr("r",calendarHeatmap.settings.item_size/2),a.append("text").attr("x",calendarHeatmap.settings.label_padding/2.25).attr("y",calendarHeatmap.settings.label_padding/2.5).attr("dy",function(){ | ||
return Math.floor(calendarHeatmap.settings.width/100)/3}).attr("font-size",function(){return Math.floor(calendarHeatmap.settings.label_padding/3)+"px"}).html("←"),a.transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",1)},removeGlobalOverview:function(){calendarHeatmap.items.selectAll(".item-block-year").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-out").style("opacity",0).remove(),calendarHeatmap.labels.selectAll(".label-year").remove()},removeYearOverview:function(){calendarHeatmap.items.selectAll(".item-circle").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease").style("opacity",0).remove(),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-month").remove(),calendarHeatmap.hideBackButton()},removeMonthOverview:function(){calendarHeatmap.items.selectAll(".item-block-month").selectAll(".item-block-rect").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",0).attr("x",function(a,e){return e%2===0?-calendarHeatmap.settings.width/3:calendarHeatmap.settings.width/3}).remove(),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.hideBackButton()},removeWeekOverview:function(){calendarHeatmap.items.selectAll(".item-block-week").selectAll(".item-block-rect").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",0).attr("x",function(a,e){return e%2===0?-calendarHeatmap.settings.width/3:calendarHeatmap.settings.width/3}).remove(),calendarHeatmap.labels.selectAll(".label-day").remove(),calendarHeatmap.labels.selectAll(".label-week").remove(),calendarHeatmap.hideBackButton()},removeDayOverview:function(){calendarHeatmap.items.selectAll(".item-block").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease-in").style("opacity",0).attr("x",function(a,e){return e%2===0?-calendarHeatmap.settings.width/3:calendarHeatmap.settings.width/3}).remove(),calendarHeatmap.labels.selectAll(".label-time").remove(),calendarHeatmap.labels.selectAll(".label-project").remove(),calendarHeatmap.hideBackButton()},hideTooltip:function(){calendarHeatmap.tooltip.transition().duration(calendarHeatmap.settings.transition_duration/2).ease("ease-in").style("opacity",0)},hideBackButton:function(){calendarHeatmap.buttons.selectAll(".button").transition().duration(calendarHeatmap.settings.transition_duration).ease("ease").style("opacity",0).remove()},formatTime:function(a){var e=parseInt(a,10),t=Math.floor(e/3600),n=Math.floor((e-3600*t)/60),r="";return t>0&&(r+=1===t?"1 hour ":t+" hours "),n>0&&(r+=1===n?"1 minute":n+" minutes"),0===t&&0===n&&(r=a+" seconds"),r}}; |
{ | ||
"name": "calendar-heatmap-graph", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "d3.js calendar heatmap graph.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/g1eb/calendar-heatmap#readme", |
@@ -5,3 +5,3 @@ # Calendar heatmap graph | ||
Includes visualization of year, month, week and day overview. | ||
Includes a global overview of multiple years and visualizations of year, month, week and day overview with zoom for details-on-demand. | ||
@@ -16,2 +16,5 @@ Inspired by [Github's contribution graph](https://help.github.com/articles/viewing-contributions-on-your-profile/#contributions-calendar) | ||
### Global overview | ||
[![calendar heatmap - global overview](https://raw.githubusercontent.com/g1eb/calendar-heatmap/master/images/screenshot_global_overview.png)](https://rawgit.com/g1eb/calendar-heatmap/master/) | ||
### Year overview | ||
@@ -61,5 +64,4 @@ [![calendar heatmap - year overview](https://raw.githubusercontent.com/g1eb/calendar-heatmap/master/images/screenshot_year_overview.png)](https://rawgit.com/g1eb/calendar-heatmap/master/) | ||
Time series data for each day going 1 year back. | ||
Each day has a total time tracked (in seconds). | ||
Details, if provided, are shown in a tooltip as well as in daily overview. | ||
Time series data where each day has a total time tracked (in seconds). | ||
Details, if provided, are shown in a tooltip on mouseover in different overviews. | ||
@@ -91,3 +93,3 @@ ``` | ||
In some cases details array could be large and in order to fit the data into the tooltip a short summary is generated with distinct projects and their total tracked time for that date. | ||
In terms of optimization summary data can be computed server-side and passed in using the ``summary'' attribute. | ||
In terms of optimization, summary data can be computed server-side and passed in using the ``summary'' attribute. | ||
And in addition to the data structure described above this would result in a summary dictionary with distinct project names and total values of tracked time in seconds, e.g.: | ||
@@ -94,0 +96,0 @@ |
@@ -33,4 +33,4 @@ 'use strict'; | ||
// Initialize current overview type and history | ||
calendarHeatmap.overview = overview || 'year'; | ||
calendarHeatmap.history = ['year']; | ||
calendarHeatmap.overview = overview || 'global'; | ||
calendarHeatmap.history = ['global']; | ||
calendarHeatmap.selected = {}; | ||
@@ -140,3 +140,5 @@ | ||
drawChart: function () { | ||
if ( calendarHeatmap.overview === 'year' ) { | ||
if ( calendarHeatmap.overview === 'global' ) { | ||
calendarHeatmap.drawGlobalOverview(); | ||
} else if ( calendarHeatmap.overview === 'year' ) { | ||
calendarHeatmap.drawYearOverview(); | ||
@@ -154,2 +156,229 @@ } else if ( calendarHeatmap.overview === 'month' ) { | ||
/** | ||
* Draw global overview (multiple years) | ||
*/ | ||
drawGlobalOverview: function () { | ||
// Add current overview to the history | ||
if ( calendarHeatmap.history[calendarHeatmap.history.length-1] !== calendarHeatmap.overview ) { | ||
calendarHeatmap.history.push(calendarHeatmap.overview); | ||
} | ||
// Define start and end of the dataset | ||
var start = moment(calendarHeatmap.data[0].date).startOf('year'); | ||
var end = moment(calendarHeatmap.data[calendarHeatmap.data.length-1].date).endOf('year'); | ||
// Define array of years and total values | ||
var year_data = d3.time.years(start, end).map(function (d) { | ||
var date = moment(d); | ||
return { | ||
'date': date, | ||
'total': calendarHeatmap.data.reduce(function (prev, current) { | ||
if ( moment(current.date).year() === date.year() ) { | ||
prev += current.total; | ||
} | ||
return prev; | ||
}, 0), | ||
'summary': function () { | ||
var summary = calendarHeatmap.data.reduce(function (summary, d) { | ||
if ( moment(d.date).year() === date.year() ) { | ||
for ( var i = 0; i < d.summary.length; i++ ) { | ||
if ( !summary[d.summary[i].name] ) { | ||
summary[d.summary[i].name] = { | ||
'value': d.summary[i].value, | ||
}; | ||
} else { | ||
summary[d.summary[i].name].value += d.summary[i].value; | ||
} | ||
} | ||
} | ||
return summary; | ||
}, {}); | ||
var unsorted_summary = Object.keys(summary).map(function (key) { | ||
return { | ||
'name': key, | ||
'value': summary[key].value | ||
}; | ||
}); | ||
return unsorted_summary.sort(function (a, b) { | ||
return b.value - a.value; | ||
}); | ||
}(), | ||
}; | ||
}); | ||
// Calculate max value of all the years in the dataset | ||
var max_value = d3.max(year_data, function (d) { | ||
return d.total; | ||
}); | ||
// Define year labels and axis | ||
var year_labels = d3.time.years(start, end).map(function (d) { | ||
return moment(d); | ||
}); | ||
var yearScale = d3.scale.ordinal() | ||
.rangeRoundBands([0, calendarHeatmap.settings.width], 0.05) | ||
.domain(year_labels.map(function(d) { | ||
return d.year(); | ||
})); | ||
// Add month data items to the overview | ||
calendarHeatmap.items.selectAll('.item-block-year').remove(); | ||
var item_block = calendarHeatmap.items.selectAll('.item-block-year') | ||
.data(year_data) | ||
.enter() | ||
.append('rect') | ||
.attr('class', 'item item-block-year') | ||
.attr('width', function () { | ||
return (calendarHeatmap.settings.width - calendarHeatmap.settings.label_padding) / year_labels.length - calendarHeatmap.settings.gutter * 5; | ||
}) | ||
.attr('height', function () { | ||
return calendarHeatmap.settings.height - calendarHeatmap.settings.label_padding; | ||
}) | ||
.attr('transform', function (d) { | ||
return 'translate(' + yearScale(d.date.year()) + ',' + calendarHeatmap.settings.tooltip_padding * 2 + ')'; | ||
}) | ||
.attr('fill', function (d) { | ||
var color = d3.scale.linear() | ||
.range(['#ffffff', calendarHeatmap.color || '#ff4500']) | ||
.domain([-0.15 * max_value, max_value]); | ||
return color(d.total) || '#ff4500'; | ||
}) | ||
.on('click', function (d) { | ||
if ( calendarHeatmap.in_transition ) { return; } | ||
// Set in_transition flag | ||
calendarHeatmap.in_transition = true; | ||
// Set selected date to the one clicked on | ||
calendarHeatmap.selected = d; | ||
// Hide tooltip | ||
calendarHeatmap.hideTooltip(); | ||
// Remove all month overview related items and labels | ||
calendarHeatmap.removeGlobalOverview(); | ||
// Redraw the chart | ||
calendarHeatmap.overview = 'year'; | ||
calendarHeatmap.drawChart(); | ||
}) | ||
.style('opacity', 0) | ||
.on('mouseover', function(d) { | ||
if ( calendarHeatmap.in_transition ) { return; } | ||
// Construct tooltip | ||
var tooltip_html = ''; | ||
tooltip_html += '<div class="header"><strong>' + (d.total ? calendarHeatmap.formatTime(d.total) : 'No time') + ' tracked</strong></div>'; | ||
tooltip_html += '<div>in ' + d.date.year() + '</div><br>'; | ||
// Add summary to the tooltip | ||
for ( var i = 0; i < d.summary.length; i++ ) { | ||
tooltip_html += '<div><span><strong>' + d.summary[i].name + '</strong></span>'; | ||
tooltip_html += '<span>' + calendarHeatmap.formatTime(d.summary[i].value) + '</span></div>'; | ||
}; | ||
// Calculate tooltip position | ||
var x = yearScale(d.date.year()) + calendarHeatmap.settings.tooltip_padding; | ||
while ( calendarHeatmap.settings.width - x < (calendarHeatmap.settings.tooltip_width + calendarHeatmap.settings.tooltip_padding * 3) ) { | ||
x -= 10; | ||
} | ||
var y = calendarHeatmap.settings.tooltip_padding * 3; | ||
// Show tooltip | ||
calendarHeatmap.tooltip.html(tooltip_html) | ||
.style('left', x + 'px') | ||
.style('top', y + 'px') | ||
.transition() | ||
.duration(calendarHeatmap.settings.transition_duration / 2) | ||
.ease('ease-in') | ||
.style('opacity', 1); | ||
}) | ||
.on('mouseout', function () { | ||
if ( calendarHeatmap.in_transition ) { return; } | ||
calendarHeatmap.hideTooltip(); | ||
}) | ||
.transition() | ||
.delay(function (d, i) { | ||
return calendarHeatmap.settings.transition_duration * (i + 1) / 10; | ||
}) | ||
.duration(function () { | ||
return calendarHeatmap.settings.transition_duration; | ||
}) | ||
.ease('ease-in') | ||
.style('opacity', 1) | ||
.call(function (transition, callback) { | ||
if ( transition.empty() ) { | ||
callback(); | ||
} | ||
var n = 0; | ||
transition | ||
.each(function() { ++n; }) | ||
.each('end', function() { | ||
if ( !--n ) { | ||
callback.apply(this, arguments); | ||
} | ||
}); | ||
}, function() { | ||
calendarHeatmap.in_transition = false; | ||
}); | ||
// Add year labels | ||
calendarHeatmap.labels.selectAll('.label-year').remove(); | ||
calendarHeatmap.labels.selectAll('.label-year') | ||
.data(year_labels) | ||
.enter() | ||
.append('text') | ||
.attr('class', 'label label-year') | ||
.attr('font-size', function () { | ||
return Math.floor(calendarHeatmap.settings.label_padding / 3) + 'px'; | ||
}) | ||
.text(function (d) { | ||
return d.year(); | ||
}) | ||
.attr('x', function (d) { | ||
return yearScale(d.year()); | ||
}) | ||
.attr('y', calendarHeatmap.settings.label_padding / 2) | ||
.on('mouseenter', function (year_label) { | ||
if ( calendarHeatmap.in_transition ) { return; } | ||
calendarHeatmap.items.selectAll('.item-block-year') | ||
.transition() | ||
.duration(calendarHeatmap.settings.transition_duration) | ||
.ease('ease-in') | ||
.style('opacity', function (d) { | ||
return ( moment(d.date).year() === year_label.year() ) ? 1 : 0.1; | ||
}); | ||
}) | ||
.on('mouseout', function () { | ||
if ( calendarHeatmap.in_transition ) { return; } | ||
calendarHeatmap.items.selectAll('.item-block-year') | ||
.transition() | ||
.duration(calendarHeatmap.settings.transition_duration) | ||
.ease('ease-in') | ||
.style('opacity', 1); | ||
}) | ||
.on('click', function (d) { | ||
if ( calendarHeatmap.in_transition ) { return; } | ||
// Set in_transition flag | ||
calendarHeatmap.in_transition = true; | ||
// Set selected month to the one clicked on | ||
calendarHeatmap.selected = d; | ||
// Hide tooltip | ||
calendarHeatmap.hideTooltip(); | ||
// Remove all year overview related items and labels | ||
calendarHeatmap.removeGlobalOverview(); | ||
// Redraw the chart | ||
calendarHeatmap.overview = 'year'; | ||
calendarHeatmap.drawChart(); | ||
}); | ||
}, | ||
/** | ||
* Draw year overview | ||
@@ -163,6 +392,16 @@ */ | ||
var year_ago = moment().startOf('day').subtract(1, 'year'); | ||
var max_value = d3.max(calendarHeatmap.data, function (d) { | ||
// Define start and end date of the selected year | ||
var start_of_year = moment(calendarHeatmap.selected.date).startOf('year'); | ||
var end_of_year = moment(calendarHeatmap.selected.date).endOf('year'); | ||
// Filter data down to the selected year | ||
var year_data = calendarHeatmap.data.filter(function (d) { | ||
return start_of_year <= moment(d.date) && moment(d.date) < end_of_year; | ||
}); | ||
// Calculate max value of the year data | ||
var max_value = d3.max(year_data, function (d) { | ||
return d.total; | ||
}); | ||
var color = d3.scale.linear() | ||
@@ -174,3 +413,3 @@ .range(['#ffffff', calendarHeatmap.color || '#ff4500']) | ||
var date = moment(d.date); | ||
var dayIndex = Math.round((date - moment(year_ago).startOf('week')) / 86400000); | ||
var dayIndex = Math.round((date - moment(start_of_year).startOf('week')) / 86400000); | ||
var colIndex = Math.trunc(dayIndex / 7); | ||
@@ -189,3 +428,3 @@ return colIndex * (calendarHeatmap.settings.item_size + calendarHeatmap.settings.gutter) + calendarHeatmap.settings.label_padding; | ||
calendarHeatmap.items.selectAll('.item-circle') | ||
.data(calendarHeatmap.data) | ||
.data(year_data) | ||
.enter() | ||
@@ -348,5 +587,3 @@ .append('rect') | ||
// Add month labels | ||
var today = moment().endOf('day'); | ||
var today_year_ago = moment().startOf('day').subtract(1, 'year'); | ||
var month_labels = d3.time.months(today_year_ago.startOf('month'), today); | ||
var month_labels = d3.time.months(start_of_year, end_of_year); | ||
var monthScale = d3.scale.linear() | ||
@@ -464,2 +701,5 @@ .range([0, calendarHeatmap.settings.width]) | ||
}); | ||
// Add button to switch back to year overview | ||
calendarHeatmap.drawButton(); | ||
}, | ||
@@ -709,3 +949,2 @@ | ||
// Add day labels | ||
@@ -1241,3 +1480,5 @@ calendarHeatmap.labels.selectAll('.label-day').remove(); | ||
// Clean the canvas from whichever overview type was on | ||
if ( calendarHeatmap.overview === 'month' ) { | ||
if ( calendarHeatmap.overview === 'year' ) { | ||
calendarHeatmap.removeYearOverview(); | ||
} else if ( calendarHeatmap.overview === 'month' ) { | ||
calendarHeatmap.removeMonthOverview(); | ||
@@ -1261,5 +1502,5 @@ } else if ( calendarHeatmap.overview === 'week' ) { | ||
.attr('x', calendarHeatmap.settings.label_padding / 2.25) | ||
.attr('y', calendarHeatmap.settings.label_padding / 2.75) | ||
.attr('y', calendarHeatmap.settings.label_padding / 2.5) | ||
.attr('dy', function () { | ||
return Math.floor(calendarHeatmap.settings.width / 100) / 2.5; | ||
return Math.floor(calendarHeatmap.settings.width / 100) / 3; | ||
}) | ||
@@ -1280,2 +1521,16 @@ .attr('font-size', function () { | ||
*/ | ||
removeGlobalOverview: function () { | ||
calendarHeatmap.items.selectAll('.item-block-year') | ||
.transition() | ||
.duration(calendarHeatmap.settings.transition_duration) | ||
.ease('ease-out') | ||
.style('opacity', 0) | ||
.remove(); | ||
calendarHeatmap.labels.selectAll('.label-year').remove(); | ||
}, | ||
/** | ||
* Transition and remove items and labels related to year overview | ||
*/ | ||
removeYearOverview: function () { | ||
@@ -1290,2 +1545,3 @@ calendarHeatmap.items.selectAll('.item-circle') | ||
calendarHeatmap.labels.selectAll('.label-month').remove(); | ||
calendarHeatmap.hideBackButton(); | ||
}, | ||
@@ -1292,0 +1548,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
779587
15
1691
127