sync graph code
This commit is contained in:
parent
3d67bc0a03
commit
78a06fb900
1 changed files with 81 additions and 3 deletions
84
graph.go
84
graph.go
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.smarteching.com/zeni/go-chart/v2"
|
"git.smarteching.com/zeni/go-chart/v2"
|
||||||
|
"git.smarteching.com/zeni/go-charts/v2"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -44,7 +45,7 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
qtyl := len(labels)
|
qtyl := len(labels)
|
||||||
qtyv := len(values)
|
qtyv := len(values)
|
||||||
|
|
||||||
// cjeck qty and equal values from url
|
// check qty and equal values from url
|
||||||
if qtyl < 2 {
|
if qtyl < 2 {
|
||||||
fmt.Fprintf(w, "Missing captions in pie")
|
fmt.Fprintf(w, "Missing captions in pie")
|
||||||
return
|
return
|
||||||
|
|
@ -85,9 +86,9 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
qtyl := len(labels)
|
qtyl := len(labels)
|
||||||
qtyv := len(values)
|
qtyv := len(values)
|
||||||
|
|
||||||
// cjeck qty and equal values from url
|
// check qty and equal values from url
|
||||||
if qtyl < 2 {
|
if qtyl < 2 {
|
||||||
fmt.Fprintf(w, "Missing captions in pie")
|
fmt.Fprintf(w, "Missing captions in bar")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if qtyv != qtyl {
|
if qtyv != qtyl {
|
||||||
|
|
@ -116,6 +117,83 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
fmt.Printf("Error rendering pie chart: %v\n", err)
|
fmt.Printf("Error rendering pie chart: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// case multiple graph bar
|
||||||
|
case "mbar":
|
||||||
|
|
||||||
|
queryValues := r.URL.Query()
|
||||||
|
labels := strings.Split(queryValues.Get("l"), "|")
|
||||||
|
values := strings.Split(queryValues.Get("v"), "|")
|
||||||
|
options := strings.Split(queryValues.Get("o"), "|")
|
||||||
|
|
||||||
|
qtyl := len(labels)
|
||||||
|
qtyv := len(values)
|
||||||
|
qtyo := len(options)
|
||||||
|
|
||||||
|
// check qty and equal values from url
|
||||||
|
if qtyl < 2 {
|
||||||
|
fmt.Fprintf(w, "Missing captions in bar")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if qtyv < 2 {
|
||||||
|
fmt.Fprintf(w, "Missing values in bar")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if qtyo < 2 {
|
||||||
|
fmt.Fprintf(w, "Missing options in bar")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
valuest := [][]float64{
|
||||||
|
{
|
||||||
|
2.0,
|
||||||
|
4.9,
|
||||||
|
7.0,
|
||||||
|
23.2,
|
||||||
|
25.6,
|
||||||
|
76.7,
|
||||||
|
135.6,
|
||||||
|
162.2,
|
||||||
|
32.6,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
2.6,
|
||||||
|
5.9,
|
||||||
|
9.0,
|
||||||
|
26.4,
|
||||||
|
28.7,
|
||||||
|
70.7,
|
||||||
|
175.6,
|
||||||
|
182.2,
|
||||||
|
48.7,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
p, err := charts.BarRender(
|
||||||
|
valuest,
|
||||||
|
charts.XAxisDataOptionFunc([]string{
|
||||||
|
"Apr",
|
||||||
|
"May",
|
||||||
|
"Jun",
|
||||||
|
"Jul",
|
||||||
|
"Aug",
|
||||||
|
"Sep",
|
||||||
|
"Oct",
|
||||||
|
"Nov",
|
||||||
|
"Dec",
|
||||||
|
}),
|
||||||
|
charts.LegendLabelsOptionFunc(labels, charts.PositionRight),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, err := p.Bytes()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "image/png")
|
||||||
|
w.Write(buf)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error rendering pie chart: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(w, "Unknown graph %s!\n", kindg)
|
fmt.Fprintf(w, "Unknown graph %s!\n", kindg)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue