1
0
mirror of https://github.com/nikolaydubina/calendarheatmap.git synced 2025-02-01 13:28:10 +02:00
calendarheatmap/main.go

104 lines
2.5 KiB
Go
Raw Normal View History

2020-07-02 02:48:34 +08:00
package main
import (
2021-04-22 15:34:22 +08:00
"bytes"
2020-12-08 07:23:11 +08:00
"encoding/json"
2020-07-02 02:48:34 +08:00
"flag"
2020-07-03 09:44:39 +08:00
"image/color"
"io/ioutil"
2020-07-02 02:48:34 +08:00
"log"
"os"
2021-02-27 15:38:13 +00:00
"path"
2021-02-27 17:21:46 +00:00
"time"
2020-07-02 02:48:34 +08:00
2021-04-22 15:34:22 +08:00
_ "embed"
2020-07-02 17:58:02 +08:00
"github.com/nikolaydubina/calendarheatmap/charts"
2021-08-10 18:42:20 +08:00
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
2020-07-02 02:48:34 +08:00
)
2021-04-22 15:34:22 +08:00
//go:embed assets/fonts/Sunflower-Medium.ttf
var defaultFontFaceBytes []byte
//go:embed assets/colorscales/green-blue-9.csv
var defaultColorScaleBytes []byte
2020-07-02 02:48:34 +08:00
func main() {
2020-12-09 20:52:01 +08:00
var (
colorScale string
labels bool
locale string
monthSep bool
outputFormat string
)
flag.BoolVar(&labels, "labels", true, "labels for weekday and months")
flag.BoolVar(&monthSep, "monthsep", true, "render month separator")
2021-02-27 15:38:13 +00:00
flag.StringVar(&colorScale, "colorscale", "green-blue-9.csv", "filename of colorscale")
2020-12-09 20:52:01 +08:00
flag.StringVar(&locale, "locale", "en_US", "locale of labels (en_US, ko_KR)")
flag.StringVar(&outputFormat, "output", "png", "output format (png, jpeg, gif, svg)")
2020-07-02 02:48:34 +08:00
flag.Parse()
2021-04-22 15:34:22 +08:00
var colorscale charts.BasicColorScale
if assetsPath := os.Getenv("CALENDAR_HEATMAP_ASSETS_PATH"); assetsPath != "" {
var err error
colorscale, err = charts.NewBasicColorscaleFromCSVFile(path.Join(assetsPath, "colorscales", colorScale))
if err != nil {
log.Fatal(err)
}
} else {
var err error
if colorScale != "green-blue-9.csv" {
log.Printf("defaulting to colorscale %s since CALENDAR_HEATMAP_ASSETS_PATH is not set", "green-blue-9.csv")
}
colorscale, err = charts.NewBasicColorscaleFromCSV(bytes.NewBuffer(defaultColorScaleBytes))
if err != nil {
log.Fatal(err)
2021-02-27 15:38:13 +00:00
}
}
2021-08-10 18:42:20 +08:00
fontFace, err := charts.LoadFontFace(defaultFontFaceBytes, opentype.FaceOptions{
Size: 26,
DPI: 280,
Hinting: font.HintingNone,
})
2020-07-02 02:48:34 +08:00
if err != nil {
2020-12-08 07:23:11 +08:00
log.Fatal(err)
2020-07-02 02:48:34 +08:00
}
2021-04-22 15:34:22 +08:00
data, err := ioutil.ReadAll(os.Stdin)
2021-02-27 15:38:13 +00:00
if err != nil {
log.Fatal(err)
}
2021-04-22 15:34:22 +08:00
var counts map[string]int
if err := json.Unmarshal(data, &counts); err != nil {
2021-02-27 15:38:13 +00:00
log.Fatal(err)
}
2020-12-07 19:55:29 +08:00
conf := charts.HeatmapConfig{
2021-08-10 18:42:20 +08:00
Counts: counts,
ColorScale: colorscale,
DrawMonthSeparator: monthSep,
DrawLabels: labels,
Margin: 30,
BoxSize: 150,
MonthSeparatorWidth: 5,
MonthLabelYOffset: 50,
TextWidthLeft: 300,
TextHeightTop: 200,
TextColor: color.RGBA{100, 100, 100, 255},
BorderColor: color.RGBA{200, 200, 200, 255},
Locale: locale,
Format: outputFormat,
FontFace: fontFace,
2021-02-27 17:21:46 +00:00
ShowWeekdays: map[time.Weekday]bool{
time.Monday: true,
time.Wednesday: true,
time.Friday: true,
},
2020-07-02 02:48:34 +08:00
}
2020-12-08 07:23:11 +08:00
charts.WriteHeatmap(conf, os.Stdout)
2020-07-02 02:48:34 +08:00
}