heatmap!
This commit is contained in:
parent
7b5062352c
commit
f6d7b984a4
1
.gitignore
vendored
1
.gitignore
vendored
@ -373,3 +373,4 @@ FodyWeavers.xsd
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
heatmap
|
||||
|
93
heatmap.go
Normal file
93
heatmap.go
Normal file
@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ActivityLine struct {
|
||||
Date time.Time
|
||||
CaloriesEstimate uint64
|
||||
}
|
||||
|
||||
func main() {
|
||||
csvFile, _ := os.Open("heat.csv")
|
||||
reader := csv.NewReader(bufio.NewReader(csvFile))
|
||||
var length int = 17
|
||||
var days = make(map[time.Time]uint64)
|
||||
for {
|
||||
line, error := reader.Read()
|
||||
if error == io.EOF {
|
||||
break
|
||||
} else if error != nil {
|
||||
log.Fatal(error)
|
||||
}
|
||||
thisTime, error := time.Parse("2006-01-02", line[0])
|
||||
thisTime = time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 0, 0, 0, 0, time.Local)
|
||||
if error != nil {
|
||||
fmt.Println(error)
|
||||
break
|
||||
}
|
||||
cals, error := strconv.ParseUint(line[1], 10, 64)
|
||||
if error != nil {
|
||||
break
|
||||
}
|
||||
|
||||
days[thisTime] = days[thisTime] + cals
|
||||
}
|
||||
start := time.Date(time.Now().Year()-1, time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
|
||||
svg := "<svg width=\"960\" height=\"152\"><g transform=\"translate(29.5,24)\"><g fill=\"none\" stroke=\"#ccc\">"
|
||||
datePointer := start
|
||||
var week int = 0
|
||||
midnightThisMorning := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
|
||||
|
||||
var monthChange = false
|
||||
var monthLabelSpots = make(map[string]int)
|
||||
for {
|
||||
|
||||
var calsOnDay = days[datePointer]
|
||||
|
||||
if calsOnDay > 0 {
|
||||
svg = fmt.Sprintf("%s\n<rect width=\"%v\" height=\"%v\" x=\"%v\" y=\"%v\" fill=\"#00FF00\"></rect>", svg, length, length, week*length, length*int(datePointer.Weekday()))
|
||||
} else {
|
||||
svg = fmt.Sprintf("%s\n<rect width=\"%v\" height=\"%v\" x=\"%v\" y=\"%v\" ></rect>", svg, length, length, week*length, length*int(datePointer.Weekday()))
|
||||
}
|
||||
datePointer = datePointer.Add(time.Hour * 24)
|
||||
|
||||
if datePointer.After(midnightThisMorning) {
|
||||
break
|
||||
}
|
||||
if datePointer.Day() == 1 {
|
||||
monthChange = true
|
||||
}
|
||||
if datePointer.Weekday() == time.Sunday {
|
||||
week++
|
||||
if monthChange {
|
||||
monthLabelSpots[datePointer.Month().String()] = week * length
|
||||
monthChange = false
|
||||
}
|
||||
}
|
||||
}
|
||||
svg = fmt.Sprintf("%s\n</g></g>", svg)
|
||||
|
||||
//week labels
|
||||
//tODO: parameterize
|
||||
svg = fmt.Sprintf("%s<g transform=\"translate(6, 16)\"><text x=\"0\" y=\"30\">Mon</text><text x=\"0\" y=\"64\">Wed</text><text x=\"0\" y=\"97\">Fri</text></g>", svg)
|
||||
|
||||
//month labels
|
||||
svg = fmt.Sprintf("%s<g transform=\"translate(30,14)\">", svg)
|
||||
for monthName := range monthLabelSpots {
|
||||
svg = fmt.Sprintf("%s<text x=\"%v\" y=\"0\">%v</text>", svg, monthLabelSpots[monthName], monthName[:3])
|
||||
}
|
||||
svg = fmt.Sprintf("%s</g>", svg)
|
||||
|
||||
svg = fmt.Sprintf("%s</svg>", svg)
|
||||
ioutil.WriteFile("heatmap.svg", []byte(svg), 0644)
|
||||
}
|
Loading…
Reference in New Issue
Block a user