/
charts.go
233 lines (207 loc) 路 5.33 KB
/
charts.go
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
package charts
import (
"image"
"image/color"
"image/draw"
"time"
"github.com/nikolaydubina/calendarheatmap/colorscales"
)
var monthLabel = map[time.Month]string{
time.January: "Jan",
time.February: "Feb",
time.March: "Mar",
time.April: "Apr",
time.May: "May",
time.June: "Jun",
time.July: "Jul",
time.August: "Aug",
time.September: "Sep",
time.October: "Oct",
time.November: "Nov",
time.December: "Dec",
}
var weekdayLabel = map[time.Weekday]string{
time.Monday: "Mon",
time.Tuesday: "Tue",
time.Wednesday: "Wed",
time.Thursday: "Thu",
time.Friday: "Fri",
time.Saturday: "Sat",
time.Sunday: "Sun",
}
var weekdayOrder = [7]time.Weekday{
time.Monday,
time.Tuesday,
time.Wednesday,
time.Thursday,
time.Friday,
time.Saturday,
time.Sunday,
}
const (
numWeeksYear = 52
numWeekCols = numWeeksYear + 1 // 53 * 7 = 371 > 366
)
// HeatmapConfig contains config of calendar heatmap image
type HeatmapConfig struct {
Year int
CountByDay map[int]int
ColorScale colorscales.ColorScale
DrawMonthSeparator bool
DrawLabels bool
BoxSize int
Margin int
TextWidthLeft int
TextHightTop int
TextColor color.RGBA
BorderColor color.RGBA
}
// NewHeatmap creates image with heatmap and additional elements
func NewHeatmap(conf HeatmapConfig) image.Image {
width := conf.TextWidthLeft + numWeekCols*(conf.BoxSize+conf.Margin)
height := conf.TextHightTop + 7*(conf.BoxSize+conf.Margin)
offset := image.Point{X: conf.TextWidthLeft, Y: conf.TextHightTop}
img := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
visitors := []DayVisitor{
&DayBoxVisitor{img, conf.ColorScale, conf.BoxSize},
}
if conf.DrawMonthSeparator {
visitors = append(
visitors,
&MonthSeparatorVisitor{
Img: img,
MinY: conf.TextHightTop,
MaxY: height - conf.Margin,
Margin: conf.Margin,
BoxSize: conf.BoxSize,
Width: 1,
Color: conf.BorderColor,
},
)
}
if conf.DrawLabels {
visitors = append(visitors, &MonthLabelsVisitor{Img: img, YOffset: 5, Color: conf.TextColor})
}
for iter := NewDayIterator(conf.Year, offset, conf.CountByDay, conf.BoxSize, conf.Margin); !iter.Done(); iter.Next() {
for _, v := range visitors {
v.Visit(iter)
}
}
if conf.DrawLabels {
drawWeekdayLabels(
img,
offset,
map[time.Weekday]bool{
time.Monday: true,
time.Wednesday: true,
time.Friday: true,
},
conf.BoxSize,
conf.Margin,
conf.TextColor,
)
}
return img
}
// DayVisitor is interface to update image based on current box
type DayVisitor interface {
Visit(iter *DayIterator)
}
// DayBoxVisitor draws signle heatbox
type DayBoxVisitor struct {
Img *image.RGBA
ColorScale colorscales.ColorScale
BoxSize int
}
// Visit called on every iteration
func (d *DayBoxVisitor) Visit(iter *DayIterator) {
p := iter.Point()
r := image.Rect(p.X, p.Y, p.X+d.BoxSize, p.Y+d.BoxSize)
color := d.ColorScale.GetColor(iter.Value())
draw.Draw(d.Img, r, &image.Uniform{color}, image.ZP, draw.Src)
}
// MonthSeparatorVisitor draws month separator
type MonthSeparatorVisitor struct {
Img *image.RGBA
MinY int
MaxY int
Margin int
BoxSize int
Width int
Color color.RGBA
}
// Visit called on every iteration
func (d *MonthSeparatorVisitor) Visit(iter *DayIterator) {
day := iter.Time()
if day.Day() == 1 && day.Month() != time.January {
p := iter.Point()
marginSep := d.Margin / 2
xL := p.X - marginSep - d.Width
xR := p.X + d.BoxSize + marginSep
// left vertical line
drawLineAxis(
d.Img,
image.Point{X: xL, Y: p.Y},
image.Point{X: xL, Y: d.MaxY - d.Width},
d.Color,
)
if day.Weekday() != weekdayOrder[0] {
// right vertical line
drawLineAxis(
d.Img,
image.Point{X: xR, Y: d.MinY},
image.Point{X: xR, Y: p.Y - marginSep - d.Width},
d.Color,
)
// right vertical line
drawLineAxis(
d.Img,
image.Point{X: xL, Y: p.Y - marginSep - d.Width},
image.Point{X: xR, Y: p.Y - marginSep - d.Width},
d.Color,
)
// connect left vertical line and horizontal one
drawLineAxis(
d.Img,
image.Point{X: xL, Y: p.Y - marginSep - d.Width},
image.Point{X: xL, Y: p.Y},
d.Color,
)
}
}
}
// MonthLabelsVisitor draws month label on top of first row 0 of month
type MonthLabelsVisitor struct {
Img *image.RGBA
YOffset int
Color color.RGBA
}
// Visit on every iteration
func (d *MonthLabelsVisitor) Visit(iter *DayIterator) {
day := iter.Time()
// Note, day is from 1~31
if iter.Row == 0 && day.Day() <= 7 {
p := iter.Point()
drawText(
d.Img,
image.Point{X: p.X, Y: p.Y - d.YOffset},
monthLabel[day.Month()],
d.Color,
)
}
}
// drawWeekdayLabel draws column of same width labels for weekdays
// All weekday labels assumed to have same width, which really depends on font.
// offset argument is top right corner of where to insert column of weekday labels.
func drawWeekdayLabels(img *image.RGBA, offset image.Point, weekdays map[time.Weekday]bool, boxSize int, margin int, color color.RGBA) {
width := 25
height := 10
y := offset.Y + height
for _, w := range weekdayOrder {
if weekdays[w] {
drawText(img, image.Point{X: offset.X - width, Y: y}, weekdayLabel[w], color)
}
y += boxSize + margin
}
}