VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.NET 使用CHART控件做实时曲线

VB.NET 使用CHART 控件做实时曲线

本人初学vb.net 编程,因一工程需要将现场数据采集过来,并以实时曲线的方式,显示出来,顾写了个例子程序,供大家参考,不对的地方请大家指正,谢谢!
这是正常运行后的效果图
废话不多说,下面是整个代码,希望能够帮到大家。


Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1
    '//定义曲线1X轴变量为时间
    Private X1(19) As String

    '//定义曲线1Y轴变量为瞬时流量
    Private Y1(19) As Double

    '//定义曲线2X轴变量为时间
    Private X2(19) As String

    '//定义曲线2Y轴变量为瞬时流量
    Private Y2(19) As Double

    '//定义画图区域变量
    Private fluxArea As ChartArea

    '//定义Serial对象
    Private fluxLine1 As Series
    Private fluxLine2 As Series

    '//定义Legends对象
    Private fluxLegend As Legend

    Private Sub InitChartSet()
        '//初始化Chart对象设置

        '//设置控件背景色
        Chart1.BackColor = Color.Black

        '//定义标题对象变量
        Dim ChartTitle As New Title

        '//设置Title信息
        With ChartTitle
            .Text = "皮带秤流量实时曲线"
            .ForeColor = Color.Yellow
            .Font = New System.Drawing.Font("微软雅黑", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        End With

        '//清除所有标题对象
        Chart1.Titles.Clear()
        Chart1.Titles.Add(ChartTitle)

        '//清空画图区域
        Chart1.ChartAreas.Clear()

        '//创建chartarea对象实例
        fluxArea = New ChartArea("fluxArea")

        '//将定义的画图区域添加到chart
        Chart1.ChartAreas.Add(fluxArea)

        '//设置显示区域
        With fluxArea
            '//设置背景颜色为黑色
            .BackColor = Color.Black
            '//设置X轴最小值
            .AxisX.Minimum = 1
            '//设置X轴最大值
            .AxisX.Maximum = 20
            '//设置X轴
            .AxisX.Interval = 1
            '//设置X轴标题
            .AxisX.Title = "时间"
            '//设置X轴标题字体
            .AxisX.TitleFont = New System.Drawing.Font("微软雅黑", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            '//设置X轴线宽
            .AxisX.LineWidth = 3
            '//设置X轴颜色
            .AxisX.LineColor = Color.Green
            '//设置X轴线型
            .AxisX.LineDashStyle = ChartDashStyle.Solid
            '//设置X轴标题对齐
            .AxisX.TitleAlignment = StringAlignment.Center
            '//设置X轴标题颜色
            .AxisX.TitleForeColor = Color.Yellow
            '//设置X轴网格刻度线
            .AxisX.MajorGrid.LineColor = Color.Green
            '//设置X轴网格刻度线宽度
            .AxisX.MajorGrid.LineWidth = 1
            '//设置X轴网格刻度线样式
            .AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash
            '//设置X轴刻度线颜色
            .AxisX.MajorTickMark.LineColor = Color.Green
            '//设置X轴刻度线长度
            .AxisX.MajorTickMark.Size = 2
            '//设置X轴刻度线宽度
            .AxisX.MajorTickMark.LineWidth = 2
            '//设置X轴刻度线样式
            .AxisX.MajorTickMark.LineDashStyle = ChartDashStyle.Solid
            '//设置X轴刻度线位置
            .AxisX.MajorTickMark.TickMarkStyle = TickMarkStyle.OutsideArea
            '//设置X轴标签颜色
            .AxisX.LabelStyle.ForeColor = Color.LawnGreen

            '//设置Y轴最小值
            .AxisY.Minimum = 0D
            '//设置Y轴最大值
            .AxisY.Maximum = 150D
            '//设置Y轴标题
            .AxisY.Title = "皮带秤瞬时流量"
            '//设置Y轴标题字体
            .AxisY.TitleFont = New System.Drawing.Font("微软雅黑", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            '//设置X轴线宽
            .AxisY.LineWidth = 3
            '//设置X轴颜色
            .AxisY.LineColor = Color.Green
            '//设置X轴线型
            .AxisY.LineDashStyle = ChartDashStyle.Solid
            '//设置Y轴标题对齐
            .AxisY.TitleAlignment = StringAlignment.Center
            '//设置Y轴标题颜色
            .AxisY.TitleForeColor = Color.Yellow
            '//设置Y轴网格刻度线
            .AxisY.MajorGrid.LineColor = Color.Green
            '//设置Y轴网格刻度线宽度
            .AxisY.MajorGrid.LineWidth = 1
            '//设置Y轴网格刻度线样式
            .AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash
            '//设置X轴刻度线颜色
            .AxisY.MajorTickMark.LineColor = Color.Green
            '//设置X轴刻度线长度
            .AxisY.MajorTickMark.Size = 2
            '//设置X轴刻度线宽度
            .AxisY.MajorTickMark.LineWidth = 2
            '//设置X轴刻度线样式
            .AxisY.MajorTickMark.LineDashStyle = ChartDashStyle.Solid
            '//设置X轴刻度线位置
            .AxisY.MajorTickMark.TickMarkStyle = TickMarkStyle.OutsideArea
            '//设置Y轴标签颜色
            .AxisY.LabelStyle.ForeColor = Color.LawnGreen
        End With

        '//创建Legends对象实例
        fluxLegend = New Legend("fluxLegend")

        '//设置Legends对象
        With fluxLegend
            '//设置图例停靠位置为自动
            .Position.Auto = True
            '//设置图例对齐方式为居中
            .Alignment = StringAlignment.Center
            '//设置图例停靠位置为底部
            .Docking = Docking.Bottom
            '//设置图例的背景色为透明
            .BackColor = Color.Transparent
            '//设置图例的字体颜色为白色
            .ForeColor = Color.White
            '//设置图例字体
            .Font = New System.Drawing.Font("微软雅黑", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        End With

        '//清除默认Legends
        Chart1.Legends.Clear()

        Chart1.Legends.Add(fluxLegend)

        '//创建series对象实例
        fluxLine1 = New Series("fluxLine1")
        fluxLine2 = New Series("fluxLine2")

        '//设置Serial对象1
        With fluxLine1
            '//设置线型
            .ChartType = SeriesChartType.Spline
            '//设置画线区域
            .ChartArea = "fluxArea"
            '//设置画线的宽度
            .BorderWidth = 3
            '//设置画线颜色
            .Color = Color.LawnGreen
            '//设置画线阴影
            .ShadowOffset = 1
            '//设置图示文字
            .LegendText = "4A路皮带秤"
            '//设置图例属性
            .Legend = "fluxLegend"
        End With

        '//设置Serial对象2
        With fluxLine2
            '//设置线型
            .ChartType = SeriesChartType.Spline
            '//设置画线区域
            .ChartArea = "fluxArea"
            '//设置画线的宽度
            .BorderWidth = 3
            '//设置画线颜色
            .Color = Color.Red
            '//设置画线阴影
            .ShadowOffset = 1
            '//设置图示文字
            .LegendText = "4B路皮带秤"
            '//设置图例属性
            .Legend = "fluxLegend"
        End With

        '//清除默认series
        Chart1.Series.Clear()

        '//将定义好的曲线对象添加到chart
        Chart1.Series.Add(fluxLine1)
        Chart1.Series.Add(fluxLine2)

       

    End Sub

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

        '//
        InitChartSet()


    End Sub

    Private Sub InitChartData()

        '// 设置初始数据
        Dim i As Integer = 0

        For i = 0 To 19
            X1(i) = Format(Now(), "HH:mm:ss")
            X2(i) = Format(Now(), "HH:mm:ss")
            Y1(i) = 0
            Y2(i) = 0
        Next

        '//显示数据
        Chart1.Series("fluxLine1").Points.DataBindXY(X1, Y1)

        Chart1.Series("fluxLine2").Points.DataBindXY(X2, Y2)

    End Sub

    Private Sub RefreshChartData()

        '// 更新曲线数据
        Dim i As Integer

        For i = 0 To 18
            X1(i) = X1(i + 1)
            X2(i) = X2(i + 1)

            Y1(i) = Y1(i + 1)
            Y2(i) = Y2(i + 1)
        Next

        Y1(19) = Math.Round(100 * Rnd(100) * 1.0264, 2)
        X1(19) = Format(Now(), "HH:mm:ss")

        Y2(19) = Math.Round(100 * Rnd(100) * 1.0264, 2)
        X2(19) = Format(Now(), "HH:mm:ss")

        Chart1.Series("fluxLine1").Points.DataBindXY(X1, Y1)

        Chart1.Series("fluxLine2").Points.DataBindXY(X2, Y2)

    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        InitChartData()

        Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

        RefreshChartData()

    End Sub
End Class
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhangsl168/article/details/95442810


相关教程