-
Notifications
You must be signed in to change notification settings - Fork 1
/
axis.go
107 lines (88 loc) · 1.78 KB
/
axis.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package gplot
import (
"log"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
type Axis struct {
p *plot.Plot
legend []string
}
func NewAxis() *Axis {
p := plot.New()
p.Title.Padding = 10
return &Axis{
p: p,
}
}
func (c *Axis) Plot__0(args ...[]float64) {
var vecs []Vector
for _, arg := range args {
vecs = append(vecs, newVec(arg))
}
c.addLine(vecs...)
}
func (c *Axis) Plot__1(args ...[]int) {
var vecs []Vector
for _, arg := range args {
i := []float64{}
for _, a := range arg {
i = append(i, float64(a))
}
vecs = append(vecs, newVec(i))
}
c.addLine(vecs...)
}
func (c *Axis) Plot__2(args ...Vector) {
c.addLine(args...)
}
func (c *Axis) Bar(args ...interface{}) {
c.addBar(args...)
}
func (a *Axis) Xlabel(xlabel string) {
a.p.X.Label.Text = xlabel
}
func (a *Axis) Ylabel(ylabel string) {
a.p.Y.Label.Text = ylabel
}
func (a *Axis) Title(title string) {
a.p.Title.Text = title
}
func (a *Axis) Legend(labels []string) {
a.legend = labels
}
func (c *Axis) NominalX(names ...string) {
c.p.NominalX(names...)
}
func (c *Axis) addLine(args ...Vector) {
for i := 0; i < len(args)/2; i++ {
x := args[2*i]
y := args[2*i+1]
points := buildLinePoints(x, y)
line, err := plotter.NewLine(points)
if err != nil {
log.Fatal(err)
}
line.Color = plotutil.Color(i)
if len(c.legend) > 0 {
c.p.Legend.Add(c.legend[i], line)
}
c.p.Add(line)
}
}
func (c *Axis) addBar(args ...interface{}) {
l := len(args)
for i, arg := range args {
w := vg.Points(16)
group := buildBarGroup(arg)
bar, _ := plotter.NewBarChart(group, w)
bar.Color = plotutil.Color(i)
bar.Offset = -1*vg.Length(l-1)*w/2 + w*vg.Length(i)
if len(c.legend) > 0 {
c.p.Legend.Add(c.legend[i], bar)
}
c.p.Add(bar)
}
}