go-chart/_examples/legend/main.go

52 lines
1.2 KiB
Go
Raw Normal View History

2016-07-17 05:53:46 +02:00
package main
import (
"net/http"
2018-12-06 16:46:27 +01:00
"git.gutmet.org/go-chart.git"
2016-07-17 05:53:46 +02:00
)
func drawChart(res http.ResponseWriter, req *http.Request) {
/*
In this example we add a `Renderable` or a custom component to the `Elements` array.
In this specific case it is a pre-built renderable (`CreateLegend`) that draws a legend for the chart's series.
If you like, you can use `CreateLegend` as a template for writing your own renderable, or even your own legend.
*/
graph := chart.Chart{
XAxis: chart.XAxis{
Style: chart.StyleShow(),
},
YAxis: chart.YAxis{
Style: chart.StyleShow(),
},
Background: chart.Style{
Padding: chart.Box{
Top: 20,
Left: 20,
},
},
2016-07-17 05:53:46 +02:00
Series: []chart.Series{
chart.ContinuousSeries{
Name: "A test series",
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
},
},
}
//note we have to do this as a separate step because we need a reference to graph
graph.Elements = []chart.Renderable{
2016-07-30 03:24:25 +02:00
chart.Legend(&graph),
2016-07-17 05:53:46 +02:00
}
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
}
func main() {
http.HandleFunc("/", drawChart)
http.ListenAndServe(":8080", nil)
}