二维面积图

例如,创建如下效果的二维面积图:

使用 Go 语言在 Excel 文档中创建二维面积图

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/360EntSecGroup-Skylar/excelize"
  5. )
  6. func main() {
  7. categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
  8. values := map[string]int{"B2": 7, "C2": 7, "D2": 8, "B3": 5, "C3": 4, "D3": 4, "B4": 2, "C4": 3, "D4": 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": "area",
  18. "series": [
  19. {
  20. "name": "Sheet1!$A$2",
  21. "categories": "",
  22. "values": "Sheet1!$B$2:$D$2"
  23. },
  24. {
  25. "name": "Sheet1!$A$3",
  26. "categories": "Sheet1!$B$1:$D$1",
  27. "values": "Sheet1!$B$3:$D$3"
  28. },
  29. {
  30. "name": "Sheet1!$A$4",
  31. "categories": "Sheet1!$B$1:$D$1",
  32. "values": "Sheet1!$B$4:$D$4"
  33. }],
  34. "format":
  35. {
  36. "x_scale": 1.0,
  37. "y_scale": 1.0,
  38. "x_offset": 15,
  39. "y_offset": 10,
  40. "print_obj": true,
  41. "lock_aspect_ratio": false,
  42. "locked": false
  43. },
  44. "legend":
  45. {
  46. "position": "bottom",
  47. "show_legend_key": false
  48. },
  49. "title":
  50. {
  51. "name": "Fruit 2D Area Chart"
  52. },
  53. "plotarea":
  54. {
  55. "show_bubble_size": true,
  56. "show_cat_name": false,
  57. "show_leader_lines": false,
  58. "show_percent": true,
  59. "show_series_name": true,
  60. "show_val": true
  61. },
  62. "show_blanks_as": "zero"
  63. }`); err != nil {
  64. fmt.Println(err)
  65. }
  66. // 保存工作簿
  67. if err := f.SaveAs("Book1.xlsx"); err != nil {
  68. fmt.Println(err)
  69. }
  70. }