圆环图

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

使用 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. if err := f.AddChart("Sheet1", "E1", `{
  17. "type": "doughnut",
  18. "series": [
  19. {
  20. "name": "Sheet1!$A$2",
  21. "categories": "Sheet1!$A$1:$C$1",
  22. "values": "Sheet1!$A$2:$C$2"
  23. }],
  24. "format":
  25. {
  26. "x_scale": 1.0,
  27. "y_scale": 1.0,
  28. "x_offset": 15,
  29. "y_offset": 10,
  30. "print_obj": true,
  31. "lock_aspect_ratio": false,
  32. "locked": false
  33. },
  34. "legend":
  35. {
  36. "position": "right",
  37. "show_legend_key": false
  38. },
  39. "title":
  40. {
  41. "name": "Fruit Doughnut Chart"
  42. },
  43. "plotarea":
  44. {
  45. "show_bubble_size": false,
  46. "show_cat_name": false,
  47. "show_leader_lines": false,
  48. "show_percent": true,
  49. "show_series_name": false,
  50. "show_val": false
  51. },
  52. "show_blanks_as": "zero"
  53. }`); err != nil {
  54. fmt.Println(err)
  55. }
  56. // 保存工作簿
  57. if err := f.SaveAs("Book1.xlsx"); err != nil {
  58. fmt.Println(err)
  59. }
  60. }