feat: git嵌入页新增gitea贡献统计
This commit is contained in:
282
src/components/ContributionHeatmap.astro
Normal file
282
src/components/ContributionHeatmap.astro
Normal file
@@ -0,0 +1,282 @@
|
||||
---
|
||||
import type {HeatmapPoint} from "@/types/stats.ts";
|
||||
|
||||
interface Props {
|
||||
points?: HeatmapPoint[]
|
||||
/** IANA 时区,用于把时间戳归到某一天 */
|
||||
timeZone?: string
|
||||
}
|
||||
|
||||
const {points = [], timeZone = "Asia/Shanghai"} = Astro.props;
|
||||
|
||||
const CELL = 11;
|
||||
const GAP = 3;
|
||||
const STEP = CELL + GAP;
|
||||
const TOP = 20;
|
||||
const LEFT = 26;
|
||||
const DAY_MS = 86_400_000;
|
||||
|
||||
type GridCell = {
|
||||
key: string
|
||||
x: number
|
||||
y: number
|
||||
value: number
|
||||
level: number
|
||||
}
|
||||
|
||||
const dayFormatter = new Intl.DateTimeFormat("en-CA", {
|
||||
timeZone,
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit"
|
||||
});
|
||||
const monthFormatter = new Intl.DateTimeFormat("zh-CN", {timeZone: "UTC", month: "numeric"});
|
||||
|
||||
const toDayKey = (ms: number) => dayFormatter.format(new Date(ms));
|
||||
const toDayMs = (key: string) => {
|
||||
const [year, month, day] = key.split("-").map(Number);
|
||||
return Date.UTC(year, month - 1, day);
|
||||
};
|
||||
const toDateText = (key: string) => key.replaceAll("-", ".");
|
||||
|
||||
// 按天聚合:接口按分钟返回,同一天可能有多条
|
||||
const byDay = new Map<string, number>();
|
||||
for (const {timestamp, contributions} of points) {
|
||||
if (!Number.isFinite(timestamp) || !Number.isFinite(contributions)) continue;
|
||||
const key = toDayKey(timestamp * 1000);
|
||||
byDay.set(key, (byDay.get(key) ?? 0) + Math.max(0, contributions));
|
||||
}
|
||||
|
||||
const days = [...byDay.keys()].sort();
|
||||
|
||||
const cells: GridCell[] = [];
|
||||
const monthLabels: {x: number, label: string}[] = [];
|
||||
const weekdayLabels = [
|
||||
{row: 0, label: "一"},
|
||||
{row: 2, label: "三"},
|
||||
{row: 4, label: "五"}
|
||||
];
|
||||
|
||||
let weeks = 0;
|
||||
let total = 0;
|
||||
let firstDate = "";
|
||||
let lastDate = "";
|
||||
|
||||
const firstKey = days.at(0);
|
||||
|
||||
if (firstKey) {
|
||||
const todayKey = toDayKey(Date.now());
|
||||
const lastDataKey = days.at(-1) ?? firstKey;
|
||||
// 末尾补到今天,保证最后一列是完整的自然周
|
||||
const endDayMs = Math.max(toDayMs(lastDataKey), toDayMs(todayKey));
|
||||
|
||||
const startMs = toDayMs(firstKey);
|
||||
const start = startMs - ((new Date(startMs).getUTCDay() + 6) % 7) * DAY_MS;
|
||||
const end = endDayMs + (6 - ((new Date(endDayMs).getUTCDay() + 6) % 7)) * DAY_MS;
|
||||
weeks = Math.floor((end - start) / (7 * DAY_MS)) + 1;
|
||||
|
||||
total = [...byDay.values()].reduce((sum, value) => sum + value, 0);
|
||||
firstDate = firstKey;
|
||||
lastDate = toDayKey(endDayMs);
|
||||
|
||||
// 用非零数据的分位数划分 4 档,避免个别高峰压扁其余格子
|
||||
const nonZero = [...byDay.values()].filter((value) => value > 0).sort((a, b) => a - b);
|
||||
const quantile = (p: number) => nonZero[Math.min(nonZero.length - 1, Math.floor(p * (nonZero.length - 1)))];
|
||||
const t1 = Math.max(1, quantile(0.4) ?? 1);
|
||||
const t2 = Math.max(t1 + 1, quantile(0.7) ?? 2);
|
||||
const t3 = Math.max(t2 + 1, quantile(0.9) ?? 3);
|
||||
const levelOf = (value: number) =>
|
||||
value <= 0 ? 0 : value <= t1 ? 1 : value <= t2 ? 2 : value <= t3 ? 3 : 4;
|
||||
|
||||
for (let time = start; time <= end; time += DAY_MS) {
|
||||
const key = new Date(time).toISOString().slice(0, 10);
|
||||
const week = Math.floor((time - start) / (7 * DAY_MS));
|
||||
const weekday = (new Date(time).getUTCDay() + 6) % 7;
|
||||
const value = byDay.get(key) ?? 0;
|
||||
|
||||
cells.push({
|
||||
key,
|
||||
x: LEFT + week * STEP,
|
||||
y: TOP + weekday * STEP,
|
||||
value,
|
||||
level: levelOf(value)
|
||||
});
|
||||
}
|
||||
|
||||
// 每月第一次出现的列打上月份标签
|
||||
let lastMonth = "";
|
||||
for (let week = 0; week < weeks; week++) {
|
||||
const monday = new Date(start + week * 7 * DAY_MS);
|
||||
const month = monthFormatter.format(monday);
|
||||
if (month !== lastMonth) {
|
||||
monthLabels.push({x: LEFT + week * STEP, label: month});
|
||||
lastMonth = month;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const width = weeks > 0 ? LEFT + (weeks - 1) * STEP + CELL + 2 : 0;
|
||||
const height = TOP + 7 * STEP - GAP + 2;
|
||||
const levels = [0, 1, 2, 3, 4];
|
||||
---
|
||||
|
||||
{
|
||||
firstKey ? (
|
||||
<section class="heatmap">
|
||||
<div class="heatmap__head">
|
||||
<span class="heatmap__range">{toDateText(firstDate)} - {toDateText(lastDate)}</span>
|
||||
<span class="heatmap__total">共 <strong>{total}</strong> 次贡献</span>
|
||||
</div>
|
||||
|
||||
<div class="heatmap__scroll" data-heatmap-scroll>
|
||||
<svg
|
||||
width={width}
|
||||
height={height}
|
||||
viewBox={`0 0 ${width} ${height}`}
|
||||
role="img"
|
||||
aria-label={`代码贡献热力图,${toDateText(firstDate)} 至 ${toDateText(lastDate)},共 ${total} 次贡献`}
|
||||
>
|
||||
{monthLabels.map(({x, label}) => (
|
||||
<text class="heatmap__month" x={x} y={10}>{label}</text>
|
||||
))}
|
||||
{weekdayLabels.map(({row, label}) => (
|
||||
<text class="heatmap__weekday" x={0} y={TOP + row * STEP + CELL - 2}>{label}</text>
|
||||
))}
|
||||
{cells.map((cell) => (
|
||||
<rect
|
||||
class={`heatmap__cell heatmap__cell--${cell.level}`}
|
||||
x={cell.x}
|
||||
y={cell.y}
|
||||
width={CELL}
|
||||
height={CELL}
|
||||
rx={2.5}
|
||||
>
|
||||
<title>{cell.key} · {cell.value > 0 ? `${cell.value} 次贡献` : "无贡献"}</title>
|
||||
</rect>
|
||||
))}
|
||||
</svg>
|
||||
</div>
|
||||
|
||||
<div class="heatmap__foot">
|
||||
<div class="heatmap__legend" aria-hidden="true">
|
||||
<span>少</span>
|
||||
{levels.map((level) => <span class={`heatmap__legend-cell heatmap__cell--${level}`}></span>)}
|
||||
<span>多</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
) : (
|
||||
<p class="heatmap__empty">暂无贡献数据</p>
|
||||
)
|
||||
}
|
||||
|
||||
<style>
|
||||
.heatmap {
|
||||
padding: 14px 20px 16px;
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary, #6b7280);
|
||||
}
|
||||
|
||||
.heatmap__head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
gap: 12px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.heatmap__range {
|
||||
font-variant-numeric: tabular-nums;
|
||||
letter-spacing: 0.2px;
|
||||
}
|
||||
|
||||
.heatmap__total strong {
|
||||
font-weight: 600;
|
||||
color: var(--text-color, #2b2f36);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.heatmap__scroll {
|
||||
overflow-x: auto;
|
||||
overflow-y: hidden;
|
||||
padding-bottom: 4px;
|
||||
scrollbar-width: thin;
|
||||
}
|
||||
|
||||
.heatmap__scroll svg {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.heatmap__cell {
|
||||
fill: var(--hm-color, #eceff4);
|
||||
shape-rendering: geometricPrecision;
|
||||
transition: fill 0.12s ease;
|
||||
}
|
||||
|
||||
.heatmap__cell:hover {
|
||||
stroke: var(--text-color, #2b2f36);
|
||||
stroke-width: 1;
|
||||
}
|
||||
|
||||
.heatmap__cell--0 { --hm-color: #eceff4; }
|
||||
.heatmap__cell--1 { --hm-color: #cdd8fb; }
|
||||
.heatmap__cell--2 { --hm-color: #9fb2f7; }
|
||||
.heatmap__cell--3 { --hm-color: #6b84f0; }
|
||||
.heatmap__cell--4 { --hm-color: #3f5be0; }
|
||||
|
||||
.heatmap__month,
|
||||
.heatmap__weekday {
|
||||
fill: var(--text-secondary, #6b7280);
|
||||
font-size: 10px;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.heatmap__weekday {
|
||||
text-anchor: start;
|
||||
}
|
||||
|
||||
.heatmap__foot {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.heatmap__legend {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.heatmap__legend-cell {
|
||||
display: inline-block;
|
||||
width: 11px;
|
||||
height: 11px;
|
||||
border-radius: 2.5px;
|
||||
background: var(--hm-color, #eceff4);
|
||||
}
|
||||
|
||||
.heatmap__empty {
|
||||
margin: 0;
|
||||
padding: 24px 20px;
|
||||
font-size: 13px;
|
||||
text-align: center;
|
||||
color: var(--text-secondary, #6b7280);
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.heatmap {
|
||||
padding: 12px 14px 14px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
// 数据从最早一天排到最新一天,窄屏下默认展示最近的部分
|
||||
document.querySelectorAll<HTMLElement>("[data-heatmap-scroll]").forEach((scroller) => {
|
||||
if (scroller.scrollWidth > scroller.clientWidth) {
|
||||
scroller.scrollLeft = scroller.scrollWidth;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user