218 lines
4.4 KiB
Go
218 lines
4.4 KiB
Go
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
|
// Use of this source code is governed by MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.smarteching.com/zeni/go-chart/v2"
|
|
"git.smarteching.com/zeni/go-charts/v2"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
// $_GET values
|
|
|
|
service := ps.ByName("graph")
|
|
|
|
if service != "" {
|
|
|
|
//serv := strings.TrimPrefix(service, "/")
|
|
serv := strings.Split(service, "/")
|
|
|
|
switch serv[1] {
|
|
|
|
case "graph":
|
|
|
|
kindg := serv[2]
|
|
|
|
if kindg != "" {
|
|
|
|
switch kindg {
|
|
// case graph pie
|
|
case "pie":
|
|
|
|
queryValues := r.URL.Query()
|
|
labels := strings.Split(queryValues.Get("l"), "|")
|
|
values := strings.Split(queryValues.Get("v"), "|")
|
|
|
|
qtyl := len(labels)
|
|
qtyv := len(values)
|
|
|
|
// check qty and equal values from url
|
|
if qtyl < 2 {
|
|
fmt.Fprintf(w, "Missing captions in pie")
|
|
return
|
|
}
|
|
if qtyv != qtyl {
|
|
fmt.Fprintf(w, "Labels and values mismatch")
|
|
return
|
|
}
|
|
|
|
var cvalues []chart.Value
|
|
var row chart.Value
|
|
// fill values
|
|
for index, line := range labels {
|
|
valuefloat, _ := strconv.ParseFloat(values[index], 64)
|
|
row.Value = valuefloat
|
|
row.Label = line
|
|
cvalues = append(cvalues, row)
|
|
}
|
|
|
|
pie := chart.PieChart{
|
|
Width: 512,
|
|
Height: 512,
|
|
Values: cvalues,
|
|
}
|
|
w.Header().Set("Content-Type", "image/png")
|
|
err := pie.Render(chart.PNG, w)
|
|
if err != nil {
|
|
fmt.Printf("Error rendering pie chart: %v\n", err)
|
|
}
|
|
|
|
// case graph bar
|
|
case "bar":
|
|
|
|
queryValues := r.URL.Query()
|
|
labels := strings.Split(queryValues.Get("l"), "|")
|
|
values := strings.Split(queryValues.Get("v"), "|")
|
|
|
|
qtyl := len(labels)
|
|
qtyv := len(values)
|
|
|
|
// check qty and equal values from url
|
|
if qtyl < 2 {
|
|
fmt.Fprintf(w, "Missing captions in bar")
|
|
return
|
|
}
|
|
if qtyv != qtyl {
|
|
fmt.Fprintf(w, "Labels and values mismatch")
|
|
return
|
|
}
|
|
|
|
var cvalues []chart.Value
|
|
var row chart.Value
|
|
// fill values
|
|
for index, line := range labels {
|
|
valuefloat, _ := strconv.ParseFloat(values[index], 64)
|
|
row.Value = valuefloat
|
|
row.Label = line
|
|
cvalues = append(cvalues, row)
|
|
}
|
|
|
|
bar := chart.BarChart{
|
|
Height: 512,
|
|
BarWidth: 50,
|
|
Bars: cvalues,
|
|
}
|
|
w.Header().Set("Content-Type", "image/png")
|
|
err := bar.Render(chart.PNG, w)
|
|
if err != nil {
|
|
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:
|
|
fmt.Fprintf(w, "Unknown graph %s!\n", kindg)
|
|
}
|
|
|
|
} else {
|
|
fmt.Fprintf(w, "Kind graph not exists")
|
|
}
|
|
//
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(w, "Unknown option in core service %s!\n", ps.ByName("serv"))
|
|
|
|
}
|
|
fmt.Fprintf(w, "Service %s!\n", serv)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Unknown core service %s!\n", ps.ByName("graph"))
|
|
}
|
|
|
|
}
|