package wps import ( "fmt" "math" "github.com/fogleman/gg" ) func Weekly_word() { const ( width, height = 900, 700 centerX, centerY = width / 2, height / 2 radius = 180 ) // 初始化绘图上下文 dc := gg.NewContext(width, height) dc.SetRGB(1, 1, 1) // 设置背景为白色 dc.Clear() // 数据示例:每个部分的角度、标签和颜色 data := []struct { angle float64 label string color string }{ {275, "1、敏感信息泄露:275", "#FF6384"}, // 红色 {75, "2、代码执行:75", "#36A2EB"}, // 蓝色 {68, "3、命令注入:68", "#FFCE56"}, // 黄色 {47, "4、目录遍历:47", "#4bc0c0"}, // 绿色 {28, "5、安全措施绕过:28", "#c12c1f"}, // 红色 {12, "6、HTTP弱口令尝试:12", "#779649"}, // 绿色 {10, "7、SQL注入:10", "#9BA0C9"}, // 绿色 } // 加载支持中文的字体 fontPath := "font/SIMFANG.TTF" // 确保字体文件在当前目录或指定路径下 fontSize := 16.0 if err := dc.LoadFontFace(fontPath, fontSize); err != nil { panic(err) } // 绘制图表标题 dc.SetRGB(0, 0, 0) dc.DrawString("安全事件类型分布Top10", float64(centerX-80), float64(centerY-250)) dc.DrawString("安全事件类型分布Top10", float64(centerX-400), float64(centerY-300)) // 绘制圆环图表 startAngle := 0.0 for _, d := range data { endAngle := startAngle + d.angle drawDonutSegment(dc, centerX, centerY, radius, startAngle, endAngle, d.color) startAngle = endAngle } // 在右侧绘制数据标签 labelX := float64(centerX + radius + 50) labelY := float64(centerY - (len(data)*20)/2) for _, d := range data { // 绘制颜色一致的小圆点 drawColorDot(dc, labelX, labelY, d.color) // 绘制标签文本 dc.DrawString(d.label, labelX+15, labelY) labelY += 20 } // 保存为图片 dc.SavePNG("donut_chart_with_labels.png") fmt.Println("圆环图表已保存为 donut_chart_with_labels.png") } // 绘制圆环的部分 func drawDonutSegment(dc *gg.Context, cx, cy, radius float64, startAngle, endAngle float64, color string) { dc.SetHexColor(color) dc.NewSubPath() dc.DrawArc(cx, cy, radius, startAngle*math.Pi/180, endAngle*math.Pi/180) dc.DrawArc(cx, cy, radius-50, endAngle*math.Pi/180, startAngle*math.Pi/180) dc.ClosePath() dc.Fill() } // 绘制颜色一致的小圆点 func drawColorDot(dc *gg.Context, x, y float64, color string) { dc.SetHexColor(color) dc.DrawCircle(x, y, 5) dc.Fill() }