-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.go
215 lines (196 loc) · 5.68 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
ole "github.com/go-ole/go-ole"
"github.com/tanaton/go-ole-msoffice/excel"
"path/filepath"
"strings"
)
type ExcelGraph struct {
app *excel.Application
}
type GraphItem struct {
x int
count int
rg *excel.Range
leg []string
}
func main() {
// COMの初期化
ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED|ole.COINIT_DISABLE_OLE1DDE)
// 確実に行う必要があるため
defer ole.CoUninitialize()
// エクセルオブジェクトの生成
e := excel.ThisApplication()
if e == nil {
return
}
ex := ExcelGraph{app: e}
// 生成してる感を出すためアプリケーションを表示する
ex.app.SetVisible(true)
// 既存のブックの読み込み
workbooks := ex.app.GetWorkbooks()
rp, _ := filepath.Abs("data/data.csv")
book := workbooks.Open(rp)
// シートの取得
sheets := book.GetWorksheets()
sheet := sheets.GetItem(1)
// 空グラフの生成
graph := sheet.ChartObjects().Add(30, 30, 500, 300)
graph.SetName("graph.goで生成したグラフ")
// 逐一描画すると遅いので、適当にまとめて描画する
ex.app.SetScreenUpdating(false)
// シート内容をグラフに変換
ex.sheetToChart(graph, sheet, []int{1, 2})
ex.app.SetScreenUpdating(true)
ex.app.SetScreenUpdating(false)
// タイトルを設定
ex.setGraphTitle(graph, "ぶりいくじっと")
// グラフオブジェクトをグラフシートに移動
chart := graph.GetChart()
chart.Location(excel.XlLocationAsNewSheet, "グラフその1")
ex.app.SetScreenUpdating(true)
// ブックを保存
ex.app.SetDisplayAlerts(false)
wp, _ := filepath.Abs("output.xlsx")
book.SaveAs(wp, excel.XlWorkbookDefault)
// ブックを閉じる
book.Close()
// Excelを閉じる
ex.app.Quit()
// メモリ解放みたいな感じ
ex.app.Release()
}
// グラフに描画するためのデータを取得
func (ex *ExcelGraph) getGraphRange(sheet *excel.Worksheet) []GraphItem {
x := 1
arr := []GraphItem{}
maxCol := sheet.GetCells().GetItem(1, sheet.GetColumns().GetCount()).GetEnd(excel.XlToLeft).GetColumn() + 1
// 探索
for {
v := sheet.GetCells().GetItem(1, x).GetValue()
if v.Value() == nil || sheet.Err != nil {
break
}
// 列の探索
leg := []string{}
i := x + 1
for ; i < maxCol; i++ {
v := sheet.GetCells().GetItem(1, i).GetValue()
if v.Value() == nil || sheet.Err != nil {
break
} else {
leg = append(leg, v.ToString())
}
}
item := GraphItem{
x: x, // データ開始位置
count: ((i - 1) - (x + 1)) + 1, // データの列数
rg: sheet.GetRange(sheet.GetColumns().GetItem(x+1), sheet.GetColumns().GetItem(i-1)),
leg: leg,
}
arr = append(arr, item)
x = i + 1
}
return arr
}
// シートからグラフを作る
func (ex *ExcelGraph) sheetToChart(g *excel.ChartObject, sheet *excel.Worksheet, secondary []int) {
j := 1
arr := ex.getGraphRange(sheet)
if len(arr) <= 0 {
fmt.Println("シートにグラフ化できるデータが無いみたい")
return
}
priname := []string{}
secname := []string{}
sec := map[int]struct{}{}
for _, it := range secondary {
sec[it] = struct{}{}
}
// 一つのレンジにまとめる
union := arr[0].rg
for i := 1; i < len(arr); i++ {
union = ex.app.Union(union, arr[i].rg)
}
chart := g.GetChart()
// データの設定
chart.SetSourceData(union, excel.XlColumns)
// グラフの種類を設定
chart.SetChartType(excel.XlXYScatterLinesNoMarkers)
// 凡例の位置を修正
legend := chart.GetLegend()
legend.SetPosition(excel.XlLegendPositionBottom)
// 要素の設定
for _, it := range arr {
xcell := sheet.GetCells().GetItem(2, it.x)
for k := 1; k <= it.count; k++ {
// 線ごとにX軸の設定
sc := chart.SeriesCollection().Item(j)
if _, ok := sec[j]; ok {
// 2軸
sc.SetAxisGroup(excel.XlSecondary)
secname = append(secname, it.leg[k-1])
} else {
priname = append(priname, it.leg[k-1])
}
end := xcell.GetEnd(excel.XlDown)
rg := sheet.GetRange(xcell, end)
sc.SetXValues(rg)
j++
}
}
// X軸の名前を取得
var at string
v := sheet.GetCells().GetItem(1, 1).GetValue()
if v.Value() != nil && sheet.Err == nil {
at = v.ToString()
} else {
at = "横軸"
}
// グラフの軸についての設定
ex.setGraphAxis(g, strings.Join(priname, " / "), at)
// 指定した要素を第二軸へ移動
if len(secondary) > 0 {
ex.setGraphAxisSecondary(g, strings.Join(secname, " / "))
}
}
// グラフの軸を設定
func (ex *ExcelGraph) setGraphAxis(g *excel.ChartObject, name, axistitle string) {
chart := g.GetChart()
cp := chart.Axes(excel.XlCategory, excel.XlPrimary)
vp := chart.Axes(excel.XlValue, excel.XlPrimary)
// X軸の目盛線の表示
cp.SetHasMajorGridlines(true)
cp.SetHasMinorGridlines(true)
// Y軸の目盛線の表示
vp.SetHasMinorGridlines(true)
// 目盛線の位置を下に移動
cp.SetTickLabelPosition(excel.XlTickLabelPositionLow)
// X軸ラベルを表示
cp.SetHasTitle(true)
at := cp.GetAxisTitle()
at.SetText(axistitle)
// Y軸ラベルを表示
vp.SetHasTitle(true)
at = vp.GetAxisTitle()
at.SetText(name)
}
// 指定した要素を第二軸に移動
func (ex *ExcelGraph) setGraphAxisSecondary(g *excel.ChartObject, name string) {
chart := g.GetChart()
// 2軸目のY軸ラベルを表示
vs := chart.Axes(excel.XlValue, excel.XlSecondary)
vs.SetHasTitle(true)
at := vs.GetAxisTitle()
at.SetText(name)
}
// タイトルを設定
func (ex *ExcelGraph) setGraphTitle(g *excel.ChartObject, title string) {
chart := g.GetChart()
chart.SetHasTitle(true)
ct := chart.GetChartTitle()
ct.SetText(title)
ct.SetPosition(excel.XlChartElementPositionAutomatic)
ct.SetIncludeInLayout(false) // タイトルをグラフと重ねる
}