|
| 1 | +<!-- credit for https://blog.douchi.space/hugo-blog-heatmap/ --> |
| 2 | +<div |
| 3 | + id="heatmap" |
| 4 | + style=" |
| 5 | + display: block; |
| 6 | + height: 150px; |
| 7 | + width: 75%; |
| 8 | + padding: 2px; |
| 9 | + text-align: center;" |
| 10 | +></div> |
| 11 | +<script src=" https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js" ></script> |
| 12 | +<script type="text/javascript"> |
| 13 | + var chartDom = document.getElementById('heatmap'); |
| 14 | + var myChart = echarts.init(chartDom); |
| 15 | + window.onresize = function() { |
| 16 | + myChart.resize(); |
| 17 | + }; |
| 18 | + var option; |
| 19 | + |
| 20 | + // get date range by number of months |
| 21 | + function getDateRange(months){ |
| 22 | + var startDate = new Date(); |
| 23 | + var mill = startDate.setMonth((startDate.getMonth() - months)); |
| 24 | + var endDate = +new Date(); |
| 25 | + startDate = +new Date(mill); |
| 26 | + |
| 27 | + endDate = echarts.format.formatTime('yyyy-MM-dd', endDate); |
| 28 | + startDate = echarts.format.formatTime('yyyy-MM-dd', startDate); |
| 29 | + |
| 30 | + var dateRange = []; |
| 31 | + dateRange.push([ |
| 32 | + startDate, |
| 33 | + endDate |
| 34 | + ]); |
| 35 | + return dateRange |
| 36 | + }; |
| 37 | + |
| 38 | + // get number of months by window size |
| 39 | + function getMonthCount(){ |
| 40 | + const windowWidth = window.innerWidth; |
| 41 | + if (windowWidth >= 600) { |
| 42 | + return 12; |
| 43 | + } |
| 44 | + if (windowWidth >= 400) { |
| 45 | + return 9; |
| 46 | + } |
| 47 | + return 6; |
| 48 | + } |
| 49 | + |
| 50 | + var postsByDate = new Map(); |
| 51 | + {{ range ((where .Site.RegularPages "Type" "post")) }} |
| 52 | + |
| 53 | + var date = {{ .Date.Format "2006-01-02" }}; |
| 54 | + var postObj = new Map(); |
| 55 | + postObj.set("title", {{ .Title }}); |
| 56 | + postObj.set("link", {{ .RelPermalink }}); |
| 57 | + var wordCount = {{ .WordCount }}; |
| 58 | + |
| 59 | + var data = postsByDate.get(date); |
| 60 | + if (data === undefined) { |
| 61 | + data = new Map(); |
| 62 | + data.set("posts", []); |
| 63 | + data.set("totalWordCount", 0); |
| 64 | + } |
| 65 | + var posts = data.get("posts"); |
| 66 | + posts.push(postObj); |
| 67 | + var totalWordCount = data.get("totalWordCount"); |
| 68 | + totalWordCount += wordCount; |
| 69 | + data.set("totalWordCount", totalWordCount); |
| 70 | + postsByDate.set(date, data); |
| 71 | + {{- end -}} |
| 72 | + |
| 73 | + var heatmapData = []; |
| 74 | + for (const [date, data] of postsByDate.entries()) { |
| 75 | + heatmapData.push([date, data.get("totalWordCount")]); |
| 76 | + } |
| 77 | + |
| 78 | + option = { |
| 79 | + title: { |
| 80 | + show: true, |
| 81 | + top: 0, |
| 82 | + left: 'center', |
| 83 | + text: '{{ .Get "title_text" }}' |
| 84 | + }, |
| 85 | + legend: { |
| 86 | + show: false, |
| 87 | + }, |
| 88 | + visualMap: { |
| 89 | + show: false, |
| 90 | + min: 0, |
| 91 | + max: 10000, |
| 92 | + type: 'piecewise', |
| 93 | + showLable: false, |
| 94 | + orient: 'horizontal', |
| 95 | + left: 'center', |
| 96 | + top: 0, |
| 97 | + itemGap: 10, |
| 98 | + inRange: { |
| 99 | + color: ['#c6e48b', '#7bc96f', '#239a3b', '#196127'], |
| 100 | + }, |
| 101 | + }, |
| 102 | + calendar: { |
| 103 | + top: 50, |
| 104 | + left: 30, |
| 105 | + right: 30, |
| 106 | + cellSize: ['auto', 'auto'], |
| 107 | + range: getDateRange(getMonthCount()), |
| 108 | + itemStyle: { |
| 109 | + color: '#fff', |
| 110 | + borderWidth: 0.5, |
| 111 | + borderColor: '#eee', |
| 112 | + }, |
| 113 | + yearLabel: { |
| 114 | + show: false, |
| 115 | + }, |
| 116 | + dayLabel: { |
| 117 | + align: 'center', |
| 118 | + nameMap: 'ZH', |
| 119 | + }, |
| 120 | + monthLabel: { |
| 121 | + nameMap: 'EN', |
| 122 | + }, |
| 123 | + splitLine: { |
| 124 | + show: false, |
| 125 | + }, |
| 126 | + }, |
| 127 | + tooltip: { |
| 128 | + hideDelay: 1000, |
| 129 | + enterable: true, |
| 130 | + formatter: function(params) { |
| 131 | + const date = params.data[0]; |
| 132 | + const posts = postsByDate.get(date).get("posts"); |
| 133 | + var content = `${date}`; |
| 134 | + for (const [i, post] of posts.entries()) { |
| 135 | + content += "<br>"; |
| 136 | + var link = post.get("link"); |
| 137 | + var title = post.get("title"); |
| 138 | + content += `<a href="${link}" target="_blank">${title}</a>` |
| 139 | + } |
| 140 | + return content; |
| 141 | + } |
| 142 | + |
| 143 | + }, |
| 144 | + series: { |
| 145 | + type: 'heatmap', |
| 146 | + coordinateSystem: 'calendar', |
| 147 | + data: heatmapData |
| 148 | + } |
| 149 | +}; |
| 150 | +option && myChart.setOption(option); |
| 151 | +</script> |
0 commit comments