| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "net/http" |
| 6 | |
| 7 | _ "net/http/pprof" |
| 8 | |
| 9 | "github.com/wcharczuk/go-chart/v2" |
| 10 | "github.com/wcharczuk/go-chart/v2/drawing" |
| 11 | ) |
| 12 | |
| 13 | func drawChart(res http.ResponseWriter, req *http.Request) { |
| 14 | |
| 15 | viridisByY := func(xr, yr chart.Range, index int, x, y float64) drawing.Color { |
| 16 | return chart.Viridis(y, yr.GetMin(), yr.GetMax()) |
| 17 | } |
| 18 | |
| 19 | graph := chart.Chart{ |
| 20 | Series: []chart.Series{ |
| 21 | chart.ContinuousSeries{ |
| 22 | Style: chart.Style{ |
| 23 | StrokeWidth: chart.Disabled, |
| 24 | DotWidth: 5, |
| 25 | DotColorProvider: viridisByY, |
| 26 | }, |
| 27 | XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(127)}.Values(), |
| 28 | YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(128).WithMin(0).WithMax(1024)}.Values(), |
| 29 | }, |
| 30 | }, |
| 31 | } |
| 32 | |
| 33 | res.Header().Set("Content-Type", chart.ContentTypePNG) |
| 34 | err := graph.Render(chart.PNG, res) |
| 35 | if err != nil { |
| 36 | log.Println(err.Error()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func unit(res http.ResponseWriter, req *http.Request) { |
| 41 | graph := chart.Chart{ |
| 42 | Height: 50, |
| 43 | Width: 50, |
| 44 | Canvas: chart.Style{ |
| 45 | Padding: chart.BoxZero, |
| 46 | }, |
| 47 | Background: chart.Style{ |
| 48 | Padding: chart.BoxZero, |
| 49 | }, |
| 50 | Series: []chart.Series{ |
| 51 | chart.ContinuousSeries{ |
| 52 | XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(), |
| 53 | YValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(), |
| 54 | }, |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | res.Header().Set("Content-Type", chart.ContentTypePNG) |
| 59 | err := graph.Render(chart.PNG, res) |
| 60 | if err != nil { |
| 61 | log.Println(err.Error()) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func main() { |
| 66 | http.HandleFunc("/", drawChart) |
| 67 | http.HandleFunc("/unit", unit) |
| 68 | log.Fatal(http.ListenAndServe(":8080", nil)) |
| 69 | } |