File tree

11 files changed

+97
-589
lines changed

11 files changed

+97
-589
lines changed
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@
4040
### 做题记录
4141

4242
![](https://leetcode-badge.haozibi.dev/v1cn/chart/submission-calendar/haozibi.svg)
43-
- LeetCodeCN: `https://leetcode-badge.haozibi.dev/v1cn/chart/submission-calendar/{LeetCode_ID}.svg`
43+
- LeetCodeCN: `https://leetcode-badge.haozibi.dev/v1cn/chart/submission-calendar/{LeetCode_ID}.svg?type=past-year&color=yellow`
4444

4545
参数
4646

47-
- `?type=last-year`: 过去一年
48-
- `?`: 默认为 今年
47+
- type: 范围
48+
- 默认: 今年
49+
- past-year: 过去一年
50+
- color: 颜色
51+
- green(默认)
52+
- yellow
53+
- blue
4954

5055
注意事项:
5156

Original file line numberDiff line numberDiff line change
@@ -154,11 +154,14 @@ func (a *APP) getSubCal(name string, r *http.Request) ([]byte, error) {
154154
err error
155155
)
156156

157-
var f func(data map[int64]int) ([]byte, error)
157+
var f func(data map[int64]int, color string) ([]byte, error)
158+
158159
t := r.URL.Query().Get("type")
160+
color := r.URL.Query().Get("color")
161+
159162
switch t {
160-
case "last-year":
161-
f = heatmap.LastYear
163+
case "past-year":
164+
f = heatmap.PastYear
162165
default:
163166
f = heatmap.CurrYear
164167
}
@@ -191,7 +194,7 @@ func (a *APP) getSubCal(name string, r *http.Request) ([]byte, error) {
191194
res[i] = v
192195
}
193196

194-
body, err = f(res)
197+
body, err = f(res, color)
195198
if err != nil {
196199
return nil, err
197200
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package card
2+
3+
func Build() ([]byte, error) {
4+
return nil, nil
5+
}
6+
7+
type Info struct {
8+
Easy int
9+
EasyTotal int
10+
Media int
11+
MediaTotal int
12+
Hard int
13+
HardTotal int
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package card
2+
3+
import "testing"
4+
5+
func TestBuild(t *testing.T) {
6+
Build()
7+
}
Loading
Loading
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,73 @@ import (
1414
".com/haozibi/leetcode-badge/internal/statics"
1515
)
1616

17-
// LastYear 过去一年
18-
func LastYear(data map[int64]int) ([]byte, error) {
17+
// PastYear 过去一年
18+
func PastYear(data map[int64]int, color string) ([]byte, error) {
1919
now := time.Now()
20-
return Build(now.AddDate(-1, 0, 0).Unix(), now.Unix(), data)
20+
return Build(now.AddDate(-1, 0, 0).Unix(), now.Unix(), data, color)
2121
}
2222

2323
// CurrYear 当前年
24-
func CurrYear(data map[int64]int) ([]byte, error) {
24+
func CurrYear(data map[int64]int, color string) ([]byte, error) {
2525
now := time.Now()
2626
s := time.Date(now.Year(), time.January, 1, 0, 0, 0, 0, time.UTC)
2727
e := time.Date(now.Year(), time.December, 31, 23, 59, 59, 0, time.UTC)
2828

29-
return Build(s.Unix(), e.Unix(), data)
29+
return Build(s.Unix(), e.Unix(), data, color)
3030
}
3131

32-
func Build(start, end int64, data map[int64]int) ([]byte, error) {
32+
func Build(start, end int64, data map[int64]int, color string) ([]byte, error) {
33+
34+
cfg := &CalendarHeatmapConfig{
35+
Colors: []string{"#EBEDF0", "#9BE9A8", "#40C463", "#30A14E", "#216E39"},
36+
BlockSize: 11,
37+
BlockRoundness: 2,
38+
BlockMargin: 2,
39+
MonthLabels: []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"},
40+
MonthLabelHeight: 15,
41+
WeekdayLabels: []string{"", "Mon", "", "Wed", "", "Fri", ""},
42+
weekLabelWidth: 0,
43+
MonthSpace: 1,
44+
}
45+
46+
switch color {
47+
case "yellow":
48+
cfg.Colors = Yellow
49+
case "green":
50+
cfg.Colors = Green
51+
case "blue":
52+
cfg.Colors = Blue
53+
default:
54+
cfg.Colors = Green
55+
}
3356

3457
input := make(map[Date]int, len(data))
3558
for k, v := range data {
3659
if k >= start && k <= end {
3760
t := time.Unix(k, 0)
61+
vv := 0
62+
if v >= 1 && v <= 3 {
63+
vv = 1
64+
}
65+
if v > 3 && v <= 6 {
66+
vv = 2
67+
}
68+
if v > 6 && v <= 10 {
69+
vv = 3
70+
}
71+
if v > 10 {
72+
vv = 4
73+
}
3874
input[Date{
3975
Year: t.Year(),
4076
Month: t.Month(),
4177
Day: t.Day(),
42-
}] = v % 5 // only 5 color
78+
}] = vv // only 5 color
4379
}
4480
}
4581

4682
st, et := time.Unix(start, 0), time.Unix(end, 0)
47-
h := New(nil)
83+
h := New(cfg)
4884
buf := h.Generate(
4985
Date{
5086
Year: st.Year(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package heatmap
2+
3+
// 0.15 0.35 0.55 0.75 0.94
4+
var (
5+
Yellow = []string{"#FDF3B7", "#F5C670", "#EC7C46", "#C9312B", "#751428"}
6+
Green = []string{"#EBEDF0", "#9BE9A8", "#40C463", "#30A14E", "#216E39"}
7+
Blue = []string{"#E9F0F9", "#B7D1E5", "#6CA1CC", "#3568AB", "#152F67"}
8+
)
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ func (c *CalendarHeatmap) Generate(dateFrom, dateTo Date, data map[Date]int) *by
121121
}
122122

123123
// draw svg
124-
size := weeks
125-
for i := 0; i < months; i++ {
126-
size += months * c.Config.MonthSpace
127-
}
124+
size := weeks + months*c.Config.MonthSpace
128125
canvasPos := c.getPosition(size, days)
129126
canvas.Start(canvasPos.X, canvasPos.Y)
130127

0 commit comments

Comments
 (0)