-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchart_generator.go
More file actions
324 lines (290 loc) · 8.61 KB
/
chart_generator.go
File metadata and controls
324 lines (290 loc) · 8.61 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"time"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
"github.com/google/go-github/v57/github"
)
// 生成所有图表
func generateCharts(issues []*github.Issue, outputDirPath string) error {
// 创建图表目录
chartsDir := filepath.Join(outputDirPath, "charts")
if err := os.MkdirAll(chartsDir, 0755); err != nil {
return fmt.Errorf("创建图表目录失败: %w", err)
}
// 生成状态分布图
if err := generateStatusChart(issues, chartsDir); err != nil {
return fmt.Errorf("生成状态分布图失败: %w", err)
}
// 生成标签分布图
if err := generateLabelsChart(issues, chartsDir); err != nil {
return fmt.Errorf("生成标签分布图失败: %w", err)
}
// 生成时间趋势图
if err := generateTimelineChart(issues, chartsDir); err != nil {
return fmt.Errorf("生成时间趋势图失败: %w", err)
}
// 生成图表索引页
if err := generateChartsIndex(chartsDir); err != nil {
return fmt.Errorf("生成图表索引页失败: %w", err)
}
return nil
}
// 生成状态分布图
func generateStatusChart(issues []*github.Issue, chartsDir string) error {
// 统计不同状态的issue数量
statusCount := make(map[string]int)
for _, issue := range issues {
status := issue.GetState()
statusCount[status]++
}
// 创建饼图实例
pie := charts.NewPie()
pie.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Theme: types.ThemeWesteros,
Width: "800px",
Height: "600px",
}),
charts.WithTitleOpts(opts.Title{
Title: "Issues状态分布",
Subtitle: fmt.Sprintf("总数: %d", len(issues)),
}),
charts.WithTooltipOpts(opts.Tooltip{Show: opts.Bool(true)}),
charts.WithLegendOpts(opts.Legend{Show: opts.Bool(true)}),
)
// 准备数据
items := make([]opts.PieData, 0, len(statusCount))
for status, count := range statusCount {
items = append(items, opts.PieData{
Name: status,
Value: count,
})
}
// 添加数据到图表
pie.AddSeries("状态", items).
SetSeriesOptions(
charts.WithLabelOpts(opts.Label{
Show: opts.Bool(true),
Formatter: "{b}: {c} ({d}%)",
}),
charts.WithPieChartOpts(opts.PieChart{
Radius: []string{"40%", "70%"},
}),
)
// 保存图表
f, err := os.Create(filepath.Join(chartsDir, "status_chart.html"))
if err != nil {
return err
}
defer f.Close()
return pie.Render(f)
}
// 生成标签分布图
func generateLabelsChart(issues []*github.Issue, chartsDir string) error {
// 统计不同标签的issue数量
labelCount := make(map[string]int)
for _, issue := range issues {
if len(issue.Labels) == 0 {
labelCount["无标签"]++
continue
}
for _, label := range issue.Labels {
labelName := label.GetName()
labelCount[labelName]++
}
}
// 按数量排序标签
type labelItem struct {
Name string
Count int
}
var labelItems []labelItem
for name, count := range labelCount {
labelItems = append(labelItems, labelItem{Name: name, Count: count})
}
sort.Slice(labelItems, func(i, j int) bool {
return labelItems[i].Count > labelItems[j].Count
})
// 如果标签太多,只取前10个
maxLabels := 10
if len(labelItems) > maxLabels {
labelItems = labelItems[:maxLabels]
}
// 创建柱状图实例
bar := charts.NewBar()
bar.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Theme: types.ThemeWesteros,
Width: "900px",
Height: "500px",
}),
charts.WithTitleOpts(opts.Title{
Title: "Issues标签分布",
Subtitle: fmt.Sprintf("前%d个标签", len(labelItems)),
}),
charts.WithTooltipOpts(opts.Tooltip{Show: opts.Bool(true)}),
charts.WithLegendOpts(opts.Legend{Show: opts.Bool(false)}),
charts.WithXAxisOpts(opts.XAxis{
Name: "标签",
AxisLabel: &opts.AxisLabel{Rotate: 45},
}),
charts.WithYAxisOpts(opts.YAxis{
Name: "数量",
}),
)
// 准备数据
xAxis := make([]string, 0, len(labelItems))
values := make([]opts.BarData, 0, len(labelItems))
for _, item := range labelItems {
xAxis = append(xAxis, item.Name)
values = append(values, opts.BarData{Value: item.Count})
}
// 添加数据到图表
bar.SetXAxis(xAxis).
AddSeries("数量", values).
SetSeriesOptions(
charts.WithLabelOpts(opts.Label{
Show: opts.Bool(true),
Position: "top",
}),
)
// 保存图表
f, err := os.Create(filepath.Join(chartsDir, "labels_chart.html"))
if err != nil {
return err
}
defer f.Close()
return bar.Render(f)
}
// 生成时间趋势图
func generateTimelineChart(issues []*github.Issue, chartsDir string) error {
// 按月统计issue创建数量
monthlyCount := make(map[string]int)
// 找出最早和最晚的日期
var earliestDate, latestDate time.Time
for i, issue := range issues {
createdAt := issue.GetCreatedAt()
if i == 0 || createdAt.Before(earliestDate) {
earliestDate = createdAt.Time
}
if i == 0 || createdAt.After(latestDate) {
latestDate = createdAt.Time
}
}
// 生成所有月份的键
current := time.Date(earliestDate.Year(), earliestDate.Month(), 1, 0, 0, 0, 0, time.UTC)
end := time.Date(latestDate.Year(), latestDate.Month(), 1, 0, 0, 0, 0, time.UTC)
for !current.After(end) {
monthKey := current.Format("2006-01")
monthlyCount[monthKey] = 0
current = current.AddDate(0, 1, 0)
}
// 统计每月的issue数量
for _, issue := range issues {
monthKey := issue.GetCreatedAt().Format("2006-01")
monthlyCount[monthKey]++
}
// 按时间顺序排序月份
var months []string
for month := range monthlyCount {
months = append(months, month)
}
sort.Strings(months)
// 创建折线图实例
line := charts.NewLine()
line.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Theme: types.ThemeWesteros,
Width: "1000px",
Height: "500px",
}),
charts.WithTitleOpts(opts.Title{
Title: "Issues创建时间趋势",
Subtitle: fmt.Sprintf("从 %s 到 %s", earliestDate.Format("2006-01"), latestDate.Format("2006-01")),
}),
charts.WithTooltipOpts(opts.Tooltip{Show: opts.Bool(true)}),
charts.WithLegendOpts(opts.Legend{Show: opts.Bool(true)}),
charts.WithXAxisOpts(opts.XAxis{
Name: "月份",
AxisLabel: &opts.AxisLabel{Rotate: 45},
}),
charts.WithYAxisOpts(opts.YAxis{
Name: "数量",
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "inside",
Start: 0,
End: 100,
}),
charts.WithDataZoomOpts(opts.DataZoom{
Type: "slider",
Start: 0,
End: 100,
}),
)
// 准备数据
values := make([]opts.LineData, 0, len(months))
for _, month := range months {
values = append(values, opts.LineData{Value: monthlyCount[month]})
}
// 添加数据到图表
line.SetXAxis(months).
AddSeries("新建Issues", values).
SetSeriesOptions(
charts.WithLineChartOpts(opts.LineChart{
Smooth: opts.Bool(true),
}),
charts.WithLabelOpts(opts.Label{
Show: opts.Bool(true),
}),
charts.WithMarkPointNameTypeItemOpts(
opts.MarkPointNameTypeItem{Name: "最大值", Type: "max"},
opts.MarkPointNameTypeItem{Name: "最小值", Type: "min"},
),
charts.WithMarkLineNameTypeItemOpts(
opts.MarkLineNameTypeItem{Name: "平均值", Type: "average"},
),
)
// 保存图表
f, err := os.Create(filepath.Join(chartsDir, "timeline_chart.html"))
if err != nil {
return err
}
defer f.Close()
return line.Render(f)
}
// 生成图表索引页
func generateChartsIndex(chartsDir string) error {
page := components.NewPage()
page.SetLayout(components.PageFlexLayout)
// 创建HTML内容
content := `
<div style='margin: 20px; text-align: center;'>
<h1>GitHub Issues 图表分析</h1>
<div style="display: flex; flex-direction: column; gap: 15px; margin-top: 30px;">
<a href="status_chart.html" style="font-size: 18px; padding: 10px; background-color: #f0f0f0; border-radius: 5px; text-decoration: none; color: #333;">状态分布图</a>
<a href="labels_chart.html" style="font-size: 18px; padding: 10px; background-color: #f0f0f0; border-radius: 5px; text-decoration: none; color: #333;">标签分布图</a>
<a href="timeline_chart.html" style="font-size: 18px; padding: 10px; background-color: #f0f0f0; border-radius: 5px; text-decoration: none; color: #333;">时间趋势图</a>
</div>
</div>
`
// 使用自定义HTML内容
custom := charts.NewCustom()
custom.AddCustomizedHeaders(content)
page.AddCharts(custom)
page.SetPageTitle("GitHub Issues 图表分析")
// 保存索引页
f, err := os.Create(filepath.Join(chartsDir, "index.html"))
if err != nil {
return err
}
defer f.Close()
return page.Render(f)
}