Skip to content

Commit

Permalink
hplot: add timeseries example with epok
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Sep 1, 2023
1 parent 948b009 commit 003a5cf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
gioui.org v0.2.0
git.sr.ht/~sbinet/epok v0.1.0
git.sr.ht/~sbinet/go-arrow v0.2.0
github.com/astrogo/fitsio v0.3.0
github.com/campoy/embedmd v1.0.0
Expand Down Expand Up @@ -59,6 +60,7 @@ require (
github.com/posener/complete v1.2.3 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/teambition/rrule-go v1.8.2 // indirect
golang.org/x/exp/shiny v0.0.0-20230801115018-d63ba01acd4b // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ gioui.org/shader v1.0.6/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM=
gioui.org/x v0.2.0 h1:/MbdjKH19F16auv19UiQxli2n6BYPw7eyh9XBOTgmEw=
gioui.org/x v0.2.0/go.mod h1:rCGN2nZ8ZHqrtseJoQxCMZpt2xrZUrdZ2WuMRLBJmYs=
git.sr.ht/~sbinet/cmpimg v0.1.0 h1:E0zPRk2muWuCqSKSVZIWsgtU9pjsw3eKHi8VmQeScxo=
git.sr.ht/~sbinet/epok v0.1.0 h1:gtcT69TjnNZl5NansTvzqWtIJ5W4ysMSyiZIjYDzORE=
git.sr.ht/~sbinet/epok v0.1.0/go.mod h1:fPYSc65tPhuTrm4kYvAZDx/vGaMsbIGg7owyUn+DD48=
git.sr.ht/~sbinet/gg v0.5.0 h1:6V43j30HM623V329xA9Ntq+WJrMjDxRjuAB1LFWF5m8=
git.sr.ht/~sbinet/gg v0.5.0/go.mod h1:G2C0eRESqlKhS7ErsNey6HHrqU1PwsnCQlekFi9Q2Oo=
git.sr.ht/~sbinet/go-arrow v0.2.0 h1:QIiVPcEtMb2lzOWDgBrIz9Sa+7Xvs2EJ/Icxe6IcUSI=
Expand Down Expand Up @@ -114,6 +116,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/teambition/rrule-go v1.8.2 h1:lIjpjvWTj9fFUZCmuoVDrKVOtdiyzbzc93qTmRVe/J8=
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
Expand Down
Binary file added hplot/testdata/timeseries_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions hplot/ticks_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package hplot_test

import (
"image/color"
"log"
"time"

"git.sr.ht/~sbinet/epok"
"go-hep.org/x/hep/hplot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)
Expand Down Expand Up @@ -75,3 +79,67 @@ func ExampleTicks() {
log.Fatalf("error: %+v\n", err)
}
}

func ExampleTicks_timeseries() {
cnv := epok.UTCUnixTimeConverter{}

p := hplot.New()
p.Title.Text = "Time series"
p.Y.Label.Text = "Goroutines"

p.Y.Min = 0
p.Y.Max = 4
p.X.AutoRescale = true
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 := hplot.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, hplot.NewGrid())

err = p.Save(20*vg.Centimeter, 10*vg.Centimeter, "testdata/timeseries.png")
if err != nil {
log.Fatalf("could not save plot: %+v", err)
}
}

func parse(vs ...string) time.Time {
format := "2006-01-02 15:04:05"
if len(vs) > 1 {
format = vs[1]
}
t, err := time.Parse(format, vs[0])
if err != nil {
panic(err)
}
return t
}
4 changes: 4 additions & 0 deletions hplot/ticks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ import (
func TestTicks(t *testing.T) {
checkPlot(cmpimg.CheckPlot)(ExampleTicks, t, "ticks.png")
}

func TestTicksTime(t *testing.T) {
checkPlot(cmpimg.CheckPlot)(ExampleTicks_timeseries, t, "timeseries.png")
}

0 comments on commit 003a5cf

Please sign in to comment.