-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.go
72 lines (63 loc) · 1.72 KB
/
graph.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
package ncpgraph
import (
"bytes"
"fmt"
"log"
"net/http"
"os"
chart "github.com/wcharczuk/go-chart/v2"
)
func CreateLineGraph(xname, yname string, xval []float64, yval [][]float64, legend []string, fileName string) error {
start := xval[0]
xlen := len(xval)
end := xval[xlen-1]
cs := make([]chart.Series, 0)
for i, y := range yval {
s := chart.ContinuousSeries{ //TimeSeries{
Style: chart.Style{StrokeColor: chart.GetDefaultColor(i)},
Name: legend[i],
XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(start).WithEnd(end)}.Values(),
YValues: y,
}
cs = append(cs, s)
}
graph := chart.Chart{
Background: chart.Style{
Padding: chart.Box{Top: 40, Left: 20, Right: 20, Bottom: 20},
},
XAxis: chart.XAxis{Name: xname,
ValueFormatter: chart.IntValueFormatter, // TimeHourValueFormatter,
},
YAxis: chart.YAxis{Name: yname},
Series: cs,
}
graph.Elements = []chart.Renderable{
chart.LegendThin(&graph),
}
buffer := bytes.NewBuffer([]byte{})
err := graph.Render(chart.PNG, buffer)
if err != nil {
log.Fatal(err)
}
f, err := os.Create("images/" + fileName + ".png")
if err != nil {
fmt.Println(err)
}
if _, err := f.Write(buffer.Bytes()); err != nil {
fmt.Println(err)
}
return err
}
func StartWebService(port string, dirPath string) {
servPort := port
if len(port) == 0 {
servPort = ":8080"
}
servPath := dirPath
if len(dirPath) == 0 {
servPath = "./images"
}
fs := http.FileServer(http.Dir(servPath))
fmt.Printf("Please visit http://localhost%s or http://hostip%s in browser to view image files in %s\n", servPort, servPort, servPath)
http.ListenAndServe(servPort, fs) // ":8080"
}