go-chart/examples/linear_regression/main.go

41 lines
1.3 KiB
Go
Raw Normal View History

2016-07-27 15:34:15 -04:00
package main
2019-09-10 00:02:48 -04:00
//go:generate go run main.go
2016-07-27 15:34:15 -04:00
import (
2019-09-10 00:02:48 -04:00
"os"
2016-07-27 15:34:15 -04:00
2024-10-27 22:52:38 -04:00
chart "git.smarteching.com/zeni/go-chart/v2"
2016-07-27 15:34:15 -04:00
)
2019-09-10 00:02:48 -04:00
func main() {
2016-07-27 15:34:15 -04:00
/*
In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValuesProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted.
2016-07-27 15:34:15 -04:00
*/
mainSeries := chart.ContinuousSeries{
Name: "A test series",
2019-09-10 00:02:48 -04:00
XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(1.0).WithEnd(100.0)}.Values(), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements.
YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(100).WithMin(0).WithMax(100)}.Values(), //generates a []float64 randomly from 0 to 100 with 100 elements.
2016-07-27 15:34:15 -04:00
}
// note we create a LinearRegressionSeries series by assignin the inner series.
// we need to use a reference because `.Render()` needs to modify state within the series.
linRegSeries := &chart.LinearRegressionSeries{
InnerSeries: mainSeries,
} // we can optionally set the `WindowSize` property which alters how the moving average is calculated.
graph := chart.Chart{
Series: []chart.Series{
mainSeries,
linRegSeries,
},
}
2019-09-10 00:02:48 -04:00
f, _ := os.Create("output.png")
defer f.Close()
graph.Render(chart.PNG, f)
2016-07-27 15:34:15 -04:00
}