关闭

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

标签: vb.netgdi+integerfunctionnewlineclass
1388人阅读 评论(0) 收藏 举报
分类:
很久没写过本机windows App了,群内有人要实现花饼图的代码,而且不能用水晶报表,恰逢10.1,自己写了个实现。

新建一个windows app工程,在窗体上填加一个picturebox(picshow)和三个label(lblrectA,B,C)
在窗体代码页中他填加下面的代码:

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
        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 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


函数过程并没有进行优化,很多代码还是可以抽象出函数过程的,比如填充部分.如果你在自己项目中有应用,那你自己修改吧~当然也可以设计成一个class,时间原因,我没有那么做,你可以试下,很简单的东西,不多说了。帖个效果图
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43076次
    • 积分:638
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:1篇
    • 译文:3篇
    • 评论:5条
    最新评论
    我的生活BLOG