-
Notifications
You must be signed in to change notification settings - Fork 19
/
common.go
212 lines (199 loc) · 5.95 KB
/
common.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package glot
import (
"fmt"
)
// SetTitle sets the title for the plot
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
func (plot *Plot) SetTitle(title string) error {
return plot.Cmd(fmt.Sprintf("set title \"%s\" ", title))
}
// SetXLabel changes the label for the x-axis
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetXLabel("X-Axis")
func (plot *Plot) SetXLabel(label string) error {
return plot.Cmd(fmt.Sprintf("set xlabel '%s'", label))
}
// SetYLabel changes the label for the y-axis
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetYLabel("Y-Axis")
func (plot *Plot) SetYLabel(label string) error {
return plot.Cmd(fmt.Sprintf("set ylabel '%s'", label))
}
// SetZLabel changes the label for the z-axis
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetZLabel("Z-Axis")
func (plot *Plot) SetZLabel(label string) error {
return plot.Cmd(fmt.Sprintf("set zlabel '%s'", label))
}
// SetLabels Functions helps to set labels for x, y, z axis simultaneously
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetLabels("X-axis","Y-Axis","Z-Axis")
func (plot *Plot) SetLabels(labels ...string) error {
ndims := len(labels)
if ndims > 3 || ndims <= 0 {
return &gnuplotError{fmt.Sprintf("invalid number of dims '%v'", ndims)}
}
var err error
for i, label := range labels {
switch i {
case 0:
ierr := plot.SetXLabel(label)
if ierr != nil {
err = ierr
return err
}
case 1:
ierr := plot.SetYLabel(label)
if ierr != nil {
err = ierr
return err
}
case 2:
ierr := plot.SetZLabel(label)
if ierr != nil {
err = ierr
return err
}
}
}
return nil
}
// SetXrange changes the label for the x-axis
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetXrange(-2,2)
func (plot *Plot) SetXrange(start int, end int) error {
return plot.Cmd(fmt.Sprintf("set xrange [%d:%d]", start, end))
}
// SetLogscale changes the label for the x-axis
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.SetYrange(-2, 18)
// plot.AddPointGroup("rates", "circle", [][]float64{{2, 4, 8, 16, 32}, {4, 7, 4, 10, 3}})
// plot.SetLogscale("x", 2)
func (plot *Plot) SetLogscale(axis string, base int) error {
return plot.Cmd(fmt.Sprintf("set logscale %s %d", axis, base))
}
// SetYrange changes the label for the y-axis
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetYrange(-2,2)
func (plot *Plot) SetYrange(start int, end int) error {
return plot.Cmd(fmt.Sprintf("set yrange [%d:%d]", start, end))
}
// SetZrange changes the label for the z-axis
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetZrange(-2,2)
func (plot *Plot) SetZrange(start int, end int) error {
return plot.Cmd(fmt.Sprintf("set zrange [%d:%d]", start, end))
}
// SavePlot function is used to save the plot at this point.
// The plot is dynamic and additional pointgroups can be added and removed and different versions
// of the same plot can be saved.
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetZrange(-2,2)
// plot.SavePlot("1.jpeg")
func (plot *Plot) SavePlot(filename string) (err error) {
if plot.nplots == 0 {
return &gnuplotError{fmt.Sprintf("This plot has 0 curves and therefore its a redundant plot and it can't be printed.")}
}
outputFormat := "set terminal " + plot.format
plot.CheckedCmd(outputFormat)
outputFileCommand := "set output" + "'" + filename + "'"
plot.CheckedCmd(outputFileCommand)
plot.CheckedCmd("replot ")
return nil
}
// SetFormat function is used to save the plot at this point.
// The plot is dynamic and additional pointgroups can be added and removed and different versions
// of the same plot can be saved.
//
// Usage
// dimensions := 3
// persist := false
// debug := false
// plot, _ := glot.NewPlot(dimensions, persist, debug)
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetFormat("pdf")
// plot.SavePlot("1.pdf")
// NOTE: png is default format for saving files.
func (plot *Plot) SetFormat(newformat string) error {
allowed := []string{
"png", "pdf"}
for _, s := range allowed {
if newformat == s {
plot.format = newformat
return nil
}
}
fmt.Printf("** Format '%v' not in allowed list %v\n", newformat, allowed)
fmt.Printf("** default to 'png'\n")
err := &gnuplotError{fmt.Sprintf("invalid format '%s'", newformat)}
return err
}