forked from wa0x6e/cal-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcal-heatmap.min.js
9 lines (9 loc) · 18.1 KB
/
cal-heatmap.min.js
1
2
3
4
5
6
7
8
9
/*! cal-heatmap v2.2.1 (Wed Jun 19 2013 14:14:27)
* ---------------------------------------------
* A module to create calendar heat map to visualise time data series a la github contribution graph
* https://github.com/kamisama/cal-heatmap
* Licensed under the MIT license
* Copyright 2013 Wan Qi Chen
*/
var CalHeatMap=function(){"use strict";var t=this,e=["json","csv","txt"];this.options={id:"cal-heatmap",paintOnLoad:!0,range:12,cellsize:10,cellpadding:2,cellradius:0,domainGutter:2,domain:"hour",subDomain:"min",weekStartOnMonday:1,start:new Date,data:"",dataType:e[0],loadOnInit:!0,scale:[10,20,30,40],displayScale:!0,highlightToday:!1,format:{date:null,legend:null},itemName:["item","items"],cellLabel:{empty:"{date}",filled:"{count} {name} {connector} {date}"},scaleLabel:{lower:"less than {min} {name}",inner:"between {down} and {up} {name}",upper:"more than {max} {name}"},duration:500,browsing:!1,browsingOptions:{nextLabel:"Next",previousLabel:"Previous"},onClick:null,afterLoad:null,afterLoadNextDomain:function(){},afterLoadPreviousDomain:function(){},onComplete:null,afterLoadData:function(t){return t}},this._domainType={min:{row:function(){return 10},column:function(){return 6},position:{x:function(e){return Math.floor(e.getMinutes()/t._domainType.min.row(e))},y:function(e){return e.getMinutes()%t._domainType.min.row(e)}},format:{date:"%H:%M, %A %B %-e, %Y",legend:"",connector:"at"},extractUnit:function(t){return t.getMinutes()}},hour:{name:"hour",row:function(){return 6},column:function(e){switch(t.options.domain){case"day":return 4;case"week":return 28;case"month":return 4*t.getEndOfMonth(e).getDate()}},position:{x:function(e){return"month"===t.options.domain?Math.floor(e.getHours()/t._domainType.hour.row(e))+4*(e.getDate()-1):"week"===t.options.domain?Math.floor(e.getHours()/t._domainType.hour.row(e))+4*t.getWeekDay(e):Math.floor(e.getHours()/t._domainType.hour.row(e))},y:function(e){return e.getHours()%t._domainType.hour.row(e)}},format:{date:"%Hh, %A %B %-e, %Y",legend:"%H:00",connector:"at"},extractUnit:function(e){var n=d3.time.format("%H");return e.getFullYear()+""+t.getDayOfYear(e)+n(e)}},day:{name:"day",row:function(){return 7},column:function(e){switch(e=new Date(e),t.options.domain){case"year":return 54;case"month":return t.getWeekNumber(new Date(e.getFullYear(),e.getMonth()+1,0))-t.getWeekNumber(e)+1;case"week":return 1}},position:{x:function(e){switch(t.options.domain){case"week":return 0;case"month":return t.getWeekNumber(e)-t.getWeekNumber(new Date(e.getFullYear(),e.getMonth()));case"year":return t.getWeekNumber(e)}},y:function(e){return t.getWeekDay(e)}},format:{date:"%A %B %-e, %Y",legend:"%e %b",connector:"on"},extractUnit:function(e){return e.getFullYear()+""+t.getDayOfYear(e)}},x_day:{name:"x_day",row:function(){switch(t.options.domain){case"year":return 54;case"month":return 6;case"week":return 1}},column:function(){return 7},position:{x:function(e){return t.getWeekDay(e)},y:function(e){switch(t.options.domain){case"week":return 0;case"month":return t.getWeekNumber(e)-t.getWeekNumber(new Date(e.getFullYear(),e.getMonth()));case"year":return t.getWeekNumber(e)}}},format:{date:"%A %B %-e, %Y",legend:"%e %b",connector:"on"},extractUnit:function(e){return e.getFullYear()+""+t.getDayOfYear(e)}},week:{name:"week",row:function(){return 1},column:function(e){switch(e=new Date(e),t.options.domain){case"year":return 54;case"month":return t.getWeekNumber(new Date(e.getFullYear(),e.getMonth()+1,0))-t.getWeekNumber(e)}return 1},position:{x:function(e){switch(t.options.domain){case"year":return t.getWeekNumber(e);case"month":return t.getWeekNumber(e)-t.getWeekNumber(new Date(e.getFullYear(),e.getMonth()))-1}},y:function(){return 0}},format:{date:"%B Week #%W",legend:"%B Week #%W",connector:"on"},extractUnit:function(e){return t.getWeekNumber(e)}},month:{name:"month",row:function(){return 1},column:function(){return 12},position:{x:function(e){return Math.floor(e.getMonth()/t._domainType.month.row(e))},y:function(e){return e.getMonth()%t._domainType.month.row(e)}},format:{date:"%B %Y",legend:"%B",connector:"on"},extractUnit:function(t){return t.getMonth()}},year:{name:"year",row:function(){return 1},column:function(){return 12},position:{x:function(t){return Math.floor(t.getFullYear()/this._domainType.year.row(t))},y:function(t){return t.getFullYear()%this._domainType.year.row(t)}},format:{date:"%Y",legend:"%Y",connector:"on"},extractUnit:function(t){return t.getFullYear()}}},this.svg=null,this._completed=!1,this._domains=[];var n=0,o=[],a=function(){return t.formatDate="function"==typeof t.options.format.date?t.options.format.date:d3.time.format(t.options.format.date),t._domains=t.getDomain(t.options.start).map(function(t){return t.getTime()}),t.options.browsing&&(d3.select("#"+t.options.id).append("a").attr("href","#").attr("rel","prev").attr("class","graph-browse-previous").attr("title","Load previous "+t._domainType[t.options.domain].name).on("click",function(){t.loadPreviousDomain()}).html(t.options.browsingOptions.previousLabel),d3.select("#"+t.options.id).append("a").attr("href","#").attr("rel","next").attr("class","graph-browse-next").attr("title","Load next "+t._domainType[t.options.domain].name).on("click",function(){t.loadNextDomain()}).html(t.options.browsingOptions.nextLabel)),d3.select("#"+t.options.id).append("svg").attr("class","graph"),t.options.paintOnLoad&&(t.paint(),t.options.displayScale&&t.displayScale(),null!==t.options.afterLoad&&t.afterLoad(),t.options.loadOnInit?t.fill(t.getDatas(t.options.data,new Date(t._domains[0]),t.getSubDomain(t._domains[t._domains.length-1]).pop()),t.svg):"function"==typeof t.options.onComplete&&t.onComplete()),!0};this.loadNextDomain=function(){d3.event&&d3.event.preventDefault(),t._domains.push(t.getNextDomain().getTime()),t._domains.shift(),t.paint(),t.getDatas(t.options.data,new Date(t._domains[t._domains.length-1]),t.getSubDomain(t._domains[t._domains.length-1]).pop(),t.svg),t.afterLoadNextDomain(new Date(t._domains[t._domains.length-1]))},this.loadPreviousDomain=function(){d3.event&&d3.event.preventDefault(),t._domains.unshift(t.getPreviousDomain().getTime()),t._domains.pop(),t.paint(!0),t.getDatas(t.options.data,new Date(t._domains[0]),t.getSubDomain(t._domains[0]).pop(),t.svg),t.afterLoadPreviousDomain(new Date(t._domains[0]))},this.paint=function(e){function a(e){t.options.cellradius>0&&e.attr("rx",t.options.cellradius).attr("ry",t.options.cellradius)}e===void 0&&(e=!1);var i=Math.max(25,2*t.options.cellsize),r=function(e){return t.options.cellsize*t._domainType[t.options.subDomain].column(e)+t.options.cellpadding*t._domainType[t.options.subDomain].column(e)},s=function(e){return t.options.cellsize*t._domainType[t.options.subDomain].row(e)+t.options.cellpadding*t._domainType[t.options.subDomain].row(e)+t.options.cellpadding},u=d3.time.format(t.options.format.legend),l=function(t){return 0===n?o[t]:e?o[0]:o[t+1]},c=function(o){return e?n:-1*r(o)-t.options.domainGutter},m=function(o){return e?n+r(o)/2:-1*(t.options.domainGutter+r(o)/2)},d=d3.select("#"+t.options.id+" .graph").attr("height",function(t){return s(t)+i}).selectAll("svg").data(t._domains,function(t){return t}),p=0,f=0,g=d.enter().insert("svg:svg").attr("width",function(a){var i=r(a);return p+=f=i+t.options.domainGutter,0===n?o.push(p-f):e?o.unshift(-1*f):o.push(n),i}).attr("height",function(t){return s(t)+i}).attr("x",function(t,e){return l(e)}),h=d3.select("#"+t.options.id+" .graph").selectAll("text").data(t._domains,function(t){return t});h.enter().insert("text").attr("y",function(t){return s(t)+i/2}).attr("x",function(t,e){return l(e)+r(t)/2}).attr("class","graph-label").attr("text-anchor","middle").attr("dominant-baseline","middle").text(function(t){return u(new Date(t))});var D=d.selectAll("rect").data(function(e){return t.getSubDomain(e)}).enter().append("svg:rect").attr("class",function(e){return t.getClassName(e)}).attr("width",t.options.cellsize).attr("height",t.options.cellsize).attr("x",function(e){return t.positionSubDomainX(e)}).attr("y",function(e){return t.positionSubDomainY(e)}).call(a);D.append("svg:title").text(function(e){return t.formatDate(e)});var y=e?n-o[o.length-1]:o[1];if(0!==n){for(var w=o.length-1;w>=0;)e?o[w]-=o[0]:w>=1&&(o[w]-=o[1]),w--;e?o.pop():o.shift()}if(d.transition().duration(t.options.duration).attr("x",function(t,e){return o[e]}),d.exit().transition().duration(t.options.duration).attr("x",function(t){return c(t)}).remove(),h.transition().duration(t.options.duration).attr("x",function(t,e){return o[e]+r(t)/2}),h.exit().transition().duration(t.options.duration).attr("x",function(t){return m(t)}).remove(),0===n)n=p,d3.select("#"+t.options.id+" .graph").attr("width",n);else if(f!==y){var v=n+f-y;v!==n&&(n=v,d3.select("#"+t.options.id+" .graph").transition().duration(t.options.duration).attr("width",n))}t.svg=null===t.svg?g:d3.select("#"+t.options.id+" .graph").selectAll("svg").data(t._domains,function(t){return t})},this.init=function(n){if(null!==n&&void 0!==n&&"undefined"!==n)for(var o in t.options)null!==n[o]&&void 0!==n[o]&&"undefined"!==n[o]&&(t.options[o]=n[o]);return this._domainType.hasOwnProperty(t.options.domain)&&"min"!==t.options.domain?0>e.indexOf(t.options.dataType)?(console.log("The data type '"+t.options.dataType+"' is not valid data type"),!1):(t.getDomain(t.options.start),null===t.options.format.date&&(t.options.format.date=this._domainType[t.options.subDomain].format.date),null===t.options.format.legend&&(t.options.format.legend=this._domainType[t.options.domain].format.legend),a()):(console.log("The domain '"+t.options.domain+"' is not valid domain"),!1)}};CalHeatMap.prototype={onClick:function(t,e){return"function"==typeof this.options.onClick?this.options.onClick(t,e):(console.log("Provided callback for onClick is not a function."),!1)},afterLoad:function(){return"function"==typeof this.options.afterLoad?this.options.afterLoad():(console.log("Provided callback for afterLoad is not a function."),!1)},onComplete:function(){return"function"==typeof this.options.onComplete?this.options.onComplete():(console.log("Provided callback for onComplete is not a function."),!1)},afterLoadPreviousDomain:function(t){if("function"==typeof this.options.afterLoadPreviousDomain){var e=this.getSubDomain(t);return this.options.afterLoadPreviousDomain(e.shift(),e.pop())}return console.log("Provided callback for afterLoadPreviousDomain is not a function."),!1},afterLoadNextDomain:function(t){if("function"==typeof this.options.afterLoadNextDomain){var e=this.getSubDomain(t);return this.options.afterLoadNextDomain(e.shift(),e.pop())}return console.log("Provided callback for afterLoadNextDomain is not a function."),!1},formatNumber:d3.format(",g"),displayScale:function(){var t=this,e=d3.select("#"+this.options.id).append("svg:svg").attr("class","graph-scale").attr("height",this.options.cellsize+2*this.options.cellpadding).selectAll().data(d3.range(0,this.options.scale.length+1)),n=e.enter().append("svg:rect").attr("width",this.options.cellsize).attr("height",this.options.cellsize).attr("class",function(t){return"graph-rect q"+(t+1)}).attr("transform",function(e){return"translate("+e*(t.options.cellsize+t.options.cellpadding)+", "+t.options.cellpadding+")"}).attr("fill-opacity",0);n.transition().delay(function(e,n){return t.options.duration*n/10}).attr("fill-opacity",1),n.append("svg:title").text(function(e){return t.options.scale[e+1],0===e?t.options.scaleLabel.lower.format({min:t.options.scale[e],name:t.options.itemName[1]}):e===t.options.scale.length?t.options.scaleLabel.upper.format({max:t.options.scale[e-1],name:t.options.itemName[1]}):t.options.scaleLabel.inner.format({down:t.options.scale[e-1],up:t.options.scale[e],name:t.options.itemName[1]})})},display:function(t,e){var n=this;return e.each(function(e){t.hasOwnProperty(e)&&d3.select(this).selectAll("rect").attr("class",function(o){var a=n._domainType[n.options.subDomain].extractUnit(o),i=n.getClassName(o)+(t[e].hasOwnProperty(a)?" "+n.scale(t[e][a]):"");return null!==n.options.onClick&&(i+=" hover_cursor"),i}).on("click",function(o){if(null!==n.options.onClick){var a=n._domainType[n.options.subDomain].extractUnit(o);return n.onClick(o,t[e].hasOwnProperty(a)?t[e][a]:0)}}).select("title").text(function(o){var a=n._domainType[n.options.subDomain].extractUnit(o);return t[e].hasOwnProperty(a)&&null!==t[e][a]?n.options.cellLabel.filled.format({count:n.formatNumber(t[e][a]),name:n.options.itemName[1!==t[e][a]?1:0],connector:n._domainType[n.options.subDomain].format.connector,date:n.formatDate(o)}):n.options.cellLabel.empty.format({date:n.formatDate(o)})})}),!0},positionSubDomainX:function(t){var e=this._domainType[this.options.subDomain].position.x(t);return e*this.options.cellsize+e*this.options.cellpadding},positionSubDomainY:function(t){var e=this._domainType[this.options.subDomain].position.y(t);return e*this.options.cellsize+e*this.options.cellpadding},getClassName:function(t){var e="graph-rect";return this.options.highlightToday&&"hour"!==this.options.subDomain&&"min"!==this.options.subDomain&&this.isToday(t)&&(e+=" today"),e},isToday:function(t){var e=!1,n=new Date,o=n.getMonth()+1,a=n.getDate(),i=n.getFullYear();return a===t.getDate()&&o===t.getMonth()+1&&i===t.getFullYear()&&(e=!0),e},getDayOfYear:d3.time.format("%j"),getWeekNumber:function(t){var e=1===this.options.weekStartOnMonday?d3.time.format("%W"):d3.time.format("%U");return e(t)},getWeekDay:function(t){return 0===this.options.weekStartOnMonday?t.getDay():0===t.getDay()?6:t.getDay()-1},getEndOfMonth:function(t){return"number"==typeof t&&(t=new Date(t)),new Date(t.getFullYear(),t.getMonth()+1,0)},getWeekDomain:function(t,e){var n;0===this.options.weekStartOnMonday?n=new Date(t.getFullYear(),t.getMonth(),t.getDate()-t.getDay()):1===t.getDay()?n=new Date(t.getFullYear(),t.getMonth(),t.getDate()):0===t.getDay()?(n=new Date(t.getFullYear(),t.getMonth(),t.getDate()),n.setDate(n.getDate()-6)):n=new Date(t.getFullYear(),t.getMonth(),t.getDate()-t.getDay()+1);var o=new Date(n),a=new Date(o.setDate(o.getDate()+7*e));return 1===this.options.weekStartOnMonday?d3.time.mondays(Math.min(n,a),Math.max(n,a)):d3.time.sundays(Math.min(n,a),Math.max(n,a))},getYearDomain:function(t,e){var n=new Date(t.getFullYear(),0),o=new Date(t.getFullYear()+e,0);return d3.time.years(Math.min(n,o),Math.max(n,o))},getMinuteDomain:function(t,e){var n=new Date(t.getFullYear(),t.getMonth(),t.getDate(),t.getHours()),o=new Date(n.getTime()+6e4*e);return d3.time.minutes(Math.min(n,o),Math.max(n,o))},getHourDomain:function(t,e){var n=new Date(t.getFullYear(),t.getMonth(),t.getDate(),t.getHours()),o=e;return"number"==typeof e&&(o=new Date(n.getTime()+36e5*e)),d3.time.hours(Math.min(n,o),Math.max(n,o))},getDayDomain:function(t,e){var n=new Date(t.getFullYear(),t.getMonth(),t.getDate()),o=new Date(n);return o=new Date(o.setDate(o.getDate()+parseInt(e,10))),d3.time.days(Math.min(n,o),Math.max(n,o))},getMonthDomain:function(t,e){var n=new Date(t.getFullYear(),t.getMonth()),o=new Date(n);return o=o.setMonth(o.getMonth()+e),d3.time.months(Math.min(n,o),Math.max(n,o))},getDomain:function(t,e){switch("number"==typeof t&&(t=new Date(t)),e===void 0&&(e=this.options.range),this.options.domain){case"hour":return this.getHourDomain(t,e);case"x_day":case"day":return this.getDayDomain(t,e);case"week":return this.getWeekDomain(t,e);case"month":return this.getMonthDomain(t,e);case"year":return this.getYearDomain(t,e)}},getSubDomain:function(t){"number"==typeof t&&(t=new Date(t));var e=this,n=function(t,n){if("year"===n)return e.getDayOfYear(new Date(t.getFullYear()+1,0,0));if("month"===n){var o=new Date(t.getFullYear(),t.getMonth()+1,0);return o.getDate()}return"week"===n?7:void 0},o=function(t,e){switch(e){case"hour":return 60;case"x_day":case"day":return 1440;case"week":return 10080}},a=function(t,e){if("day"===e||"x_day"===e)return 24;if("week"===e)return 168;if("month"===e){var n=new Date(t.getFullYear(),t.getMonth()+1,0);return 24*n.getDate()}},i=function(t,n){if("month"===n){var o=new Date(t.getFullYear(),t.getMonth()+1,0),a=e.getWeekNumber(o),i=e.getWeekNumber(new Date(t.getFullYear(),t.getMonth()));return i>a&&(i=0,a++),a-i+1}return"year"===n?e.getWeekNumber(new Date(t.getFullYear(),11,31)):void 0};switch(this.options.subDomain){case"min":return this.getMinuteDomain(t,o(t,this.options.domain));case"hour":return this.getHourDomain(t,a(t,this.options.domain));case"x_day":case"day":return this.getDayDomain(t,n(t,this.options.domain));case"week":return this.getWeekDomain(t,i(t,this.options.domain));case"month":return this.getMonthDomain(t,12)}},getNextDomain:function(){return this.getDomain(this._domains[this._domains.length-1],2).pop()},getPreviousDomain:function(){return this.getDomain(this._domains[0],-1)[0]},scale:function(t){if(isNaN(t))return"qi";if(null===t)return"";for(var e=0,n=this.options.scale.length-1;n>=e;e++){if(0===t&&this.options.scale[0]>0)return"";if(this.options.scale[0]>0&&0>t)return"qi";if(this.options.scale[e]>=t)return"q"+(e+1)}return"q"+(this.options.scale.length+1)},fill:function(t,e){return t!==!1&&t!==!0?(null!==this.options.onComplete&&this._completed===!1&&(this.onComplete(),this._completed=!0),this.display(this.parseDatas(t),e)):!1},getDatas:function(t,e,n,o){var a=this;switch(o===void 0&&(o=a.svg),typeof t){case"string":if(""===t)return!1;var i=function(t){a.fill(t,o)};switch(this.options.dataType){case"json":d3.json(this.parseURI(t,e,n),i);break;case"csv":d3.csv(this.parseURI(t,e,n),i);break;case"text":d3.text(this.parseURI(t,e,n),"text/plain",i)}return!0;case"object":return t}return!1},parseDatas:function(t){var e={};if("function"!=typeof this.options.afterLoadData)return console.log("Provided callback for afterLoadData is not a function."),{};t=this.options.afterLoadData(t);for(var n in t){var o=new Date(1e3*n),a=this.getDomain(o)[0].getTime();if(!(0>this._domains.indexOf(a))){var i=this._domainType[this.options.subDomain].extractUnit(o);e[a]===void 0&&(e[a]={}),e[a][i]!==void 0?e[a][i]+=t[n]:e[a][i]=t[n]}}return e},parseURI:function(t,e,n){return t=t.replace(/\{\{t:start\}\}/g,e.getTime()/1e3),t=t.replace(/\{\{t:end\}\}/g,n.getTime()/1e3),t=t.replace(/\{\{d:start\}\}/g,e.toISOString()),t=t.replace(/\{\{d:end\}\}/g,n.toISOString())}},String.prototype.format=function(){var t=this;for(var e in arguments[0]){var n=RegExp("\\{"+e+"\\}","gi");t=t.replace(n,arguments[0][e])}return t},"function"==typeof define&&define.amd&&define(["d3"],function(){return CalHeatMap});
//@ sourceMappingURL=cal-heatmap.source-map.js