mirror of
https://github.com/nikolaydubina/calendarheatmap.git
synced 2024-12-06 04:04:44 +02:00
Merge pull request #8 from nikolaydubina/feature/script-stdin
better json data example; +readme example
This commit is contained in:
commit
4a9956de4c
34
README.md
34
README.md
@ -5,7 +5,19 @@
|
||||
|
||||
Self-contained, plain Go implementation of calendar heatmap inspired by Github contribution activity.
|
||||
|
||||
```
|
||||
$ go build
|
||||
$ echo '{
|
||||
"2020-05-16": 8,
|
||||
"2020-05-17": 13,
|
||||
"2020-05-18": 5,
|
||||
"2020-05-19": 8,
|
||||
"2020-05-20": 5
|
||||
}' | ./calendarheatmap > chart.png
|
||||
```
|
||||
|
||||
Basic
|
||||
|
||||
![basic](charts/testdata/basic.png)
|
||||
|
||||
Colorscales
|
||||
@ -42,33 +54,21 @@ img := charts.NewHeatmap(charts.HeatmapConfig{
|
||||
})
|
||||
```
|
||||
|
||||
Example script,
|
||||
Example script
|
||||
```
|
||||
$ go build
|
||||
|
||||
$ ./calendarheatmap -input testdata/basic.json -output basicjson.png
|
||||
|
||||
$ ./calendarheatmap -input testdata/custom.txt -output custom.png -input-format row-day-seconds-count
|
||||
|
||||
$ cat testdata/basic.json | ./calendarheatmap -output jpeg > chart.jpeg
|
||||
$ cat testdata/custom.txt | ./calendarheatmap -input row-day-seconds-count > custom.png
|
||||
$ ./calendarheatmap -h
|
||||
|
||||
Usage of ./calendarheatmap:
|
||||
-colorscale string
|
||||
refer to colorscales for examples (default "PuBu9")
|
||||
-input string
|
||||
input filename (default "input.txt")
|
||||
-intput-format /parsers
|
||||
format of input file refer to /parsers for examples (default "json-basic")
|
||||
format of input file, refer to parsers module (default "json-basic")
|
||||
-labels
|
||||
labels for weekday and months (default true)
|
||||
-monthsep
|
||||
render month separator (default true)
|
||||
-output string
|
||||
output filename (default "chart.png")
|
||||
-output-format string
|
||||
output format (png, jpeg, gif) (default "png")
|
||||
```
|
||||
|
||||
TODO:
|
||||
- [ ] SVG support
|
||||
- [ ] select start and end date
|
||||
```
|
29
main.go
29
main.go
@ -19,18 +19,16 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
filenameInput := flag.String("input", "input.txt", "input filename")
|
||||
filenameChart := flag.String("output", "chart.png", "output filename")
|
||||
monthSep := flag.Bool("monthsep", true, "render month separator")
|
||||
colorScale := flag.String("colorscale", "PuBu9", "refer to colorscales for examples")
|
||||
inputFormat := flag.String("input", "json-basic", "format of input file, refer to parsers module")
|
||||
labels := flag.Bool("labels", true, "labels for weekday and months")
|
||||
outputFormat := flag.String("output-format", "png", "output format (png, jpeg, gif)")
|
||||
inputFormat := flag.String("input-format", "json-basic", "format of input file refer to `/parsers` for examples")
|
||||
monthSep := flag.Bool("monthsep", true, "render month separator")
|
||||
outputFormat := flag.String("output", "png", "output format (png, jpeg, gif)")
|
||||
flag.Parse()
|
||||
|
||||
data, err := ioutil.ReadFile(*filenameInput)
|
||||
data, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Errorf("cant not read file: %w", err))
|
||||
log.Fatal(fmt.Errorf("cant not read from stdin: %w", err))
|
||||
}
|
||||
|
||||
var parser parsers.Parser
|
||||
@ -40,10 +38,13 @@ func main() {
|
||||
case "json-basic":
|
||||
parser = &parsers.BasicJSONParser{}
|
||||
default:
|
||||
log.Fatal("unnknown parser format")
|
||||
log.Fatal("unknown parser format")
|
||||
return
|
||||
}
|
||||
year, countByDay, err := parser.Parse(data)
|
||||
if err != nil {
|
||||
log.Fatal("error parsing data: %w", err)
|
||||
}
|
||||
|
||||
img := charts.NewHeatmap(charts.HeatmapConfig{
|
||||
Year: year,
|
||||
@ -58,23 +59,19 @@ func main() {
|
||||
TextColor: color.RGBA{100, 100, 100, 255},
|
||||
BorderColor: color.RGBA{200, 200, 200, 255},
|
||||
})
|
||||
f, err := os.Create(*filenameChart)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Errorf("can not create file: %w", err))
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
outWriter := os.Stdout
|
||||
switch *outputFormat {
|
||||
case "png":
|
||||
if err := png.Encode(f, img); err != nil {
|
||||
if err := png.Encode(outWriter, img); err != nil {
|
||||
log.Fatal(fmt.Errorf("can not encode png: %w", err))
|
||||
}
|
||||
case "jpeg":
|
||||
if err := jpeg.Encode(f, img, nil); err != nil {
|
||||
if err := jpeg.Encode(outWriter, img, nil); err != nil {
|
||||
log.Fatal(fmt.Errorf("can not encode jpeg: %w", err))
|
||||
}
|
||||
case "gif":
|
||||
if err := gif.Encode(f, img, nil); err != nil {
|
||||
if err := gif.Encode(outWriter, img, nil); err != nil {
|
||||
log.Fatal(fmt.Errorf("can not encode gifg: %w", err))
|
||||
}
|
||||
default:
|
||||
|
48
testdata/basic.json
vendored
48
testdata/basic.json
vendored
@ -1,8 +1,42 @@
|
||||
{
|
||||
"2020-01-01": 1,
|
||||
"2020-01-02": 2,
|
||||
"2020-01-03": 3,
|
||||
"2020-01-04": 4,
|
||||
"2020-01-05": 5,
|
||||
"2020-01-06": 6
|
||||
}
|
||||
"2020-05-16": 8,
|
||||
"2020-05-17": 13,
|
||||
"2020-05-18": 5,
|
||||
"2020-05-19": 8,
|
||||
"2020-05-20": 5,
|
||||
"2020-05-21": 5,
|
||||
"2020-05-22": 3,
|
||||
"2020-05-23": 5,
|
||||
"2020-05-24": 6,
|
||||
"2020-05-25": 3,
|
||||
"2020-05-26": 5,
|
||||
"2020-05-27": 8,
|
||||
"2020-05-28": 2,
|
||||
"2020-05-29": 2,
|
||||
"2020-05-30": 8,
|
||||
"2020-05-31": 5,
|
||||
"2020-06-01": 1,
|
||||
"2020-06-02": 3,
|
||||
"2020-06-03": 1,
|
||||
"2020-06-04": 3,
|
||||
"2020-06-05": 1,
|
||||
"2020-06-06": 3,
|
||||
"2020-06-07": 5,
|
||||
"2020-06-09": 1,
|
||||
"2020-06-10": 2,
|
||||
"2020-06-12": 9,
|
||||
"2020-06-13": 7,
|
||||
"2020-06-14": 4,
|
||||
"2020-06-15": 1,
|
||||
"2020-06-17": 1,
|
||||
"2020-06-20": 2,
|
||||
"2020-06-21": 1,
|
||||
"2020-06-23": 2,
|
||||
"2020-06-24": 2,
|
||||
"2020-06-25": 3,
|
||||
"2020-06-26": 3,
|
||||
"2020-06-27": 2,
|
||||
"2020-06-28": 1,
|
||||
"2020-06-29": 1,
|
||||
"2020-06-30": 2
|
||||
}
|
Loading…
Reference in New Issue
Block a user