go-chart/examples/legend/main.go

44 lines
984 B
Go
Raw Normal View History

2016-07-16 23:53:46 -04:00
package main
2019-09-10 00:02:48 -04:00
//go:generate go run main.go
2016-07-16 23:53:46 -04:00
import (
2019-09-10 00:02:48 -04:00
"os"
2016-07-16 23:53:46 -04:00
2019-02-13 19:09:26 -05:00
chart "github.com/wcharczuk/go-chart"
2016-07-16 23:53:46 -04:00
)
2019-09-10 00:02:48 -04:00
func main() {
2016-07-16 23:53:46 -04:00
/*
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{
Background: chart.Style{
Padding: chart.Box{
Top: 20,
Left: 20,
},
},
2016-07-16 23:53:46 -04: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-29 21:24:25 -04:00
chart.Legend(&graph),
2016-07-16 23:53:46 -04:00
}
2019-09-10 00:02:48 -04:00
f, _ := os.Create("output.png")
defer f.Close()
graph.Render(chart.PNG, f)
2016-07-16 23:53:46 -04:00
}