- import namespace
Imports System.Windows.Forms.DataVisualization.Charting
找个namespace包含用于图表 Windows 窗体[vb.net教程](https://www.xin3721.com/eschool/VBNetxin3721/)控件方法和属性(如chart控件)
-
设置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)
-
使用代码配置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 = 1series1.Color = Color.Redseries1.XValueType = ChartValueType.Timeseries1.ChartArea = "ChartArea1"Dim series2 As New Series("Read Count2")series2.ChartType = SeriesChartType.Line '设置Series的绘图类型series2.BorderWidth = 1series2.Color = Color.Blueseries2.XValueType = ChartValueType.Timeseries2.ChartArea = "ChartArea2"Chart1.Series.Add(series1)Chart1.Series.Add(series2)
b. 未添加ChartArea
在上一段代码之前添加如下代码Chart1.ChartAreas.Clear() '清空ChartAreaDim ChartAreas1 As New ChartArea("ChartArea1") '定义新的ChartAreaDim ChartAreas2 As New ChartArea("ChartArea2") '定义新的ChartAreaChart1.ChartAreas.Add(ChartAreas1) '将新定义的ChartArea加入Chart1Chart1.ChartAreas.Add(ChartAreas2) '将新定义的ChartArea加入Chart1
-
添加points
For Each ptA In Chart1.ChartAreasDim ptSeries As Series'对每series进行数据扫描 For Each ptSeries In Chart1.SeriesptSeries.Points.AddXY(x y)Next ptSeriesNext
-
动态修改X轴坐标(例如:随时间变化)
Dim timeStamp As DateTime = DateTime.NowDim 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 < removeBeforeptSeries.Points.RemoveAt(0)End WhileDim ptA As ChartAreaFor Each ptA In Chart1.ChartAreasptA.AxisX.Minimum = ptSeries.Points(0).XValueptA.AxisX.Maximum = DateTime.FromOADate(ptSeries.Points(0).XValue).AddSeconds(30).ToOADate()Next
-
清空数据
Chart1.Series(0).Points.Clear()
效果图