趋明 / 编程开发 / 使用vb.net绘制简单的饼图,柱状图,折线图

分享

   

使用vb.net绘制简单的饼图,柱状图,折线图

2012-03-12  趋明

 

关键词使用vb.net绘制简单的饼图                                          

=================Part1==================

       TestDrawGraphic.aspx:在网页上放三个按钮,定义其单击事件到后台写代码

 

'=================Part2==================

'TestDrawGraphic.aspx.vb

'使用vb.net绘制简单的饼图,柱状图,折线图

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.imaging


Public Class testOWC
    Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents Button3 As System.Web.UI.WebControls.Button

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Sub createBG()        '绘制饼图
        Dim height As Integer = 200
        Dim width As Integer = 200
        Dim bmp As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim pen As Pen = New Pen(Color.Black)

        Dim outline As Rectangle = New Rectangle(0, 0, height - 5, width - 5)

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.Clear(Color.White)

        '绘制饼图
        '绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆
        g.DrawEllipse(pen, outline)
        '填充由一对坐标、一个宽度、一个高度以及两条射线指定的椭圆所定义的扇形区的内部
        '关键在第二,三个参数:从x轴到扇形边1的度数;从扇形边1到边2的读书,下面绘制了各占90度的图
        'g.FillPie(New SolidBrush(Color.Red), outline, -20.0F, 90.0F)
        'g.FillPie(New SolidBrush(Color.Yellow), outline, 70.0F, 150.0F)
        'g.FillPie(New SolidBrush(Color.Blue), outline, 220.0F, 100.0F)
        'g.FillPie(New SolidBrush(Color.Green), outline, 320.0F, 20.0F)
        g.FillPie(New SolidBrush(Color.Red), outline, 0.0F, 90.0F)
        g.FillPie(New SolidBrush(Color.Yellow), outline, 90.0F, 90.0F)
        g.FillPie(New SolidBrush(Color.Blue), outline, 180.0F, 90.0F)
        g.FillPie(New SolidBrush(Color.Green), outline, 270.0F, 90.0F)

        bmp.Save(Page.Response.OutputStream, ImageFormat.Jpeg)

        g.Dispose()
        bmp.Dispose()
    End Sub
    Sub createZG()
        Dim height As Integer = 200
        Dim width As Integer = 200
        Dim space As Integer = 20 '原点到左边和下边的距离
        Dim interval As Integer = 20 '单位长度
        Dim max_x As Integer = 8 'x轴最大刻度
        Dim max_y As Integer = 8
        Dim bmp As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim pen As Pen = New Pen(Color.Black, 1)
        Dim pen_chart As Pen = New Pen(Color.Blue, 12)

        '定义一组数据
        Dim arrData() As Double = {5.21, 3.34, 2.5, 7.65, 6.54, 1.2, 2.32, 6.48}
        Dim i As Integer

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.Clear(Color.White)

        'g.DrawLine(pen,point1,point2)
        'Pen 对象,它确定线条的颜色、宽度和样式。
        'Point 结构,它表示要连接的第一个点。
        'Point 结构,它表示要连接的第二个点。
        g.DrawLine(pen, New Point(space, height - space), New Point(width, height - space)) 'x轴
        g.DrawLine(pen, New Point(space, 0), New Point(space, height - space))  'y轴

        'x轴上的刻度
        For i = 0 To width - interval Step interval
            If i <= max_x * interval Then
                'g.DrawString(string,Font,Brush,PointF)
                'String对象,要绘制的字符串。
                'Font 对象,它定义字符串的文本格式。
                'Brush 对象,它确定所绘制文本的颜色和纹理。
                'PointF 结构,它指定所绘制文本的左上角。
                '可以单独先定义变量drawString, drawFont, drawBrush, drawPoint
                g.DrawString(i / interval, New Font("Arail", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space + i - 5, height - space))
            End If
        Next

        'y轴上的刻度
        For i = 0 To height - interval Step interval
            If i <= max_y * interval Then
                g.DrawLine(pen, New Point(space, height - i - space), New Point(space + 5, height - i - space))

                If i <> 0 Then
                    g.DrawString(i / interval, New Font("Arial", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space - 12, height - space - i - 6))
                End If
            End If
        Next

        '柱形图
        For i = 0 To UBound(arrData)
            g.DrawLine(pen_chart, New Point(space + (i + 1) * interval, height - space - arrData(i) * interval), New Point(space + (i + 1) * interval, height - space))

        Next

        '输出结果
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg)

        g.Dispose()
        bmp.Dispose()
    End Sub
    Sub createZXG()
        Dim height As Integer = 200
        Dim width As Integer = 200
        Dim space As Integer = 20 '原点到左边和下边的距离
        Dim interval As Integer = 20 '单位长度
        Dim max_x As Integer = 8 'x轴最大刻度
        Dim max_y As Integer = 8
        Dim bmp As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim pen As Pen = New Pen(Color.Black, 1)
        Dim pen_chart As Pen = New Pen(Color.Blue, 2)    '在此处设置折线宽度和颜色

        '定义一组数据
        Dim arrData() As Double = {5.21, 3.34, 2.5, 7.65, 6.54, 1.2, 2.32, 6.48}
        Dim i As Integer

        g.SmoothingMode = SmoothingMode.AntiAlias
        g.Clear(Color.White)

        'g.DrawLine(pen,point1,point2)
        'Pen 对象,它确定线条的颜色、宽度和样式。
        'Point 结构,它表示要连接的第一个点。
        'Point 结构,它表示要连接的第二个点。
        g.DrawLine(pen, New Point(space, height - space), New Point(width, height - space)) 'x轴
        g.DrawLine(pen, New Point(space, 0), New Point(space, height - space))  'y轴

        'x轴上的刻度
        For i = 0 To width - interval Step interval
            If i <= max_x * interval Then
                'g.DrawString(string,Font,Brush,PointF)
                'String对象,要绘制的字符串。
                'Font 对象,它定义字符串的文本格式。
                'Brush 对象,它确定所绘制文本的颜色和纹理。
                'PointF 结构,它指定所绘制文本的左上角。
                '可以单独先定义变量drawString, drawFont, drawBrush, drawPoint
                g.DrawString(i / interval, New Font("Arail", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space + i - 5, height - space))
            End If
        Next

        'y轴上的刻度
        For i = 0 To height - interval Step interval
            If i <= max_y * interval Then
                g.DrawLine(pen, New Point(space, height - i - space), New Point(space + 5, height - i - space))

                If i <> 0 Then
                    g.DrawString(i / interval, New Font("Arial", 9, FontStyle.Regular), SystemBrushes.WindowText, New PointF(space - 12, height - space - i - 6))
                End If
            End If
        Next
        '-----------------------------------以上同画柱状图---------------------------------------------
        '画折线图
        For i = 0 To UBound(arrData) - 1
            g.DrawLine(pen_chart, New Point(space + i * interval, height - space - arrData(i) * interval), New Point(space + (i + 1) * interval, height - space - arrData(i + 1) * interval))
            'g.DrawLine(pen_chart, New Point(space + (i + 1) * interval, height - space - arrData(i) * interval), New Point(space + (i + 1) * interval, height - space))
        Next

        bmp.Save(Response.OutputStream, ImageFormat.Jpeg)

        g.Dispose()
        bmp.Dispose()

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        createBG()   '画饼图
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        createZG()   '画柱状图
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        createZXG() '画折线图
    End Sub
End Class

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多
    喜欢该文的人也喜欢 更多

    ×
    ×

    ¥.00

    微信或支付宝扫码支付:

    开通即同意《个图VIP服务协议》

    全部>>