官网

https://github.com/Bloggify/github-calendar

解答

https://stackoverflow.com/questions/34516592/embed-github-contributions-graph-in-website

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{% raw %}

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/IonicaBizau/github-calendar/dist/github-calendar.css"/>

<script src="https://cdn.jsdelivr.net/gh/IonicaBizau/github-calendar/dist/github-calendar.min.js"></script>

<!-- Optionally, include the theme (if you don't want to struggle to write the CSS) -->


<!-- Prepare a container for your calendar. -->

<div class="calendar">
<!-- Loading stuff -->
Loading the data just for you.
</div>


<script>
new GitHubCalendar(".calendar", "weilining");
</script>

{% endraw %}

效果

Loading the data just for you.

官网

https://github.com/Zfour/Butterfly-gitcalendar

代码

未开pjax

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
{% raw %}
<div class="recent-post-item" style="width: 100%">
<div class="calendar" id="calendar">
<div
id="gitmessage"
:style="{top:y+px,left:x+px,position: fixed,zIndex:9999}"
>
<div class="angle-wrapper">
<span>{{span1}} &nbsp;</span><span>{{span2}} 次上传</span>
</div>
</div>
<div class="position-relative">
<div class="border py-2 graph-before-activity-overview">
<div
class="js-calendar-graph mx-md-2 mx-3 d-flex flex-column flex-items-end flex-xl-items-center overflow-hidden pt-1 is-graph-loading graph-canvas calendar-graph height-full text-center"
data-graph-url="/users/Zfour/contributions?to=2020-10-29"
data-url="/Zfour"
data-from="2019-10-27 00:00:00 UTC"
data-to="2020-10-29 23:59:59 UTC"
data-org="data-org"
>
<div id="calendarcanvasbox" v-if="simplemode">
<canvas id="gitcanvas" style="animation: none"></canvas>
</div>
<svg
class="js-calendar-graph-svg"
width="100%"
viewBox="0 0 770 128"
v-if="!simplemode"
>
<text
class="month"
:x="32 + monthindex*64"
y="20"
v-for="(month,monthindex) in monthchange"
>
{{month}}
</text>
<text class="wday" text-anchor="start" dx="0" dy="40"></text>
<text class="wday" text-anchor="start" dx="0" dy="65"></text>
<text class="wday" text-anchor="start" dx="0" dy="90"></text>
<text class="wday" text-anchor="start" dx="0" dy="115"></text>
<g
v-for="(weekitem,weekIndex) in data"
:transform="'translate('+ (16 + weekIndex*14) + ',' + '0)'"
>
<rect
@mouseover="selectStyle(dayitem,$event)"
@mouseleave="outStyle()"
v-for="(dayitem,dayIndex) in weekitem"
:style="{fill:thiscolor(dayitem.count),shapeRendering:crispedges}"
:data-score="dayitem.count"
:data-date="dayitem.date"
x="0"
:y=" 30 + dayIndex*13 "
width="11"
height="11"
></rect>
</g>
</svg>
</div>
<div class="contrib-footer clearfix mt-1 mx-3 px-3 pb-1">
<div class="float-left text-gray">
数据来源
<a :href="'https://github.com/'+ user " target="blank">@{{user}}</a>
</div>
<div
class="contrib-legend text-gray"
title="A summary of pull requests, issues opened, and commits to the default and gh-pages branches."
>
Less
<ul class="legend">
<li :style="{backgroundColor:color[0]}"></li>
<li :style="{backgroundColor:color[2]}"></li>
<li :style="{backgroundColor:color[4]}"></li>
<li :style="{backgroundColor:color[6]}"></li>
<li :style="{backgroundColor:color[8]}"></li>
</ul>
More
</div>
</div>
</div>
</div>
<div class="contrib-column contrib-column-first table-column">
<span class="text-muted">过去一年提交</span
><span class="contrib-number">{{total}}</span
><span class="text-muted"
>{{oneyearbeforeday}}&nbsp;-&nbsp;{{thisday}}</span
>
</div>
<div class="contrib-column table-column">
<span class="text-muted">最近一月提交</span
><span class="contrib-number">{{thisweekdatacore}}</span
><span class="text-muted">{{amonthago}}&nbsp;-&nbsp;{{thisday}}</span>
</div>
<div class="contrib-column table-column">
<span class="text-muted">最近一周提交</span
><span class="contrib-number">{{weekdatacore}}</span
><span class="text-muted">{{aweekago}}&nbsp;-&nbsp;{{thisday}}</span>
</div>
</div>
</div>


<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-gitcalendar/gitcalendar/css/gitcalendar.css" media="defer" onload="this.media='all'">
<script defer src="https://rmt.dogedoge.com/fetch/~/source/jsdelivr/npm/jquery@latest/dist/jquery.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
<script defer data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-gitcalendar/gitcalendar/js/gitcalendar.js"></script>

{% endraw %}

开pjax

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
var calendar = new Vue({
el: "#calendar",
data: {
simplemode: true, //打开时使用canvas绘制gitcalendar,关闭时使用svg绘制gitcalendar
//canvas:dom数少,但图像会发生模糊,自适应一般 svg:dom数多,图像清晰,自适应更佳

user: "weilining", //这里填写你的github用户名

fixed: "fixed",
px: "px",
x: "",
y: "",
span1: "",
span2: "",
month: [
"一月",
"二月",
"三月",
"四月",
"五月",
"六月",
"七月",
"八月",
"九月",
"十月",
"十一月",
"十二月",
],
monthchange: [],
oneyearbeforeday: "",
thisday: "",
amonthago: "",
aweekago: "",
weekdatacore: 0,
datacore: 0,
total: 0,
datadate: "",
data: [],
positionplusdata: [],
firstweek: [],
lastweek: [],
beforeweek: [],
thisweekdatacore: 0,
mounthbeforeday: 0,
mounthfirstindex: 0,
crispedges: "crispedges",
thisdayindex: 0,
amonthagoindex: 0,
amonthagoweek: [],
firstdate: [],
first2date: [],
montharrbefore: [],
monthindex: 0,
purple: [
"#ebedf0",
"#fdcdec",
"#fc9bd9",
"#fa6ac5",
"#f838b2",
"#f5089f",
"#c4067e",
"#92055e",
"#540336",
"#48022f",
"#30021f",
],
green: [
"#ebedf0",
"#f0fff4",
"#dcffe4",
"#bef5cb",
"#85e89d",
"#34d058",
"#28a745",
"#22863a",
"#176f2c",
"#165c26",
"#144620",
],
blue: [
"#ebedf0",
"#f1f8ff",
"#dbedff",
"#c8e1ff",
"#79b8ff",
"#2188ff",
"#0366d6",
"#005cc5",
"#044289",
"#032f62",
"#05264c",
],
color: [
"#ebedf0",
"#fdcdec",
"#fc9bd9",
"#fa6ac5",
"#f838b2",
"#f5089f",
"#c4067e",
"#92055e",
"#540336",
"#48022f",
"#30021f",
],
},
methods: {
selectStyle(data, event) {
$(".angle-wrapper").show();
this.span1 = data.date;
this.span2 = data.count;
this.x = event.clientX - 100;
this.y = event.clientY - 60;
},
outStyle() {
$(".angle-wrapper").hide();
},
thiscolor(x) {
if (x === 0) {
var i = parseInt(x / 2);
return this.color[0];
} else if (x < 2) {
return this.color[1];
} else if (x < 20) {
var i = parseInt(x / 2);
return this.color[i];
} else {
return this.color[9];
}
},
},
});
var githubapiurl = "https://githubapi.ryanchristian.dev/user/" + calendar.user;
$(function () {
$.ajax({
type: "GET",
url: githubapiurl,
dataType: "json",
success: function (data) {
calendar.data = data.contributions;
calendar.total = data.total;
calendar.first2date = calendar.data[48];
calendar.firstdate = calendar.data[47];
calendar.firstweek = data.contributions[0];
calendar.lastweek = data.contributions[52];
calendar.beforeweek = data.contributions[51];
calendar.thisdayindex = calendar.lastweek.length - 1;
calendar.thisday = calendar.lastweek[calendar.thisdayindex].date;
calendar.oneyearbeforeday = calendar.firstweek[0].date;
calendar.monthindex = calendar.thisday.substring(5, 7) * 1;
calendar.montharrbefore = calendar.month.splice(
calendar.monthindex,
12 - calendar.monthindex
);
calendar.monthchange = calendar.montharrbefore.concat(calendar.month);
addweek();
addlastmonth();

function responsiveChart() {
var c = document.getElementById("gitcanvas");
var cmessage = document.getElementById("gitmessage");
var ctx = c.getContext("2d");
c.width = document.getElementById("calendarcanvasbox").offsetWidth;
var linemaxwitdh = (0.96 * c.width) / calendar.data.length;
c.height = 9 * linemaxwitdh;
var lineminwitdh = 0.8 * linemaxwitdh;
var setposition = {
x: 0.02 * c.width,
y: 0.025 * c.width,
};
for (var week in calendar.data) {
weekdata = calendar.data[week];
for (var day in weekdata) {
var dataitem = { date: "", count: "", x: 0, y: 0 };
calendar.positionplusdata.push(dataitem);
ctx.fillStyle = calendar.thiscolor(weekdata[day].count);
setposition.y = Math.round(setposition.y * 100) / 100;
dataitem.date = weekdata[day].date;
dataitem.count = weekdata[day].count;
dataitem.x = setposition.x;
dataitem.y = setposition.y;
ctx.fillRect(
setposition.x,
setposition.y,
lineminwitdh,
lineminwitdh
);
setposition.y = setposition.y + linemaxwitdh;
}
setposition.y = 0.025 * c.width;
setposition.x = setposition.x + linemaxwitdh;
}
ctx.font = "600 Arial";
ctx.fillStyle = "#aaa";
ctx.fillText("日", 0, 1.9 * linemaxwitdh);
ctx.fillText("二", 0, 3.9 * linemaxwitdh);
ctx.fillText("四", 0, 5.9 * linemaxwitdh);
ctx.fillText("六", 0, 7.9 * linemaxwitdh);
var monthindexlist = c.width / 24;
for (var index in calendar.monthchange) {
ctx.fillText(
calendar.monthchange[index],
monthindexlist,
0.7 * linemaxwitdh
);
monthindexlist = monthindexlist + c.width / 12;
}
cmessage.onmousemove = function (event) {
$(".angle-wrapper").hide();
};
c.onmousemove = function (event) {
$(".angle-wrapper").hide();
getMousePos(c, event);
};

function getMousePos(canvas, event) {
var rect = canvas.getBoundingClientRect();
var x = event.clientX - rect.left * (canvas.width / rect.width);
var y = event.clientY - rect.top * (canvas.height / rect.height);
//console.log("x:"+x+",y:"+y);
for (var item of calendar.positionplusdata) {
var lenthx = x - item.x;
var lenthy = y - item.y;
//console.log(lenthx,lenthy);
if (0 < lenthx && lenthx < lineminwitdh) {
if (0 < lenthy && lenthy < lineminwitdh) {
//console.log(item.date,item.count)
$(".angle-wrapper").show();
calendar.span1 = item.date;
calendar.span2 = item.count;
calendar.x = event.clientX - 100;
calendar.y = event.clientY - 60;
}
}
//if(0< x - item.x <lineminwitdh&&0< y - item.y <lineminwitdh){
//console.log(item.count,item.date);
//}
}
}
}

responsiveChart();
$(window).on("resize", responsiveChart);
window.onscroll = function () {
$(".angle-wrapper").hide();
};
console.log(calendar.positionplusdata);

function addlastmonth() {
if (calendar.thisdayindex === 0) {
thisweekcore(52);
thisweekcore(51);
thisweekcore(50);
thisweekcore(49);
thisweekcore(48);
calendar.thisweekdatacore += calendar.firstdate[6].count;
calendar.amonthago = calendar.firstdate[6].date;
} else {
thisweekcore(52);
thisweekcore(51);
thisweekcore(50);
thisweekcore(49);
thisweek2core();
calendar.amonthago =
calendar.first2date[calendar.thisdayindex - 1].date;
}
}

function thisweek2core() {
for (
var i = calendar.thisdayindex - 1;
i < calendar.first2date.length;
i++
) {
calendar.thisweekdatacore += calendar.first2date[i].count;
}
}

function thisweekcore(index) {
for (var item of calendar.data[index]) {
calendar.thisweekdatacore += item.count;
}
}

function addlastweek() {
for (var item of calendar.lastweek) {
calendar.weekdatacore += item.count;
}
}

function addbeforeweek() {
for (
var i = calendar.thisdayindex;
i < calendar.beforeweek.length;
i++
) {
calendar.weekdatacore += calendar.beforeweek[i].count;
}
}

function addweek() {
if (calendar.thisdayindex === 6) {
calendar.aweekago = calendar.lastweek[0].date;
addlastweek();
} else {
lastweek = data.contributions[51];
calendar.aweekago = lastweek[calendar.thisdayindex + 1].date;
addlastweek();
addbeforeweek();
}
}
},
});
});
if (document.getElementById("calendarcanvasbox").offsetWidth < 500) {
calendar.simplemode = false;
}

效果

{{span1}}  {{span2}} 次上传
{{month}}
过去一年提交{{total}}{{oneyearbeforeday}} - {{thisday}}
最近一月提交{{thisweekdatacore}}{{amonthago}} - {{thisday}}
最近一周提交{{weekdatacore}}{{aweekago}} - {{thisday}}