Skip to content

Commit 729f4be

Browse files
committed
Add heatmap for article like Github activity
1 parent 1955516 commit 729f4be

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

content/en/about_me.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ lastmod = 2022-02-23T17:23:37+08:00
55
draft = false
66
+++
77

8+
{{< heatmap title_text="Article Calendar" >}}
9+
810
## About me {#about-me}
911

1012
I'm Ramsay, a software engineer making a living by pressing keyboard, an amateur cook, an Emacs deadhead and Linux enthusiast.

content/zh/about_me_zh.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ draft = false
66
toc = true
77
+++
88

9+
{{< heatmap title_text="文章日历" >}}
10+
911
## <span class="section-num">0x0</span> 自我认知 {#自我认知}
1012
一个努力但平凡普通的人,希望做个有趣的人, work hard and be nice to people.
1113

layouts/shortcodes/heatmap.html

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)