| 1 | package main |
| 2 | |
| 3 | //go:generate go run main.go |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | |
| 8 | chart "github.com/wcharczuk/go-chart/v2" |
| 9 | ) |
| 10 | |
| 11 | func main() { |
| 12 | |
| 13 | /* |
| 14 | In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument. |
| 15 | InnerSeries only needs to implement `ValuesProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted. |
| 16 | */ |
| 17 | |
| 18 | mainSeries := chart.ContinuousSeries{ |
| 19 | Name: "A test series", |
| 20 | 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. |
| 21 | YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(100).WithMin(0).WithMax(100)}.Values(), //generates a []float64 randomly from 0 to 100 with 100 elements. |
| 22 | } |
| 23 | |
| 24 | // note we create a LinearRegressionSeries series by assignin the inner series. |
| 25 | // we need to use a reference because `.Render()` needs to modify state within the series. |
| 26 | linRegSeries := &chart.LinearRegressionSeries{ |
| 27 | InnerSeries: mainSeries, |
| 28 | } // we can optionally set the `WindowSize` property which alters how the moving average is calculated. |
| 29 | |
| 30 | graph := chart.Chart{ |
| 31 | Series: []chart.Series{ |
| 32 | mainSeries, |
| 33 | linRegSeries, |
| 34 | }, |
| 35 | } |
| 36 | |
| 37 | f, _ := os.Create("output.png") |
| 38 | defer f.Close() |
| 39 | graph.Render(chart.PNG, f) |
| 40 | } |