1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 | <script>
(function () {
const username = "Wcowin"; // TODO:换成你的 GitHub 用户名
const CELL = 11;
const GAP = 3;
const ROWS = 7;
const LEVEL_OPACITY = [0.05, 0.25, 0.5, 0.75, 1.0];
const MONTHS = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
const DAYS = ["", "Mon", "", "Wed", "", "Fri", ""];
function formatDate(dateStr) {
const d = new Date(dateStr);
return `${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日`;
}
async function loadHeatmap() {
try {
const response = await fetch(
`https://github-contributions-api.jogruber.de/v4/${username}?y=last`
);
const data = await response.json();
renderHeatmap(data);
} catch (error) {
console.error("Error loading GitHub contributions:", error);
document.getElementById("stats-count").textContent = "--";
}
}
function renderHeatmap(data) {
const svg = document.getElementById("heatmapSvg");
const statsEl = document.getElementById("stats-count");
const tooltipEl = document.getElementById("heatmapTooltip");
if (!svg) return;
statsEl.textContent = data.total.lastYear;
const weeks = Math.ceil(data.contributions.length / ROWS);
const labelOffset = 28;
const headerOffset = 16;
const svgWidth = labelOffset + weeks * (CELL + GAP);
const svgHeight = headerOffset + ROWS * (CELL + GAP);
svg.setAttribute("viewBox", `0 0 ${svgWidth} ${svgHeight}`);
svg.innerHTML = "";
const monthLabels = [];
let lastMonth = -1;
for (let w = 0; w < weeks; w++) {
const idx = w * ROWS;
if (idx < data.contributions.length) {
const month = new Date(data.contributions[idx].date).getMonth();
if (month !== lastMonth) {
monthLabels.push({
label: MONTHS[month],
x: labelOffset + w * (CELL + GAP),
});
lastMonth = month;
}
}
}
const ns = "http://www.w3.org/2000/svg";
// 顶部月份标签
monthLabels.forEach((m) => {
const text = document.createElementNS(ns, "text");
text.setAttribute("x", m.x);
text.setAttribute("y", 11);
text.setAttribute("class", "heatmap-label");
text.setAttribute("font-size", 10);
text.textContent = m.label;
svg.appendChild(text);
});
// 左侧星期标签
DAYS.forEach((d, i) => {
if (d) {
const text = document.createElementNS(ns, "text");
text.setAttribute("x", 0);
text.setAttribute(
"y",
headerOffset + i * (CELL + GAP) + CELL - 1
);
text.setAttribute("class", "heatmap-label");
text.setAttribute("font-size", 9);
text.textContent = d;
svg.appendChild(text);
}
});
// 贡献格子
data.contributions.forEach((day, idx) => {
const col = Math.floor(idx / ROWS);
const row = idx % ROWS;
const rect = document.createElementNS(ns, "rect");
rect.setAttribute("x", labelOffset + col * (CELL + GAP));
rect.setAttribute("y", headerOffset + row * (CELL + GAP));
rect.setAttribute("width", CELL);
rect.setAttribute("height", CELL);
rect.setAttribute("rx", 2);
rect.setAttribute("ry", 2);
rect.setAttribute("class", "heatmap-cell");
rect.setAttribute("data-date", day.date);
rect.setAttribute("data-count", day.count);
const opacity = LEVEL_OPACITY[Math.min(day.level, 4)];
rect.setAttribute("fill", `rgba(35, 154, 59, ${opacity})`);
rect.addEventListener("mouseenter", (e) => {
tooltip(day.date, day.count, e);
});
rect.addEventListener("mousemove", (e) => {
tooltip(day.date, day.count, e);
});
rect.addEventListener("mouseleave", () => {
tooltipEl.classList.remove("visible");
});
svg.appendChild(rect);
});
// 标签与 hover 高亮效果
if (!document.querySelector("style[data-heatmap-style]")) {
const style = document.createElement("style");
style.setAttribute("data-heatmap-style", "");
style.textContent = `
.heatmap-label {
fill: rgba(0, 0, 0, 0.4);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
font-size: 10px;
}
[data-md-color-scheme="slate"] .heatmap-label {
fill: rgba(255, 255, 255, 0.7);
}
.heatmap-cell {
transition: opacity 150ms, filter 150ms;
cursor: default;
filter: drop-shadow(0 0 0 rgba(35, 154, 59, 0));
}
.heatmap-cell:hover {
opacity: 1 !important;
filter: drop-shadow(0 2px 6px rgba(35, 154, 59, 0.4));
}
`;
document.head.appendChild(style);
}
function tooltip(date, count, event) {
const text =
count > 0
? `${formatDate(date)}: ${count} 次贡献`
: `${formatDate(date)}: 无贡献`;
const contentEl = tooltipEl.querySelector(".github-heatmap-tooltip__content");
if (contentEl) {
contentEl.textContent = text;
}
tooltipEl.classList.add("visible");
// 按格子定位:显示在对应格子正上方居中,箭头指向格子
const target = event.currentTarget;
const margin = 8;
const gap = 8;
if (target && target.getBoundingClientRect) {
const cellRect = target.getBoundingClientRect();
const tooltipRect = tooltipEl.getBoundingClientRect();
let x = cellRect.left + cellRect.width / 2 - tooltipRect.width / 2;
let y = cellRect.top - tooltipRect.height - gap;
let arrowPosition = cellRect.left + cellRect.width / 2;
let isAbove = true;
const maxX = window.innerWidth - tooltipRect.width - margin;
x = Math.max(margin, Math.min(x, maxX));
if (y < margin) {
y = cellRect.bottom + gap;
isAbove = false;
}
tooltipEl.style.left = x + "px";
tooltipEl.style.top = y + "px";
// 设置箭头方向并计算箭头位置
tooltipEl.classList.remove("arrow-top", "arrow-bottom");
tooltipEl.classList.add(isAbove ? "arrow-bottom" : "arrow-top");
const arrowEl = tooltipEl.querySelector(".github-heatmap-tooltip__arrow");
if (arrowEl) {
const arrowOffset = arrowPosition - x;
arrowEl.style.left = arrowOffset + "px";
}
}
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", loadHeatmap);
} else {
loadHeatmap();
}
})();
</script>
|