/
dots.go
89 lines (73 loc) · 1.69 KB
/
dots.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package graphics
import (
"code.google.com/p/plotinum/plot"
"code.google.com/p/plotinum/vg"
"image/color"
"math"
)
// DotPlot plots a XY plot with dots in it
type Dots struct {
X []float64
Y []float64
color.Color
}
func NewDots(x, y []float64) (*Dots, error) {
return &Dots{x, y, color.RGBA{G: 100, B: 200, A: 255}}, nil
}
func (pt *Dots) Plot(da plot.DrawArea, plt *plot.Plot) {
trX, trY := plt.Transforms(&da)
da.SetColor(pt.Color)
for i := range pt.Y {
// Transform the data x, y coordinate of this bubble
// to the corresponding drawing coordinate.
x := trX(pt.X[i])
y := trY(pt.Y[i])
// Get the radius of this bubble. The radius
// is specified in drawing units (i.e., its size
// is given as the final size at which it will
// be drawn) so it does not need to be transformed.
rad := vg.Length(2)
// Fill a circle centered at x,y on the draw area.
var p vg.Path
p.Move(x+rad, y)
p.Arc(x, y, rad, 0, 2*math.Pi)
p.Close()
da.Fill(p)
}
}
func (pt *Dots) DataRange() (xmin, xmax, ymin, ymax float64) {
xmin = math.MaxFloat64
xmax = -math.MaxFloat64
ymin = math.MaxFloat64
ymax = -math.MaxFloat64
for i := range pt.Y {
if pt.Y[i] > ymax {
ymax = pt.Y[i]
}
if pt.Y[i] < ymin {
ymin = pt.Y[i]
}
if pt.X[i] > xmax {
xmax = pt.X[i]
}
if pt.X[i] < xmin {
xmin = pt.X[i]
}
}
return 0, xmax+1, 0, ymax
}
func DotsPaint(x, y []float64, title, xlabel, ylabel, file string) {
p, err := plot.New()
if err != nil {
return
}
p.Title.Text = title
p.X.Label.Text = xlabel
p.Y.Label.Text = ylabel
bs, err := NewDots(x, y)
// bs.Color = color.RGBA{R: 196, B: 128, A: 255}
p.Add(bs)
if err := p.Save(5, 5, file); err != nil {
return
}
}