VB.net chart 控制元件使用

chinaherolts2008發表於2020-12-11
  1. import namespace

Imports System.Windows.Forms.DataVisualization.Charting

找個namespace包含用於圖表 Windows 窗體[vb.net教程](https://www.xin3721.com/eschool/VBNetxin3721/)控制元件方法和屬性(如chart控制元件)
  1. 設定chart控制元件
    在這裡插入圖片描述

    a. 在ChartArea屬性裡,新增ChartArea1, ChartArea2.

    b. 設定align相關屬性。

    ![在這裡插入圖片描述](https://img-blog.csdn.net/20170425215651790?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpaGFvNTU1NTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center#pic_center)
    
  2. 使用程式碼配置ChartArea

    a. 已新增ChartArea。

     這種情況直接設定
    
    
    
     Chart1.ChartAreas(0).AxisX.Title = "Length (nt)"   '設定ChartArea裡座標軸標題
     Chart1.ChartAreas(0).AxisY.Title = "Read count"
    
    
     Chart1.ChartAreas(1).AxisX.Title = "Length (nt)"   '設定ChartArea裡座標軸標題
     Chart1.ChartAreas(1).AxisY.Title = "Read count"
    
    
     minValue = DateTime.Now
     '30預覽區域 
     maxValue = minValue.AddSeconds(30)
    
    
     Chart1.ChartAreas(0).AxisX.Minimum = minValue.ToOADate()
     Chart1.ChartAreas(0).AxisX.Maximum = maxValue.ToOADate()
    
    
     Chart1.ChartAreas(1).AxisX.Minimum = minValue.ToOADate()
     Chart1.ChartAreas(1).AxisX.Maximum = maxValue.ToOADate()
    
    
     Chart1.Series.Clear()
    
    
     Dim series1 As New Series("Read Count")
     series1.ChartType = SeriesChartType.Line         '設定Series的繪圖型別
     series1.BorderWidth = 1
     series1.Color = Color.Red
     series1.XValueType = ChartValueType.Time
    
     series1.ChartArea = "ChartArea1"
    
     Dim series2 As New Series("Read Count2")
     series2.ChartType = SeriesChartType.Line         '設定Series的繪圖型別
     series2.BorderWidth = 1
     series2.Color = Color.Blue
     series2.XValueType = ChartValueType.Time
    
     series2.ChartArea = "ChartArea2"
    
     Chart1.Series.Add(series1)
     Chart1.Series.Add(series2)
    

    b. 未新增ChartArea

     在上一段程式碼之前新增如下程式碼
    
     Chart1.ChartAreas.Clear() '清空ChartArea
     Dim ChartAreas1 As New ChartArea("ChartArea1")    '定義新的ChartArea
     Dim ChartAreas2 As New ChartArea("ChartArea2")    '定義新的ChartArea
     Chart1.ChartAreas.Add(ChartAreas1)                 '將新定義的ChartArea加入Chart1
     Chart1.ChartAreas.Add(ChartAreas2)                 '將新定義的ChartArea加入Chart1
    
  3. 新增points

    For Each ptA In Chart1.ChartAreas
         Dim ptSeries As Series
         '對每series進行資料掃描 
         For Each ptSeries In Chart1.Series
             ptSeries.Points.AddXY(x y)
         Next ptSeries
     Next
    
  4. 動態修改X軸座標(例如:隨時間變化)

     Dim timeStamp As DateTime = DateTime.Now
     Dim removeBefore As Double = timeStamp.AddSeconds((CDbl(15) * -1)).ToOADate()
     'remove oldest values to maintain a constant number of data points 
     While ptSeries.Points(0).XValue < removeBefore
         ptSeries.Points.RemoveAt(0)
     End While
    
    
     Dim ptA As ChartArea
     For Each ptA In Chart1.ChartAreas
         ptA.AxisX.Minimum = ptSeries.Points(0).XValue
         ptA.AxisX.Maximum = DateTime.FromOADate(ptSeries.Points(0).XValue).AddSeconds(30).ToOADate()
     Next
    
  5. 清空資料

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

效果圖

在這裡插入圖片描述

相關文章