# 使用GDI+画2D饼状图(VB.Net)

Public Class Form1

'/*************************design by 撒卡*************************/
'/功能:使用GDI画饼图demo
'/时间:07.09.30
'/****************************************************************/

Private Const Pi As Single = 3.1415926

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With PicShow
'.Top = 10
'.Left = 10
'.Width = 300
'.Height = 300
.Dock = DockStyle.Fill
End With
End Sub

Private Sub Mypaint(ByVal e As PaintEventArgs)

Dim MyCircle As Graphics = e.Graphics
Dim Myline As Graphics = e.Graphics
Dim MyPen As New Pen(Color.Black)
Dim myRect As New Rectangle(50, 50, 200, 200)
'MyCircle.DrawEllipse(MyPen, myRect)  '边框,被覆盖
Myline.DrawLine(MyPen, 50, 150, 150, 150)

'划分区间
Dim sRectPer(2) As Single, x1(2) As Single, y1(2) As Single '定义的数组数为区间数-1
sRectPer(0) = Radian("30", "30", "30", "40")
x1(0) = 150 - Math.Cos(sRectPer(0)) * 100 '半径为100
y1(0) = 150 - Math.Sin(sRectPer(0)) * 100
Myline.DrawLine(MyPen, x1(0), y1(0), 150, 150)

x1(1) = 150 - Math.Cos(sRectPer(1)) * 100
y1(1) = 150 - Math.Sin(sRectPer(1)) * 100
Myline.DrawLine(MyPen, x1(1), y1(1), 150, 150)

'填充区间
Dim x2(3) As Single, y2(3) As Single   '数组大小等于区间数
For i As Double = 0 To sRectPer(0) Step 0.0001
x2(0) = 150 - Math.Cos(i) * 100
y2(0) = 150 - Math.Sin(i) * 100
Myline.DrawLine(Pens.Magenta, x2(0), y2(0), 150, 150)
Next

For j As Double = sRectPer(0) To sRectPer(1) Step 0.0001
x2(1) = 150 - Math.Cos(j) * 100
y2(1) = 150 - Math.Sin(j) * 100
Myline.DrawLine(Pens.SeaGreen, x2(1), y2(1), 150, 150)
Next
Dim newline As Graphics = e.Graphics
For k As Double = sRectPer(1) To Pi * 2 Step 0.0001
x2(2) = 150 - Math.Cos(k) * 100
y2(2) = 150 - Math.Sin(k) * 100
newline.DrawLine(Pens.Orange, x2(2), y2(2), 150, 150)
Next
'Dim a As Single = percentage("30", "30", "30", "40")
'Debug.Print(a.ToString)

'标示区间
With lblrectA
.Top = (150 + y1(0)) * 0.5
.Left = (50 + x1(0)) * 0.3
.Text = "组分A比例:" & percentage("30", "30", "30", "40")
.BackColor = Color.Magenta
.ForeColor = Color.White
End With
With lblrectB
.Top = (y1(0) + y1(1)) * 0.5
.Left = (x1(0) + x1(1)) * 0.4
.Text = "组分B比例:" & percentage("30", "30", "30", "40")
.BackColor = Color.SeaGreen
.ForeColor = Color.White
End With
With lblrectC
.Top = (150 + y1(1)) * 0.5
.Left = (50 + x1(1)) * 0.3
.Text = "组分C比例:" & percentage("40", "30", "30", "40")
.BackColor = Color.Orange
.ForeColor = Color.White
End With

MyPen.Dispose()
End Sub

Private Sub PicShow_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PicShow.Paint
Mypaint(e)
End Sub

Private Function percentage(ByVal rect As Integer, ByVal ParamArray rectall() As Integer) As Single
Dim sum As Int64
For i As Integer = 0 To UBound(rectall, 1)
sum += rectall(i)
Next
percentage = rect / sum
End Function

Private Function Radian(ByVal rect As Integer, ByVal ParamArray rectall() As Integer) As Double
radian = percentage(rect, rectall) * Pi * 2
End Function
End Class

• 本文已收录于以下专栏：

## 使用VB.Net GDI+编程编写的3D魔方游戏程序

• yctccg
• 2016年08月16日 10:40
• 269

## [vb.net]使用GDI+绘图达到图片随鼠标缩放与移动的效果

[vb.net]使用GDI+绘图达到图片随鼠标缩放与移动的效果近日需要在软件中加入对图片的浏览功能，类似地图的拖拽及缩放操作。 网上大部分教程写的不够详细，对于初学者来讲不易理解。总结下要点，有二： ...

## vb.net GDI+入门——了解Font类

Font类用于指定文本格式，就像我们在其它应用程序中设置字体、字号等。 Font的构造函数语法为： Public sub New (byval prototype As Font,byval ne...

## vb.net GDI+入门——画笔、画刷和颜色

GDI是Graphics Device Interface的缩写，含义是图形设备接口，它的主要任务是负责系统与绘图程序之间的信息交换，处理所有Windows程序的图形输出。顾名思义，GDI+就是GDI...

## C# 使用GDI+绘制柱状图

• 2013年09月14日 01:42
• 49KB
• 下载

举报原因： 您举报文章：使用GDI+画2D饼状图(VB.Net) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)