1
0
mirror of https://github.com/nikolaydubina/calendarheatmap.git synced 2025-02-13 08:24:06 +02:00

79 lines
1.6 KiB
Go
Raw Normal View History

2020-12-07 19:55:29 +08:00
package charts
import (
"fmt"
"image"
2020-12-08 07:23:11 +08:00
"image/color"
2020-12-07 19:55:29 +08:00
"io"
"text/template"
)
type Day struct {
Count int
Date string
Color string
Show bool
}
type WeekdayLabel struct {
Label string
Show bool
}
type Params struct {
Days [53][7]Day
LabelsMonths [12]string
LabelsWeekdays [7]WeekdayLabel
2020-12-08 07:23:11 +08:00
LabelsColor string
2020-12-07 19:55:29 +08:00
}
2020-12-08 07:23:11 +08:00
func writeColor(c color.RGBA) string {
return fmt.Sprintf("rgb(%d,%d,%d)", c.R, c.G, c.B)
}
func writeSVG(conf HeatmapConfig, w io.Writer) error {
2020-12-07 19:55:29 +08:00
fullYearTemplate := template.Must(template.New("fullyear").Funcs(template.FuncMap{
"mul": func(a int, b int) int { return a * b },
"add": func(a int, b int) int { return a + b },
"sub": func(a int, b int) int { return a - b },
}).Parse(fullyear))
days := [53][7]Day{}
2020-12-08 07:23:11 +08:00
for iter := NewDayIterator(conf.Counts, image.Point{}, 0, 0); !iter.Done(); iter.Next() {
2020-12-07 19:55:29 +08:00
days[iter.Col][iter.Row] = Day{
Count: iter.Count(),
Date: iter.Time().Format("2006-01-02"),
2020-12-08 07:23:11 +08:00
Color: writeColor(conf.ColorScale.GetColor(iter.Value())),
2020-12-07 19:55:29 +08:00
Show: true,
}
}
locale := "en_US"
if conf.Locale != "" {
locale = conf.Locale
}
labelsProvider := NewLabelsProvider(locale)
labelsMonths := [12]string{}
for i, v := range labelsProvider.months {
labelsMonths[i-1] = v
}
labelsWeekdays := [7]WeekdayLabel{}
for i, v := range labelsProvider.weekdays {
labelsWeekdays[i] = WeekdayLabel{v, true}
}
params := Params{
Days: days,
LabelsMonths: labelsMonths,
LabelsWeekdays: labelsWeekdays,
2020-12-08 07:23:11 +08:00
LabelsColor: writeColor(conf.TextColor),
2020-12-07 19:55:29 +08:00
}
fullYearTemplate.Execute(w, params)
2020-12-08 07:23:11 +08:00
return nil
2020-12-07 19:55:29 +08:00
}