圆环图

例如,创建如下效果的圆环图:

使用 Go 语言在 Excel 文档中创建圆环图

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/360EntSecGroup-Skylar/excelize"
  5. )
  6. func main() {
  7. categories := map[string]string{"A1": "Apple", "B1": "Orange", "C1": "Pear"}
  8. values := map[string]int{"A2": 2, "B2": 3, "C2": 3}
  9. f := excelize.NewFile()
  10. for k, v := range categories {
  11. f.SetCellValue("Sheet1", k, v)
  12. }
  13. for k, v := range values {
  14. f.SetCellValue("Sheet1", k, v)
  15. }
  16. err := f.AddChart("Sheet1", "E1", `{"type":"doughnut","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$A$1:$C$1","values":"Sheet1!$A$2:$C$2"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"right","show_legend_key":false},"title":{"name":"Fruit Doughnut Chart"},"plotarea":{"show_bubble_size":false,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"zero"}`)
  17. if err != nil {
  18. fmt.Println(err)
  19. }
  20. // 保存工作簿
  21. err = f.SaveAs("./Book1.xlsx")
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. }