so, I've come up with something:
-
https://git.sr.ht/~sbinet/epok
```go
func ExampleTicks_yearlyPlot() {
cnv := epok.UTCUnixTimeConverter{}
p := plot.New()
p.Title.Text = "Time series"
p.Y.Label.Text = "Goroutines"
p.Y.Min = 0
p.Y.Max = 4
p.X.Tick.Marker = epok.Ticks{
Ruler: epok.Rules{
Major: epok.Rule{
Freq: epok.Yearly,
Range: epok.Range{Step: 5},
},
},
Format: "2006-01-02\n15:04:05",
Converter: cnv,
}
xysFrom := func(vs ...float64) plotter.XYs {
o := make(plotter.XYs, len(vs))
for i := range o {
o[i].X = vs[i]
o[i].Y = float64(i + 1)
}
return o
}
data := xysFrom(
cnv.FromTime(parse("2010-02-03 01:02:03")),
cnv.FromTime(parse("2015-02-03 04:05:06")),
cnv.FromTime(parse("2020-02-03 07:08:09")),
)
line, pnts, err := plotter.NewLinePoints(data)
if err != nil {
log.Fatalf("could not create plotter: %+v", err)
}
line.Color = color.RGBA{B: 255, A: 255}
pnts.Shape = draw.CircleGlyph{}
pnts.Color = color.RGBA{R: 255, A: 255}
p.Add(line, pnts, plotter.NewGrid())
err = p.Save(20*vg.Centimeter, 10*vg.Centimeter, "testdata/yearly.png")
if err != nil {
log.Fatalf("could not save plot: %+v", err)
}
}
```
with the linked output:
-
https://git.sr.ht/~sbinet/epok/tree/main/item/testdata/yearly_golden.png
I had to extend the plot.TimeTicks.Time field to a 'Converter' interface
to be able to convert back and forth between time.Time and float64
values.
Building on
github.com/teambition/rrule-go, I could relatively easily
generate ticks with a given frequency (yearly, monthly, ...).
I plan to provide an "AutoTicks" ticker that can figure out the best
(for some definition of best) frequency and range so the above example
could be simplified to:
```
p.X.Tick.Marker = epok.AutoTicks{
Format: "...", // perhaps, even w/o Format.
}
```
There's one hurdle though, as can be seen in the 'testdata/yearly.png'
output:
While epok.Ticks{} will generate the following ticks ('+' denotes a
major tick):
+2010-01-01 00:00:00 2011-01-01 00:00:00 2012-01-01 00:00:00
2013-01-01 00:00:00 2014-01-01 00:00:00 +2015-01-01 00:00:00
2016-01-01 00:00:00 2017-01-01 00:00:00 2018-01-01 00:00:00
2019-01-01 00:00:00 +2020-01-01 00:00:00
when given the below min/max x-values:
min=2010-02-03 01:02:03
max=2020-02-03 07:08:09
ie: it will "insert" 2010-01-01 as first major tick.
but the current gonum/plot will drop that first (major) tick as it
is outside of the data canvas area.
that's done here:
-
https://github.com/gonum/plot/blob/v0.13.0/axis.go#L269
and here:
-
https://github.com/gonum/plot/blob/v0.13.0/axis.go#L285
do you think axes should be allowed to automatically modify their
min/max range ?
-s
PS: I guess, at some point (for pl...@v0.14.0 ?), epok could be merged
into gonum/plot proper.