Example_timeSeries draws a time series.
()
| 18 | |
| 19 | // Example_timeSeries draws a time series. |
| 20 | func Example_timeSeries() { |
| 21 | rnd := rand.New(rand.NewPCG(1, 1)) |
| 22 | |
| 23 | // xticks defines how we convert and display time.Time values. |
| 24 | xticks := plot.TimeTicks{Format: "2006-01-02\n15:04"} |
| 25 | |
| 26 | // randomPoints returns some random x, y points |
| 27 | // with some interesting kind of trend. |
| 28 | randomPoints := func(n int) plotter.XYs { |
| 29 | const ( |
| 30 | month = 1 |
| 31 | day = 1 |
| 32 | hour = 1 |
| 33 | min = 1 |
| 34 | sec = 1 |
| 35 | nsec = 1 |
| 36 | ) |
| 37 | pts := make(plotter.XYs, n) |
| 38 | for i := range pts { |
| 39 | date := time.Date(2007+i, month, day, hour, min, sec, nsec, time.UTC).Unix() |
| 40 | pts[i].X = float64(date) |
| 41 | pts[i].Y = float64(pts[i].X+10*rnd.Float64()) * 1e-9 |
| 42 | } |
| 43 | return pts |
| 44 | } |
| 45 | |
| 46 | n := 10 |
| 47 | data := randomPoints(n) |
| 48 | |
| 49 | p := plot.New() |
| 50 | p.Title.Text = "Time Series" |
| 51 | p.X.Tick.Marker = xticks |
| 52 | p.Y.Label.Text = "Number of Gophers\n(Billions)" |
| 53 | p.Add(plotter.NewGrid()) |
| 54 | |
| 55 | line, points, err := plotter.NewLinePoints(data) |
| 56 | if err != nil { |
| 57 | log.Panic(err) |
| 58 | } |
| 59 | line.Color = color.RGBA{G: 255, A: 255} |
| 60 | points.Shape = draw.CircleGlyph{} |
| 61 | points.Color = color.RGBA{R: 255, A: 255} |
| 62 | |
| 63 | p.Add(line, points) |
| 64 | |
| 65 | err = p.Save(10*vg.Centimeter, 5*vg.Centimeter, "testdata/timeseries.png") |
| 66 | if err != nil { |
| 67 | log.Panic(err) |
| 68 | } |
| 69 | } |
nothing calls this directly
no test coverage detected