PageSourceSearch

https://coderwei99.github.io/gitcalendar/js/gitcalendar.js

js coderwei99.github.io collected 2026-10-03 08:49:00 UTC 9,550 bytes, 244 lines download raw bytes

1const gitcalendar = new Vue({
2    el: '#gitcalendar',
3    data: {
4        simplemode: false, //打开时使用canvas绘制gitgitcalendar,关闭时使用svg绘制gitgitcalendar
5        //canvas:dom数少,但图像会发生模糊,自适应一般  svg:dom数多,图像清晰,自适应更佳
6
7        user: 'coderwei99', //这里填写你的github用户名
8
9        fixed: 'fixed',
10        px: 'px',
11        x: '',
12        y: '',
13        span1: '',
14        span2: '',
15        month: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
16        monthchange: [],
17        oneyearbeforeday: '',
18        thisday: '',
19        amonthago: '',
20        aweekago: '',
21        weekdatacore: 0,
22        datacore: 0,
23        total: 0,
24        datadate: '',
25        data: [],
26        positionplusdata: [],
27        firstweek: [],
28        lastweek: [],
29        beforeweek: [],
30        thisweekdatacore: 0,
31        mounthbeforeday: 0,
32        mounthfirstindex: 0,
33        crispedges: 'crispedges',
34        thisdayindex: 0,
35        amonthagoindex: 0,
36        amonthagoweek: [],
37        firstdate: [],
38        first2date: [],
39        montharrbefore: [],
40        monthindex: 0,
41        // purple: ['#ebedf0', '#fdcdec', '#fc9bd9', '#fa6ac5', '#f838b2', '#f5089f', '#c4067e', '#92055e', '#540336', '#48022f', '#30021f',],
42        color: ['#ebedf0', '#85e89d', '#34d058', '#28a745', '#22863a', '#176f2c', '#165c26', '#144620'],
43        // blue: ['#ebedf0', '#f1f8ff', '#dbedff', '#c8e1ff', '#79b8ff', '#2188ff', '#0366d6', '#005cc5', '#044289', '#032f62', '#05264c',],
44        // color: ['#ebedf0', '#fdcdec', '#fc9bd9', '#fa6ac5', '#f838b2', '#f5089f', '#c4067e', '#92055e', '#540336', '#48022f', '#30021f',],
45        // color: ['#ebedf0', '#f1f8ff', '#dbedff', '#c8e1ff', '#79b8ff', '#2188ff', '#0366d6', '#005cc5', '#044289', '#032f62', '#05264c']
46    },
47    methods: {
48        selectStyle(data, event) {
49            document.querySelector('.angle-wrapper').style.display = 'block'
50            this.span1 = data.date;
51            this.span2 = data.count;
52            this.x = event.clientX - 100;
53            this.y = event.clientY - 60
54        },
55        outStyle() {
56            document.querySelector('.angle-wrapper').style.display = 'none'
57        },
58        thiscolor(x) {
59            if (x === 0) {
60                let i = parseInt(x / 2);
61                return this.color[0]
62            } else if (x < 2) {
63                return this.color[1]
64            } else if (x < 20) {
65                let i = parseInt(x / 2);
66                return this.color[i]
67            } else {
68                return this.color[9]
69            }
70        },
71    }
72});
73
74let githubapiurl = "https://python-github-calendar-api.vercel.app/api?" + gitcalendar.user;
75//let githubapiurl = "https://githubapi.ryanchristian.dev/user/" + gitcalendar.user;  
76//旧的api策略
77
78//canvas绘图
79function responsiveChart() {
80    if (gitcalendar.simplemode) {
81        let c = document.getElementById("gitcanvas");
82        let cmessage = document.getElementById("gitmessage");
83        let ctx = c.getContext("2d");
84        c.width = document.getElementById("gitcalendarcanvasbox").offsetWidth;
85        let linemaxwitdh = 0.96 * c.width / gitcalendar.data.length;
86        c.height = 9 * linemaxwitdh;
87        let lineminwitdh = 0.8 * linemaxwitdh;
88        let setposition = {
89            x: 0.02 * c.width,
90            y: 0.025 * c.width
91        };
92        for (let week in gitcalendar.data) {
93            weekdata = gitcalendar.data[week];
94            for (let day in weekdata) {
95                let dataitem = { date: "", count: "", x: 0, y: 0 };
96                gitcalendar.positionplusdata.push(dataitem);
97                ctx.fillStyle = gitcalendar.thiscolor(weekdata[day].count);
98                setposition.y = Math.round(setposition.y * 100) / 100;
99                dataitem.date = weekdata[day].date;
100                dataitem.count = weekdata[day].count;
101                dataitem.x = setposition.x;
102                dataitem.y = setposition.y;
103                ctx.fillRect(setposition.x, setposition.y, lineminwitdh, lineminwitdh);
104                setposition.y = setposition.y + linemaxwitdh
105            }
106            ;
107            setposition.y = 0.025 * c.width;
108            setposition.x = setposition.x + linemaxwitdh
109        }
110        ;
111        ctx.font = "600  Arial";
112        ctx.fillStyle = '#aaa';
113        ctx.fillText("日", 0, 1.9 * linemaxwitdh);
114        ctx.fillText("二", 0, 3.9 * linemaxwitdh);
115        ctx.fillText("四", 0, 5.9 * linemaxwitdh);
116        ctx.fillText("六", 0, 7.9 * linemaxwitdh);
117        let monthindexlist = c.width / 24;
118        for (let index in gitcalendar.monthchange) {
119            ctx.fillText(gitcalendar.monthchange[index], monthindexlist, 0.7 * linemaxwitdh);
120            monthindexlist = monthindexlist + c.width / 12
121        }
122        ;
123        cmessage.onmousemove = function (event) {
124            document.querySelector('.angle-wrapper').style.display = 'none'
125        };
126        c.onmousemove = function (event) {
127            document.querySelector('.angle-wrapper').style.display = 'none'
128            getMousePos(c, event);
129        };
130
131        function getMousePos(canvas, event) {
132            var rect = canvas.getBoundingClientRect();
133            var x = event.clientX - rect.left * (canvas.width / rect.width);
134            var y = event.clientY - rect.top * (canvas.height / rect.height);
135            //console.log("x:"+x+",y:"+y);
136            for (let item of gitcalendar.positionplusdata) {
137                let lenthx = x - item.x;
138                let lenthy = y - item.y;
139                //console.log(lenthx,lenthy);
140                if (0 < lenthx && lenthx < lineminwitdh) {
141                    if (0 < lenthy && lenthy < lineminwitdh) {
142                        //console.log(item.date,item.count)
143                        document.querySelector('.angle-wrapper').style.display = 'block'
144                        gitcalendar.span1 = item.date;
145                        gitcalendar.span2 = item.count;
146                        gitcalendar.x = event.clientX - 100;
147                        gitcalendar.y = event.clientY - 60
148                    }
149                }
150                //if(0< x - item.x <lineminwitdh&&0< y - item.y <lineminwitdh){
151                //console.log(item.count,item.date);
152                //}
153            }
154        }
155    }
156}
157
158//数据统计算法
159function addlastmonth() {
160    if (gitcalendar.thisdayindex === 0) {
161        thisweekcore(52);
162        thisweekcore(51);
163        thisweekcore(50);
164        thisweekcore(49);
165        thisweekcore(48);
166        gitcalendar.thisweekdatacore += gitcalendar.firstdate[6].count;
167        gitcalendar.amonthago = gitcalendar.firstdate[6].date
168    } else {
169        thisweekcore(52);
170        thisweekcore(51);
171        thisweekcore(50);
172        thisweekcore(49);
173        thisweek2core();
174        gitcalendar.amonthago = gitcalendar.first2date[gitcalendar.thisdayindex - 1].date
175    }
176};
177function thisweek2core() {
178    for (let i = gitcalendar.thisdayindex - 1; i < gitcalendar.first2date.length; i++) {
179        gitcalendar.thisweekdatacore += gitcalendar.first2date[i].count
180    }
181};
182function thisweekcore(index) {
183    for (let item of gitcalendar.data[index]) {
184        gitcalendar.thisweekdatacore += item.count
185    }
186};
187function addlastweek() {
188    for (let item of gitcalendar.lastweek) {
189        gitcalendar.weekdatacore += item.count
190    }
191};
192function addbeforeweek() {
193    for (let i = gitcalendar.thisdayindex; i < gitcalendar.beforeweek.length; i++) {
194        gitcalendar.weekdatacore += gitcalendar.beforeweek[i].count
195    }
196};
197function addweek(data) {
198    if (gitcalendar.thisdayindex === 6) {
199        gitcalendar.aweekago = gitcalendar.lastweek[0].date;
200        addlastweek()
201    } else {
202        lastweek = data.contributions[51];
203        gitcalendar.aweekago = lastweek[gitcalendar.thisdayindex + 1].date;
204        addlastweek();
205        addbeforeweek()
206    }
207}
208
209fetch(githubapiurl)
210    .then(data => data.json())
211    .then(data => {
212        gitcalendar.data = data.contributions;
213        gitcalendar.total = data.total;
214        gitcalendar.first2date = gitcalendar.data[48];
215        gitcalendar.firstdate = gitcalendar.data[47];
216        gitcalendar.firstweek = data.contributions[0];
217        gitcalendar.lastweek = data.contributions[52];
218        gitcalendar.beforeweek = data.contributions[51];
219        gitcalendar.thisdayindex = gitcalendar.lastweek.length - 1;
220        gitcalendar.thisday = gitcalendar.lastweek[gitcalendar.thisdayindex].date;
221        gitcalendar.oneyearbeforeday = gitcalendar.firstweek[0].date;
222        gitcalendar.monthindex = gitcalendar.thisday.substring(5, 7) * 1;
223        gitcalendar.montharrbefore = gitcalendar.month.splice(gitcalendar.monthindex, 12 - gitcalendar.monthindex);
224        gitcalendar.monthchange = gitcalendar.montharrbefore.concat(gitcalendar.month);
225        addweek(data);
226        addlastmonth();
227        responsiveChart();
228    })
229    .catch(function (error) {
230        console.log(error);
231    });
232
233//手机版更换为svg绘制
234if (document.getElementById("gitcalendarcanvasbox").offsetWidth < 500) { gitcalendar.simplemode = false }
235
236//当改变窗口大小时重新绘制canvas
237window.onresize = function () {
238    responsiveChart();
239}
240
241//解决滚动滑轮时出现的标签显示
242window.onscroll = function () {
243    if (document.querySelector('.angle-wrapper')) { document.querySelector('.angle-wrapper').style.display = 'none' }
244};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.