-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.go
48 lines (42 loc) · 1.03 KB
/
plot.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
package engine
import (
"bytes"
"fmt"
"image"
"image/png"
"net/http"
"os/exec"
"strings"
)
func servePlot(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path[len("/plot/"):]
var a, b interface{}
u := strings.Split(url, "/") // u a:b
if len(u) == 2 {
a, b = u[0], u[1]
} else {
a, b = GUI.Value("usingx"), GUI.Value("usingy")
}
cmd := "gnuplot"
args := []string{"-e", fmt.Sprintf(`set format x "%%g"; set key off; set format y "%%g"; set term svg size 480,320 fsize 10; plot "%vtable.txt" u %v:%v w li; set output;exit;`, OD, a, b)}
out, err := exec.Command(cmd, args...).CombinedOutput()
if err != nil {
w.Write(emptyIMG())
GUI.Set("plotErr", string(out))
return
} else {
w.Header().Set("Content-Type", "image/svg+xml")
w.Write(out)
GUI.Set("plotErr", "")
}
}
var empty_img []byte
// empty image to show if there's no plot...
func emptyIMG() []byte {
if empty_img == nil {
o := bytes.NewBuffer(nil)
png.Encode(o, image.NewNRGBA(image.Rect(0, 0, 4, 4)))
empty_img = o.Bytes()
}
return empty_img
}