...
1
2
3
4
5 package plotter
6
7 import (
8 "gonum.org/v1/plot"
9 "gonum.org/v1/plot/vg"
10 "gonum.org/v1/plot/vg/draw"
11 )
12
13
14
15 type Scatter struct {
16
17 XYs
18
19
20
21 GlyphStyleFunc func(int) draw.GlyphStyle
22
23
24
25 draw.GlyphStyle
26 }
27
28
29
30 func NewScatter(xys XYer) (*Scatter, error) {
31 data, err := CopyXYs(xys)
32 if err != nil {
33 return nil, err
34 }
35 return &Scatter{
36 XYs: data,
37 GlyphStyle: DefaultGlyphStyle,
38 }, err
39 }
40
41
42
43 func (pts *Scatter) Plot(c draw.Canvas, plt *plot.Plot) {
44 trX, trY := plt.Transforms(&c)
45 glyph := func(i int) draw.GlyphStyle { return pts.GlyphStyle }
46 if pts.GlyphStyleFunc != nil {
47 glyph = pts.GlyphStyleFunc
48 }
49 for i, p := range pts.XYs {
50 c.DrawGlyph(glyph(i), vg.Point{X: trX(p.X), Y: trY(p.Y)})
51 }
52 }
53
54
55
56
57 func (pts *Scatter) DataRange() (xmin, xmax, ymin, ymax float64) {
58 return XYRange(pts)
59 }
60
61
62
63 func (pts *Scatter) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
64 glyph := func(i int) draw.GlyphStyle { return pts.GlyphStyle }
65 if pts.GlyphStyleFunc != nil {
66 glyph = pts.GlyphStyleFunc
67 }
68 bs := make([]plot.GlyphBox, len(pts.XYs))
69 for i, p := range pts.XYs {
70 bs[i].X = plt.X.Norm(p.X)
71 bs[i].Y = plt.Y.Norm(p.Y)
72 r := glyph(i).Radius
73 bs[i].Rectangle = vg.Rectangle{
74 Min: vg.Point{X: -r, Y: -r},
75 Max: vg.Point{X: +r, Y: +r},
76 }
77 }
78 return bs
79 }
80
81
82
83 func (pts *Scatter) Thumbnail(c *draw.Canvas) {
84 c.DrawGlyph(pts.GlyphStyle, c.Center())
85 }
86
View as plain text