script. var gitcalendar = new Vue({ el: '#gitcalendar', data: { simplemode: !{theme.gitcalendar.simplemode}, user: '!{theme.gitcalendar.user}', fixed: 'fixed', px: 'px', x: '', y: '', span1: '', span2: '', month: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], monthchange: [], oneyearbeforeday: '', thisday: '', amonthago: '', aweekago: '', weekdatacore: 0, datacore: 0, total: 0, datadate: '', data: [], positionplusdata: [], firstweek: [], lastweek: [], beforeweek: [], thisweekdatacore: 0, mounthbeforeday: 0, mounthfirstindex: 0, crispedges: 'crispedges', thisdayindex: 0, amonthagoindex: 0, amonthagoweek: [], firstdate: [], first2date: [], montharrbefore: [], monthindex: 0, color: !{theme.gitcalendar.color} }, methods: { selectStyle(data, event) { document.querySelector('.angle-wrapper').style.display = 'block' this.span1 = data.date; this.span2 = data.count; this.x = event.clientX - 100; this.y = event.clientY - 60 }, outStyle() { document.querySelector('.angle-wrapper').style.display = 'none' }, thiscolor(x) { if (x === 0) { let i = parseInt(x / 2); return this.color[0] } else if (x < 2) { return this.color[1] } else if (x < 20) { let i = parseInt(x / 2); return this.color[i] } else { return this.color[9] } }, } }); var githubapiurl = "https://python-github-calendar-api.vercel.app/api?" + gitcalendar.user; function responsiveChart() { let c = document.getElementById("gitcanvas"); if (c) { let cmessage = document.getElementById("gitmessage"); let ctx = c.getContext("2d"); c.width = document.getElementById("gitcalendarcanvasbox").offsetWidth; let linemaxwitdh = 0.96 * c.width / gitcalendar.data.length; c.height = 9 * linemaxwitdh; let lineminwitdh = 0.8 * linemaxwitdh; let setposition = { x: 0.02 * c.width, y: 0.025 * c.width }; for (let week in gitcalendar.data) { weekdata = gitcalendar.data[week]; for (let day in weekdata) { let dataitem = { date: "", count: "", x: 0, y: 0 }; gitcalendar.positionplusdata.push(dataitem); ctx.fillStyle = gitcalendar.thiscolor(weekdata[day].count); setposition.y = Math.round(setposition.y * 100) / 100; dataitem.date = weekdata[day].date; dataitem.count = weekdata[day].count; dataitem.x = setposition.x; dataitem.y = setposition.y; ctx.fillRect(setposition.x, setposition.y, lineminwitdh, lineminwitdh); setposition.y = setposition.y + linemaxwitdh }; setposition.y = 0.025 * c.width; setposition.x = setposition.x + linemaxwitdh }; ctx.font = "600 Arial"; ctx.fillStyle = '#aaa'; ctx.fillText("日", 0, 1.9 * linemaxwitdh); ctx.fillText("二", 0, 3.9 * linemaxwitdh); ctx.fillText("四", 0, 5.9 * linemaxwitdh); ctx.fillText("六", 0, 7.9 * linemaxwitdh); let monthindexlist = c.width / 24; for (let index in gitcalendar.monthchange) { ctx.fillText(gitcalendar.monthchange[index], monthindexlist, 0.7 * linemaxwitdh); monthindexlist = monthindexlist + c.width / 12 }; cmessage.onmousemove = function(event) { document.querySelector('.angle-wrapper').style.display = 'none' }; c.onmousemove = function(event) { document.querySelector('.angle-wrapper').style.display = 'none' getMousePos(c, event); };
function getMousePos(canvas, event) { var rect = canvas.getBoundingClientRect(); var x = event.clientX - rect.left * (canvas.width / rect.width); var y = event.clientY - rect.top * (canvas.height / rect.height); for (let item of gitcalendar.positionplusdata) { let lenthx = x - item.x; let lenthy = y - item.y; if (0 < lenthx && lenthx < lineminwitdh) { if (0 < lenthy && lenthy < lineminwitdh) { document.querySelector('.angle-wrapper').style.display = 'block' gitcalendar.span1 = item.date; gitcalendar.span2 = item.count; gitcalendar.x = event.clientX - 100; gitcalendar.y = event.clientY - 60 } } } } } } function addlastmonth() { if (gitcalendar.thisdayindex === 0) { thisweekcore(52); thisweekcore(51); thisweekcore(50); thisweekcore(49); thisweekcore(48); gitcalendar.thisweekdatacore += gitcalendar.firstdate[6].count; gitcalendar.amonthago = gitcalendar.firstdate[6].date } else { thisweekcore(52); thisweekcore(51); thisweekcore(50); thisweekcore(49); thisweek2core(); gitcalendar.amonthago = gitcalendar.first2date[gitcalendar.thisdayindex - 1].date } };
function thisweek2core() { for (let i = gitcalendar.thisdayindex - 1; i < gitcalendar.first2date.length; i++) { gitcalendar.thisweekdatacore += gitcalendar.first2date[i].count } };
function thisweekcore(index) { for (let item of gitcalendar.data[index]) { gitcalendar.thisweekdatacore += item.count } };
function addlastweek() { for (let item of gitcalendar.lastweek) { gitcalendar.weekdatacore += item.count } };
function addbeforeweek() { for (let i = gitcalendar.thisdayindex; i < gitcalendar.beforeweek.length; i++) { gitcalendar.weekdatacore += gitcalendar.beforeweek[i].count } };
function addweek(data) { if (gitcalendar.thisdayindex === 6) { gitcalendar.aweekago = gitcalendar.lastweek[0].date; addlastweek() } else { lastweek = data.contributions[51]; gitcalendar.aweekago = lastweek[gitcalendar.thisdayindex + 1].date; addlastweek(); addbeforeweek() } }
fetch(githubapiurl) .then(data => data.json()) .then(data => { gitcalendar.data = data.contributions; gitcalendar.total = data.total; gitcalendar.first2date = gitcalendar.data[48]; gitcalendar.firstdate = gitcalendar.data[47]; gitcalendar.firstweek = data.contributions[0]; gitcalendar.lastweek = data.contributions[52]; gitcalendar.beforeweek = data.contributions[51]; gitcalendar.thisdayindex = gitcalendar.lastweek.length - 1; gitcalendar.thisday = gitcalendar.lastweek[gitcalendar.thisdayindex].date; gitcalendar.oneyearbeforeday = gitcalendar.firstweek[0].date; gitcalendar.monthindex = gitcalendar.thisday.substring(5, 7) * 1; gitcalendar.montharrbefore = gitcalendar.month.splice(gitcalendar.monthindex, 12 - gitcalendar.monthindex); gitcalendar.monthchange = gitcalendar.montharrbefore.concat(gitcalendar.month); addweek(data); addlastmonth(); responsiveChart(); }) .catch(function(error) { console.log(error); });
if (document.getElementById("gitcalendarcanvasbox").offsetWidth < 500) { gitcalendar.simplemode = false }
window.onresize = function() { if (gitcalendar.simplemode) responsiveChart() }
window.onscroll = function() { if (document.querySelector('.angle-wrapper')) { document.querySelector('.angle-wrapper').style.display = 'none' } };
|