diff --git a/graph.go b/graph.go index 1ca21b8..1677229 100644 --- a/graph.go +++ b/graph.go @@ -11,6 +11,7 @@ import ( "strings" "git.smarteching.com/zeni/go-chart/v2" + "git.smarteching.com/zeni/go-charts/v2" "github.com/julienschmidt/httprouter" ) @@ -44,7 +45,7 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { qtyl := len(labels) qtyv := len(values) - // cjeck qty and equal values from url + // check qty and equal values from url if qtyl < 2 { fmt.Fprintf(w, "Missing captions in pie") return @@ -85,9 +86,9 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { qtyl := len(labels) qtyv := len(values) - // cjeck qty and equal values from url + // check qty and equal values from url if qtyl < 2 { - fmt.Fprintf(w, "Missing captions in pie") + fmt.Fprintf(w, "Missing captions in bar") return } 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) } + // 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) }