main.go 1008 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. import (
  3. "github.com/go-echarts/go-echarts/v2/charts"
  4. "github.com/go-echarts/go-echarts/v2/opts"
  5. "math/rand"
  6. "os"
  7. )
  8. // generate random data for line chart
  9. func generateLineItems2() []opts.LineData {
  10. items := make([]opts.LineData, 0)
  11. for i := 0; i < 7; i++ {
  12. items = append(items, opts.LineData{Value: rand.Intn(300)})
  13. }
  14. return items
  15. }
  16. func main() {
  17. // 1.New 一个折线图对象
  18. line := charts.NewLine()
  19. // 2.设置 标题 和 子标题
  20. line.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
  21. Title: "业务费/能量值消费趋势",
  22. //Subtitle: "It's extremely easy to use, right?",
  23. }))
  24. // 3.设置 数据组
  25. line.SetXAxis([]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}).
  26. AddSeries("Category A", generateLineItems2()).
  27. AddSeries("Category B", generateLineItems2()).
  28. SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{Smooth: true}))
  29. // 4.绘图 生成html
  30. //f, _ := os.Create("lines.html")
  31. f, _ := os.Create("lines.png")
  32. line.Render(f)
  33. }