|
| 1 | +# 开盘价-最高价-最低价-收盘价股价图 {#stockOpenHighLowClose} |
| 2 | + |
| 3 | +例如,创建如下效果的开盘价-最高价-最低价-收盘价股价图: |
| 4 | + |
| 5 | +<p align="center"><img width="823" src="../images/stock_open_high_low_close_chart.png" alt="使用 Go 语言在 Excel 文档中创建开盘价-最高价-最低价-收盘价股价图"></p> |
| 6 | + |
| 7 | +```go |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/xuri/excelize/v2" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + f := excelize.NewFile() |
| 18 | + defer func() { |
| 19 | + if err := f.Close(); err != nil { |
| 20 | + fmt.Println(err) |
| 21 | + } |
| 22 | + }() |
| 23 | + for idx, row := range [][]interface{}{ |
| 24 | + {"日期", "开盘价", "最高价", "最低价", "收盘价"}, |
| 25 | + {45593, 431.66, 431.94, 426.3, 426.59}, |
| 26 | + {45590, 426.76, 432.52, 426.57, 428.15}, |
| 27 | + {45589, 425.33, 425.98, 422.4, 424.73}, |
| 28 | + {45588, 430.86, 431.08, 422.53, 424.6}, |
| 29 | + {45587, 418.49, 430.58, 418.04, 427.51}, |
| 30 | + {45586, 416.12, 418.96, 413.75, 418.78}, |
| 31 | + {45583, 417.14, 419.65, 416.26, 418.16}, |
| 32 | + {45582, 422.36, 422.5, 415.59, 416.72}, |
| 33 | + {45581, 415.17, 416.36, 410.48, 416.12}, |
| 34 | + {45580, 422.18, 422.48, 415.26, 418.74}, |
| 35 | + {45579, 417.77, 424.04, 417.52, 419.14}, |
| 36 | + {45576, 416.14, 417.13, 413.25, 416.32}, |
| 37 | + {45575, 415.23, 417.35, 413.15, 415.84}, |
| 38 | + {45574, 415.86, 420.38, 414.3, 417.46}, |
| 39 | + {45573, 410.9, 415.66, 408.17, 414.71}, |
| 40 | + {45572, 416, 417.11, 409, 409.54}, |
| 41 | + {45569, 418.24, 419.75, 414.97, 416.06}, |
| 42 | + {45568, 417.63, 419.55, 414.29, 416.54}, |
| 43 | + {45567, 422.58, 422.82, 416.71, 417.13}, |
| 44 | + {45566, 428.45, 428.48, 418.81, 420.69}, |
| 45 | + {45565, 428.21, 430.42, 425.37, 430.3}, |
| 46 | + } { |
| 47 | + cell, err := excelize.CoordinatesToCellName(1, idx+1) |
| 48 | + if err != nil { |
| 49 | + fmt.Println(err) |
| 50 | + return |
| 51 | + } |
| 52 | + if err := f.SetSheetRow("Sheet1", cell, &row); err != nil { |
| 53 | + fmt.Println(err) |
| 54 | + return |
| 55 | + } |
| 56 | + } |
| 57 | + if err := f.AddChart("Sheet1", "F1", &excelize.Chart{ |
| 58 | + Type: excelize.StockOpenHighLowClose, |
| 59 | + Series: []excelize.ChartSeries{ |
| 60 | + { |
| 61 | + Name: "Sheet1!$B$1", |
| 62 | + Categories: "Sheet1!$A$2:$A$22", |
| 63 | + Values: "Sheet1!$B$2:$B$22", |
| 64 | + }, |
| 65 | + { |
| 66 | + Name: "Sheet1!$C$1", |
| 67 | + Categories: "Sheet1!$A$2:$A$22", |
| 68 | + Values: "Sheet1!$C$2:$C$22", |
| 69 | + }, |
| 70 | + { |
| 71 | + Name: "Sheet1!$D$1", |
| 72 | + Categories: "Sheet1!$A$2:$A$22", |
| 73 | + Values: "Sheet1!$D$2:$D$22", |
| 74 | + }, |
| 75 | + { |
| 76 | + Name: "Sheet1!$E$1", |
| 77 | + Categories: "Sheet1!$A$2:$A$22", |
| 78 | + Values: "Sheet1!$E$2:$E$22", |
| 79 | + }, |
| 80 | + }, |
| 81 | + Legend: excelize.ChartLegend{Position: "none"}, |
| 82 | + Title: []excelize.RichTextRun{ |
| 83 | + {Text: "开盘价-最高价-最低价-收盘价股价图"}, |
| 84 | + }, |
| 85 | + XAxis: excelize.ChartAxis{ |
| 86 | + NumFmt: excelize.ChartNumFmt{CustomNumFmt: "yyyy年m月d日"}, |
| 87 | + }, |
| 88 | + PlotArea: excelize.ChartPlotArea{ |
| 89 | + UpBars: excelize.ChartUpDownBar{ |
| 90 | + Border: excelize.ChartLine{Type: excelize.ChartLineNone}, |
| 91 | + Fill: excelize.Fill{ |
| 92 | + Type: "pattern", Color: []string{"00B050"}, Pattern: 1, |
| 93 | + }, |
| 94 | + }, |
| 95 | + DownBars: excelize.ChartUpDownBar{ |
| 96 | + Border: excelize.ChartLine{Type: excelize.ChartLineNone}, |
| 97 | + Fill: excelize.Fill{ |
| 98 | + Type: "pattern", Color: []string{"FF0000"}, Pattern: 1, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }); err != nil { |
| 103 | + fmt.Println(err) |
| 104 | + return |
| 105 | + } |
| 106 | + // 保存工作簿 |
| 107 | + if err := f.SaveAs("Book1.xlsx"); err != nil { |
| 108 | + fmt.Println(err) |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
0 commit comments