具体代码来源于:vb.net 教程 5-12 绘图实例之统计图 3 饼图_VB.Net的博客-CSDN博客
我给这位大佬进行了改进
具体代码:
Public Function PieChart(s As String(), value As Integer(), c As Color())
Dim v As Integer = 0
For i As Integer = 0 To value.Count - 1
v += value(i)
Next
Dim Angle(value.Count) As Integer
Angle(0) = 0
For i = 0 To value.Count - 1
Angle(i + 1) = value(i) * 360 \ v + Angle(i)
Next
Angle(value.Count) = 360
For i As Integer = 0 To value.Count - 1
g.FillPie(New SolidBrush(c(i)), New Rectangle(100, 100, 200, 200), Angle(i), Angle(i + 1) - Angle(i))
g.FillRectangle(New SolidBrush(c(i)), New Rectangle(360, i * 50 + 80, 60, 40))
g.DrawString(s(i), New Font("宋体", 12), New SolidBrush(Color.Black), New Point(450, i * 50 + 90))
g.DrawString("= " & value(i), New Font("宋体", 12), New SolidBrush(Color.Black), New Point(470, i * 50 + 90))
Next
Return Image
End Function
之前写过的代码,重新在来看一遍:
Protected Image As Bitmap
Protected g As Graphics
''' <summary>
''' 单独调用函数,只执行创建任务;如果需要得到图片,则会返回一个图片(bitmap)
''' </summary>
''' <param name="bmpsize">新建的图片大小(size)</param>
''' <param name="backColor">新建的图片背景颜色(color)</param>
''' <returns>图片</returns>
Public Function Createbmp(bmpsize As Size, backColor As Color)
Image = New Bitmap(bmpsize.Width, bmpsize.Height)
g = Graphics.FromImage(Image)
g.Clear(backColor)
Return Image
End Function
这一部分一定要自己理解
主窗体:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
GDI.Createbmp(New Size(900, 900), Color.Transparent)
Dim s As String() = {"a", "b", "c", "d", "e"}
Dim value As Integer() = {1, 2, 3, 4, 5}
Dim c As Color() = {Color.Black, Color.Red, Color.Green, Color.Gray, Color.Yellow}
BackgroundImage = GDI.PieChart(s, value, c)
End Sub
效果:
等于号什么的可以自己修改哈,理解即可