go-chart/_examples/simple_moving_average/main.go

38 lines
1 KiB
Go
Raw Normal View History

2016-07-16 23:53:46 -04:00
package main
import (
"net/http"
2019-02-13 21:55:13 -05:00
chart "github.com/wcharczuk/go-chart"
2016-07-16 23:53:46 -04:00
)
func drawChart(res http.ResponseWriter, req *http.Request) {
mainSeries := chart.ContinuousSeries{
Name: "A test series",
2019-02-13 21:55:13 -05:00
XValues: chart.SeqRange(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.SeqRandomValuesWithMax(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements.
2016-07-16 23:53:46 -04:00
}
// note we create a SimpleMovingAverage series by assignin the inner series.
// we need to use a reference because `.Render()` needs to modify state within the series.
2016-07-17 14:12:44 -04:00
smaSeries := &chart.SMASeries{
2016-07-16 23:53:46 -04:00
InnerSeries: mainSeries,
} // we can optionally set the `WindowSize` property which alters how the moving average is calculated.
graph := chart.Chart{
Series: []chart.Series{
mainSeries,
smaSeries,
},
}
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
}
func main() {
http.HandleFunc("/", drawChart)
http.ListenAndServe(":8080", nil)
}