Files
blog/themes/Ying/layouts/shortcodes/heatmap.html
T

137 lines
4.0 KiB
HTML
Raw Normal View History

2024-07-26 15:28:04 +08:00
<div id="heatmap" style="max-width: 700px;height: 110px;margin-bottom: 40px;"></div>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/echarts/5.3.0-rc.1/echarts.min.js"></script>
2024-07-13 11:42:38 +08:00
<script type="text/javascript">
var chartDom = document.getElementById('heatmap');
var myChart = echarts.init(chartDom);
window.onresize = function() {
myChart.resize();
};
var option;
var dataMap = new Map();
{{ range ((where .Site.RegularPages "Type" "post")) }}
var key = {{ .Date.Format "2006-01-02" }};
var value = dataMap.get(key);
var link = {{ .RelPermalink}};
var title = {{ .Title }};
// multiple posts in same day
if (value == null) {
2024-07-26 15:28:04 +08:00
dataMap.set(key, [{link, title}]);
2024-07-13 11:42:38 +08:00
} else {
2024-07-26 15:28:04 +08:00
value.push({link, title});
2024-07-13 11:42:38 +08:00
}
{{- end -}}
var data = [];
for (const [key, value] of dataMap.entries()) {
2024-07-26 15:28:04 +08:00
data.push([key, value.length]);
2024-07-13 11:42:38 +08:00
}
2024-07-26 15:28:04 +08:00
2024-07-13 11:42:38 +08:00
var startDate = new Date();
var year_Mill = startDate.setFullYear((startDate.getFullYear() - 1));
var startDate = +new Date(year_Mill);
var endDate = +new Date();
startDate = echarts.format.formatTime('yyyy-MM-dd', startDate);
endDate = echarts.format.formatTime('yyyy-MM-dd', endDate);
2024-07-26 15:28:04 +08:00
// 检测浏览器主题模式并选择颜色方案
var prefersDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
2024-07-13 11:42:38 +08:00
2024-07-26 15:28:04 +08:00
// 定义明亮模式下的颜色方案
var lightTheme = {
backgroundColor: '#FFFFFF',
fangkuaicolor:'#F4F4F4',
2024-07-26 15:47:36 +08:00
gaoliangcolor: ['#333'],
2024-07-26 15:28:04 +08:00
riqiColor: '#999',
textbrcolor: '#FFF',
xiankuangcolor:'rgba(0, 0, 0, 0.0)',
2024-07-13 11:42:38 +08:00
};
2024-07-26 15:28:04 +08:00
// 定义暗黑模式下的颜色方案
var darkTheme = {
backgroundColor: '#1A1718',
fangkuaicolor:'#282325',
gaoliangcolor: ['#b25f2f'],
riqiColor: '#666',
textbrcolor: '#332D2F',
xiankuangcolor:'rgba(0, 0, 0, 0.0)',
};
// 根据浏览器主题模式选择当前主题
var currentTheme = prefersDarkMode ? darkTheme : lightTheme;
2024-07-13 11:42:38 +08:00
option = {
tooltip: {
2024-07-26 15:28:04 +08:00
hideDelay: 1000,
enterable: true,
backgroundColor: currentTheme.textbrcolor,
borderWidth: 0, // 边框宽度为0
formatter: function (p) {
2024-07-13 11:42:38 +08:00
const date = p.data[0];
const posts = dataMap.get(date);
2024-07-26 15:28:04 +08:00
var content = `<span style="font-size: 0.75rem;font-family: var(--font-family-code);">${date}</span>`;
2024-07-13 11:42:38 +08:00
for (const [i, post] of posts.entries()) {
2024-07-26 15:28:04 +08:00
content += "<br>";
2024-07-13 11:42:38 +08:00
var link = post.link;
var title = post.title;
2024-07-26 15:28:04 +08:00
content += `<a href="${link}" target="_blank">${title}</a>` + '<br>';
2024-07-13 11:42:38 +08:00
}
return content;
2024-07-26 15:28:04 +08:00
}
2024-07-13 11:42:38 +08:00
},
visualMap: {
2024-07-26 15:28:04 +08:00
show: false,
2024-07-13 11:42:38 +08:00
inRange: {
2024-07-26 15:28:04 +08:00
color: currentTheme.gaoliangcolor
2024-07-13 11:42:38 +08:00
},
},
calendar: {
left: 20,
2024-07-26 15:28:04 +08:00
top:20,
bottom:0,
right: 0,
2024-07-13 11:42:38 +08:00
cellSize: ['auto', 13],
2024-07-26 15:28:04 +08:00
range: [startDate, endDate],
2024-07-13 11:42:38 +08:00
itemStyle: {
2024-07-26 15:28:04 +08:00
color: currentTheme.fangkuaicolor,
borderWidth: 3.5,
borderColor: currentTheme.backgroundColor,
2024-07-13 11:42:38 +08:00
},
yearLabel: { show: false },
2024-07-26 15:28:04 +08:00
monthLabel: {
nameMap: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
textStyle: {
color: currentTheme.riqiColor,
}
},
dayLabel: {
firstDay: 1,
nameMap: ['日', '一', '', '三', '', '五', ''],
textStyle: {
color: currentTheme.riqiColor
}
},
2024-07-13 11:42:38 +08:00
splitLine: {
2024-07-26 15:28:04 +08:00
lineStyle: {
color: currentTheme.xiankuangcolor,
}
2024-07-13 11:42:38 +08:00
}
},
series: {
type: 'heatmap',
coordinateSystem: 'calendar',
data: data,
}
};
2024-07-26 15:28:04 +08:00
2024-07-13 11:42:38 +08:00
myChart.setOption(option);
myChart.on('click', function(params) {
if (params.componentType === 'series') {
// open the first post on the day
const post = dataMap.get(params.data[0])[0];
const link = window.location.origin + post.link;
window.open(link, '_blank').focus();
}
});
2024-07-26 15:28:04 +08:00
</script>