vb.net 绘制饼图(类库——13)

20 篇文章 1 订阅
17 篇文章 1 订阅

具体代码来源于: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

效果:

582a2d806958404aa4e911e442c71c20.png

等于号什么的可以自己修改哈,理解即可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值