Commit
Author: Drew DeVault [sir@cmpwn.com]
Hash: d5eacb22a525e767ec5e150c02dcbb0fabb5d383
Timestamp: Sat, 19 Sep 2020 14:17:14 +0000 (4 years ago)

+13 -4 +/-1 browse
Add date to labels for ranges >24 hours
1diff --git a/main.go b/main.go
2index 4d22670..e15d079 100644
3--- a/main.go
4+++ b/main.go
5 @@ -185,7 +185,7 @@ func main() {
6 p.Title.Text = t[0]
7 }
8 p.X.Label.Text = "Time"
9- p.X.Tick.Marker = dateTicks{}
10+ p.X.Tick.Marker = dateTicks{start, end}
11 if ms, ok := args["max"]; ok {
12 m, _ := strconv.ParseFloat(ms[0], 64)
13 p.Y.Max = m
14 @@ -259,10 +259,19 @@ func main() {
15 http.ListenAndServe(addr, router)
16 }
17
18- type dateTicks struct{}
19+ type dateTicks struct {
20+ Start time.Time
21+ End time.Time
22+ }
23+
24 // Ticks computes the default tick marks, but inserts commas
25 // into the labels for the major tick marks.
26- func (dateTicks) Ticks(min, max float64) []plot.Tick {
27+ func (dt dateTicks) Ticks(min, max float64) []plot.Tick {
28+ fmt := "15:04:05"
29+ if dt.End.Sub(dt.Start).Hours() >= 24 {
30+ fmt = "Jan 2 15:04:05"
31+ }
32+
33 tks := plot.DefaultTicks{}.Ticks(min, max)
34 for i, t := range tks {
35 if t.Label == "" { // Skip minor ticks, they are fine.
36 @@ -270,7 +279,7 @@ func (dateTicks) Ticks(min, max float64) []plot.Tick {
37 }
38 d, _ := strconv.ParseFloat(t.Label, 64)
39 tm := time.Unix(int64(d), 0)
40- tks[i].Label = tm.Format("15:04:05")
41+ tks[i].Label = tm.Format(fmt)
42 }
43 return tks
44 }