三维簇状柱形图

例如,创建如下效果的三维簇状柱形图:

使用 Go 语言在 Excel 文档中创建三维簇状柱形图

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. )
  6. func main() {
  7. f := excelize.NewFile()
  8. defer func() {
  9. if err := f.Close(); err != nil {
  10. fmt.Println(err)
  11. }
  12. }()
  13. for idx, row := range [][]interface{}{
  14. {nil, "Apple", "Orange", "Pear"},
  15. {"Small", 2, 3, 3},
  16. {"Normal", 5, 2, 4},
  17. {"Large", 6, 7, 8},
  18. } {
  19. cell, err := excelize.CoordinatesToCellName(1, idx+1)
  20. if err != nil {
  21. fmt.Println(err)
  22. return
  23. }
  24. if err := f.SetSheetRow("Sheet1", cell, &row); err != nil {
  25. fmt.Println(err)
  26. return
  27. }
  28. }
  29. max, min := 7.5, 0.5
  30. if err := f.AddChart("Sheet1", "E1", &excelize.Chart{
  31. Type: excelize.Col3DClustered,
  32. Series: []excelize.ChartSeries{
  33. {
  34. Name: "Sheet1!$A$2",
  35. Categories: "Sheet1!$B$1:$D$1",
  36. Values: "Sheet1!$B$2:$D$2",
  37. },
  38. {
  39. Name: "Sheet1!$A$3",
  40. Categories: "Sheet1!$B$1:$D$1",
  41. Values: "Sheet1!$B$3:$D$3",
  42. },
  43. {
  44. Name: "Sheet1!$A$4",
  45. Categories: "Sheet1!$B$1:$D$1",
  46. Values: "Sheet1!$B$4:$D$4",
  47. },
  48. },
  49. Format: excelize.GraphicOptions{
  50. OffsetX: 15,
  51. OffsetY: 10,
  52. },
  53. Title: []excelize.RichTextRun{
  54. {
  55. Text: "Fruit 3D Clustered Column Chart",
  56. },
  57. },
  58. PlotArea: excelize.ChartPlotArea{
  59. ShowCatName: false,
  60. ShowLeaderLines: false,
  61. ShowPercent: true,
  62. ShowSerName: true,
  63. ShowVal: true,
  64. },
  65. ShowBlanksAs: "zero",
  66. XAxis: excelize.ChartAxis{
  67. ReverseOrder: true,
  68. },
  69. YAxis: excelize.ChartAxis{
  70. Maximum: &max,
  71. Minimum: &min,
  72. },
  73. }); err != nil {
  74. fmt.Println(err)
  75. return
  76. }
  77. // 保存工作簿
  78. if err := f.SaveAs("Book1.xlsx"); err != nil {
  79. fmt.Println(err)
  80. }
  81. }