go-chart/_examples/scatter/main.go
2018-12-06 16:46:27 +01:00

72 lines
1.5 KiB
Go

package main
import (
"log"
"net/http"
_ "net/http/pprof"
"git.gutmet.org/go-chart.git"
"git.gutmet.org/go-chart.git/drawing"
"git.gutmet.org/go-chart.git/seq"
)
func drawChart(res http.ResponseWriter, req *http.Request) {
viridisByY := func(xr, yr chart.Range, index int, x, y float64) drawing.Color {
return chart.Viridis(y, yr.GetMin(), yr.GetMax())
}
graph := chart.Chart{
Series: []chart.Series{
chart.ContinuousSeries{
Style: chart.Style{
Show: true,
StrokeWidth: chart.Disabled,
DotWidth: 5,
DotColorProvider: viridisByY,
},
XValues: seq.Range(0, 127),
YValues: seq.New(seq.NewRandom().WithLen(128).WithMax(1024)).Array(),
},
},
}
res.Header().Set("Content-Type", chart.ContentTypePNG)
err := graph.Render(chart.PNG, res)
if err != nil {
log.Println(err.Error())
}
}
func unit(res http.ResponseWriter, req *http.Request) {
graph := chart.Chart{
Height: 50,
Width: 50,
Canvas: chart.Style{
Padding: chart.BoxZero,
},
Background: chart.Style{
Padding: chart.BoxZero,
},
Series: []chart.Series{
chart.ContinuousSeries{
XValues: seq.RangeWithStep(0, 4, 1),
YValues: seq.RangeWithStep(0, 4, 1),
},
},
}
res.Header().Set("Content-Type", chart.ContentTypePNG)
err := graph.Render(chart.PNG, res)
if err != nil {
log.Println(err.Error())
}
}
func main() {
http.HandleFunc("/", drawChart)
http.HandleFunc("/unit", unit)
log.Fatal(http.ListenAndServe(":8080", nil))
}