二维百分比堆积面积图

例如,创建如下效果的二维百分比堆积面积图:

使用 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": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
  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", `{"type":"areaPercentStacked","series":[{"name":"Sheet1!$A$2","categories":"","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"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":"bottom","show_legend_key":false},"title":{"name":"二维百分比堆积面积图"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`); err != nil {
  17. fmt.Println(err)
  18. }
  19. // 保存工作簿
  20. if err := f.SaveAs("Book1.xlsx"); err != nil {
  21. fmt.Println(err)
  22. }
  23. }