go-chart/examples/scatter/main.go

70 lines
1.6 KiB
Go
Raw Normal View History

package main
import (
"log"
"net/http"
2017-03-06 02:37:30 -05:00
_ "net/http/pprof"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
func drawChart(res http.ResponseWriter, req *http.Request) {
2017-04-19 18:27:31 -04:00
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{
2017-04-14 20:43:52 -04:00
StrokeWidth: chart.Disabled,
2017-04-15 12:43:59 -04:00
DotWidth: 5,
DotColorProvider: viridisByY,
},
2019-09-10 00:02:48 -04:00
XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(127)}.Values(),
YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(128).WithMin(0).WithMax(1024)}.Values(),
},
},
}
2017-04-14 20:43:52 -04:00
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{
2019-09-10 00:02:48 -04:00
XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(),
YValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(),
},
},
}
2017-04-14 20:43:52 -04:00
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))
}