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

1. import namespace

    

Imports System.Windows.Forms.DataVisualization.Charting

 

    找个namespace包含用于图表 Windows 窗体控件方法和属性(如chart控件)

 

2. 设置chart控件

    

    a. 在ChartArea属性里,添加ChartArea1, ChartArea2.

    b. 设置align相关属性。

       

       

 

3. 使用代码配置ChartArea

    a. 已添加ChartArea。

        这种情况直接设置

       


  1.  
    Chart1.ChartAreas(0).AxisX.Title = "Length (nt)" '设置ChartArea里坐标轴标题
  2.  
    Chart1.ChartAreas(0).AxisY.Title = "Read count"
  3.  
     
  4.  
     
  5.  
    Chart1.ChartAreas(1).AxisX.Title = "Length (nt)" '设置ChartArea里坐标轴标题
  6.  
    Chart1.ChartAreas(1).AxisY.Title = "Read count"
  7.  
     
  8.  
     
  9.  
    minValue = DateTime.Now
  10.  
    '30预览区域
  11.  
    maxValue = minValue.AddSeconds(30)
  12.  
     
  13.  
     
  14.  
    Chart1.ChartAreas(0).AxisX.Minimum = minValue.ToOADate()
  15.  
    Chart1.ChartAreas(0).AxisX.Maximum = maxValue.ToOADate()
  16.  
     
  17.  
     
  18.  
    Chart1.ChartAreas(1).AxisX.Minimum = minValue.ToOADate()
  19.  
    Chart1.ChartAreas(1).AxisX.Maximum = maxValue.ToOADate()
  20.  
     
  21.  
     
  22.  
    Chart1.Series.Clear()
  23.  
     
  24.  
     
  25.  
    Dim series1 As New Series("Read Count")
  26.  
    series1.ChartType = SeriesChartType.Line '设置Series的绘图类型
  27.  
    series1.BorderWidth = 1
  28.  
    series1.Color = Color.Red
  29.  
    series1.XValueType = ChartValueType.Time
  30.  
     
  31.  
    series1.ChartArea = "ChartArea1"
  32.  
     
  33.  
    Dim series2 As New Series("Read Count2")
  34.  
    series2.ChartType = SeriesChartType.Line '设置Series的绘图类型
  35.  
    series2.BorderWidth = 1
  36.  
    series2.Color = Color.Blue
  37.  
    series2.XValueType = ChartValueType.Time
  38.  
     
  39.  
    series2.ChartArea = "ChartArea2"
  40.  
     
  41.  
    Chart1.Series.Add(series1)
  42.  
    Chart1.Series.Add(series2)

 

 

    b. 未添加ChartArea

        在上一段代码之前添加如下代码


  1.  
    Chart1.ChartAreas.Clear() '清空ChartArea
  2.  
    Dim ChartAreas1 As New ChartArea("ChartArea1") '定义新的ChartArea
  3.  
    Dim ChartAreas2 As New ChartArea("ChartArea2") '定义新的ChartArea
  4.  
    Chart1.ChartAreas.Add(ChartAreas1) '将新定义的ChartArea加入Chart1
  5.  
    Chart1.ChartAreas.Add(ChartAreas2) '将新定义的ChartArea加入Chart1

 

4. 添加points

 


  1.  
    For Each ptA In Chart1.ChartAreas
  2.  
    Dim ptSeries As Series
  3.  
    '对每series进行数据扫描
  4.  
    For Each ptSeries In Chart1.Series
  5.  
    ptSeries.Points.AddXY(x y)
  6.  
    Next ptSeries
  7.  
    Next

 

 

5. 动态修改X轴坐标(例如:随时间变化)

 


  1.  
    Dim timeStamp As DateTime = DateTime.Now
  2.  
    Dim removeBefore As Double = timeStamp.AddSeconds((CDbl(15) * -1)).ToOADate()
  3.  
    'remove oldest values to maintain a constant number of data points
  4.  
    While ptSeries.Points(0).XValue < removeBefore
  5.  
    ptSeries.Points.RemoveAt(0)
  6.  
    End While
  7.  
     
  8.  
     
  9.  
    Dim ptA As ChartArea
  10.  
    For Each ptA In Chart1.ChartAreas
  11.  
    ptA.AxisX.Minimum = ptSeries.Points(0).XValue
  12.  
    ptA.AxisX.Maximum = DateTime.FromOADate(ptSeries.Points(0).XValue).AddSeconds(30).ToOADate()
  13.  
    Next

 

 

6. 清空数据

 

    Chart1.Series(0).Points.Clear()

 

 

效果图

 

 

 


相关教程