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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
| <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8"/>
<title>GitHub 风格提交日历 -- 过去一年</title>
<style>
#app {
font-family: Arial, sans-serif;
/*max-width: 900px;*/
margin: 20px auto;
}
.calendar {
display: flex;
gap: 4px;
user-select: none;
}
.week {
display: flex;
flex-direction: column;
gap: 4px;
}
.day {
width: var(--day-width);
aspect-ratio: 1/1;
border-radius: 3px;
cursor: default;
transition: background-color 0.3s ease;
/* 默认浅灰 */
background-color: #ebedf0;
position: relative;
}
.day.empty {
background-color: transparent;
cursor: default;
}
.day.in-year:after {
transition: opacity 0.2s ease;
content: attr(data-tip);
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.75);
color: white;
padding: 3px 6px;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
pointer-events: none;
z-index: 10;
opacity: 0;
}
.day.in-year:hover::after {
opacity: 1;
}
/* 顶部月份行 */
.months {
display: flex;
gap: 4px;
margin-bottom: 4px;
}
.month-label {
font-size: 12px;
color: #555;
text-align: center;
user-select: none;
line-height: 14px;
/* 宽度与日格宽度加间隙对应 */
display: inline-block;
}
/* Legend */
.legend {
display: flex;
align-items: center;
margin-top: 12px;
gap: 4px;
font-size: 12px;
color: #555;
}
.legend-color {
width: 14px;
height: 14px;
border-radius: 3px;
background-color: #ebedf0;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<div id="app">
<submit-calender :start-date="beginDate1" :end-date="endDate1"></submit-calender>
<submit-calender :start-date="beginDate2" :end-date="endDate2"></submit-calender>
</div>
<!-- Vue3 + Dayjs -->
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/3.2.31/vue.global.min.js"></script>
<script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/dayjs/1.10.8/dayjs.min.js"></script>
<script>
const {
createApp,
reactive,
computed
} = Vue;
// JS setup中新增代替插件的判断函数
function isSameOrBefore(d1, d2) {
return d1.isBefore(d2) || d1.isSame(d2);
}
function isSameOrAfter(d1, d2) {
return d1.isAfter(d2) || d1.isSame(d2);
}
const d = dayjs()
d.__proto__.isSameOrBefore = function (rhs) {
return isSameOrBefore(this, rhs)
}
d.__proto__.isSameOrAfter = function (rhs) {
return isSameOrAfter(this, rhs)
}
const SubmitCalender = {
props: ['endDate', 'startDate', 'dayWidth', 'gap'],
setup(props) {
const dayWidth = Number(props.dayWidth || 15);
const gap = Number(props.gap || 4);
// 结束日期是今天
const endDate = dayjs(props.endDate).startOf('day');
// 365天前的日期
const startDate = dayjs(props.startDate).startOf('day')
// 模拟随机提交数据,仅针对时间范围内
const contributionsData = reactive({});
let dateCursor = startDate;
while (dateCursor.isBefore(endDate) || dateCursor.isSame(endDate)) {
const key = dateCursor.format('YYYY-MM-DD');
// 激活数据,模拟0~10次提交
contributionsData[key] = Math.floor(Math.random() * 10);
dateCursor = dateCursor.add(1, 'day');
}
// 计算日历开始:startDate最早所在周的周日(前推到周日开始)
let calendarStart = startDate;
while (calendarStart.day() !== 0) {
calendarStart = calendarStart.subtract(1, 'day');
}
// 计算日历结束:endDate所在周的周六(后推到周六结束)
let calendarEnd = endDate;
while (calendarEnd.day() !== 6) {
calendarEnd = calendarEnd.add(1, 'day');
}
const weeksCount = Math.ceil(calendarEnd.diff(calendarStart, 'week', true));
// 构造weeks数组,每周列,7天一列,包含当天日期,是否在范围内标记inRange
const weeks = computed(() => {
const arr = [];
for (let w = 0; w <= weeksCount; w++) {
const week = [];
for (let d = 0; d < 7; d++) {
const date = calendarStart.add(w, 'week').add(d, 'day');
const dateStr = date.format('YYYY-MM-DD');
const inRange = date.isSameOrAfter(startDate) && date.isSameOrBefore(endDate);
const count = inRange ? (contributionsData[dateStr] ?? 0) : 0;
week.push({
date: dateStr,
count,
inRange
});
}
arr.push(week);
}
return arr;
});
// 颜色等级 - GitHub样式
const colorScale = [
'#ebedf0',
'#c6e48b',
'#7bc96f',
'#239a3b',
'#196127'
];
function getColor(count) {
if (count <= 0) return colorScale[0];
if (count <= 2) return colorScale[1];
if (count <= 4) return colorScale[2];
if (count <= 7) return colorScale[3];
return colorScale[4];
}
function formatTooltip(day) {
const d = dayjs(day.date);
return `${d.year()}年${d.month() + 1}月${d.date()}日: ${day.count} 次提交`;
}
// 计算月份左对齐显示
// 思路:找calendarStart后的每周第一天,根据其所属月份确定标签起点(第几周)
const months = computed(() => {
// 取每周第一天的日期列表
const weekFirstDays = [];
for (let w = 0; w <= weeksCount; w++) {
weekFirstDays.push(calendarStart.add(w, 'week'));
}
// 找出月份中第一个“显示在calendar里的日期”的周索引
// 遍历过去一年内出现的月份(起始至结束)
const monthInfos = [];
let cursor = dayjs(startDate);
let lastMonth = null;
const seenMonths = new Set();
while (cursor.isSameOrBefore(endDate)) {
const monthStr = cursor.format('YYYY-MM');
if (!seenMonths.has(monthStr)) {
// 找这个月第一天在calendar中的周索引
const firstDayOfMonth = dayjs(monthStr + '-01');
// 先确保首日不早于calendarStart,否则用calendarStart
let labelDate = firstDayOfMonth.isBefore(calendarStart) ? calendarStart : firstDayOfMonth;
// 只考虑labelDate在范围内
if (labelDate.isAfter(calendarEnd)) break;
// 找周索引
let startWeek = 0;
for (let i = 0; i < weekFirstDays.length; i++) {
const weekStart = weekFirstDays[i];
const weekEnd = weekFirstDays[i + 1] ? weekFirstDays[i + 1].subtract(1, 'day') : calendarEnd;
if (labelDate.isSameOrAfter(weekStart) && labelDate.isSameOrBefore(weekEnd)) {
startWeek = i;
break;
}
}
monthInfos.push({
name: labelDate.format('M月'),
startWeek
});
seenMonths.add(monthStr);
}
cursor = cursor.add(1, 'month').startOf('month');
}
return monthInfos;
});
return {
startDate,
endDate,
weeks,
getColor,
colorScale,
formatTooltip,
months,
dayWidth,
gap
};
},
template: `
<div style="width: fit-content">
<div class="months" style="position: relative; height: 20px;">
<div v-for="month in months" :key="month.name"
class="month-label"
:style="{ position: 'absolute', left: (month.startWeek * (dayWidth + gap)) + 'px', }">
{{ month.name }}
</div>
</div>
<div class="calendar" :style="{'--day-width': dayWidth + 'px'}" style="width: fit-content">
<div v-for="(week, wIndex) in weeks" :key="wIndex" class="week">
<div v-for="day in week" :key="day.date"
:class="['day', {'empty': !day.inRange}, {'in-year': day.inRange}]"
:style="{backgroundColor: day.inRange ? getColor(day.count) : 'transparent', }"
:data-tip="day.inRange ? formatTooltip(day) : null">
</div>
</div>
</div>
<div style="display: flex; justify-content: right">
<div class="legend" style="margin-top: 16px;">
<span>少</span>
<span class="legend-color" :style="{backgroundColor: colorScale[0]}"></span>
<span class="legend-color" :style="{backgroundColor: colorScale[1]}"></span>
<span class="legend-color" :style="{backgroundColor: colorScale[2]}"></span>
<span class="legend-color" :style="{backgroundColor: colorScale[3]}"></span>
<span class="legend-color" :style="{backgroundColor: colorScale[4]}"></span>
<span>多</span>
</div>
</div>
</div>`
}
createApp({
components: {
SubmitCalender
},
setup() {
const now = dayjs()
const endDate1 = now.startOf('day')
const beginDate1 = endDate1.subtract(364, 'day').startOf('day')
const endDate2 = now.startOf('year').subtract(-364, 'day')
const beginDate2 = now.startOf('year')
return {
endDate1,
beginDate1,
endDate2,
beginDate2
}
}
}).mount('#app');
</script>
</body>
</html>
|