VB.NET中用GDI+画饼图

Public Class Form1
    Private Const Pi As Single = 3.1415926

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawing.Click
        'Debug.Print("tttt")

    End Sub

    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
        Me.Controls.Add(PicShow)
    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)

        sRectPer(1) = Radian("60", "30", "30", "40") 'srectPer(n)的radian函数的第一个参数为前n+1个参数的和
        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 System.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)