(1)在窗体上布置一个MSChart控件
创新互联是一家专业提供乐东黎族企业网站建设,专注与成都网站设计、做网站、H5网站设计、小程序制作等业务。10年已为乐东黎族众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
(2)窗体代码
Option Explicit
Private Sub Form_Load()
'设置MSChart1
With MSChart1
'图上只画一条曲线
.ColumnCount = 1
'设置图表类型为二维曲线;默认为直方图
.chartType = VtChChartType2dLine
'X轴显示10个单位
.RowCount = 10
End With
'曲线数据
Dim v(1 To 10) As Single
Dim i As Integer
'曲线数据随机产生
For i = 1 To 10
v(i) = Rnd * 99 + 1
Next
' 显示曲线
With MSChart1
For i = 1 To 10
' 指定X轴的坐标
.Row = i
' X轴标签
.RowLabel = i
' 与x对应的值
.data = v(i)
Next
End With
End Sub(3)运行结果
(4)其他注意事项
在设计状态,鼠标右键单击MSChart控件,在弹出菜单中单击“属性”,可以调出控件的属性页窗口。利用这个窗口,可以直观地设计MSChart
首先你要建一个 Bitmap
再将picture控件的image属性与之相关联
再到 picture的image上画线
保存image到文件
/////////////////////////////////
Dim mybitmap As Bitmap
mybitmap = New Bitmap(300, 150)
PictureBox0.Image = mybitmap
Dim Graph As Graphics
Graph =Graphics.FromImage(PictureBox0.image)
Graph .drawline(Pens.Blue,0,0,111,111)
picturebox0.image.save("c:\aaa.jpg")
/////////////////////////////////////
存成的文件绝对有你画的线..我一直在用这种方法
在VB中绘制实时曲线是比较难的,一般要应用第三方控件或是Windows
API函数来完成,但是如果你对实时曲线的要求不是很高,只要能表示出当前的一般情况的话,我们可以直接应用VB提供给我们的空间来完成.
原则上讲,直接在Form里绘制曲线都是可以的,MSDN上面很多例程就是直接在Form里面绘制图形的,Form作为绘制图形的容器,不过一般应用中Form中不可避免的会有很多其他控件,所以我们选择PictureBox作为绘制曲线的容器.
实时曲线的绘制一般借助于Timer控件来完成,使用Timer控件,定期将串口或是其他仪器中监测到的数据送往PictureBox1,而曲线的绘制一般画成折线图,采用PictureBox1的Line方法绘制.具体实现如下:
1.选择需要显示的窗体Picture1,加入图片框Picture1,根据实际需要设置图片的大小并移到合适的位置,并在图片的外面画好量程----时间坐标系;然后加上Timer控件以及两个CommandButton,界面就基本设置好了.
2.建立坐标系,根据Picture1的大小和高度设置画出坐标系的X轴和Y轴:
Picture1
.ScaleMode
=
1
‘以VB的基本单位作为建立坐标轴以及绘制图形的单位;
Picture1.Refresh
Picture1.CurrentX
=
Picture1.ScaleLeft
+100
Picture1.CurrentY
=
Picture1.ScaleTop
Picture1.Print
Picture1.ScaleHeight
-
100
Picture1.Line(Picture1.ScaleLeft+100,Picture1.ScaleTop+100)-(Picture1.ScaleLeft+100,
Picture1.ScaleHeight
-
100)
Picture1.CurrentX
=
Picture1.ScaleLeft
+100
Picture1.CurrentY
=
Picture1.ScaleHeight
Picture1.Print
“(0,0)”
Picture1.Line
(Picture1.ScaleLeft
+
100,
Picture1.ScaleHeight
-
100)-(Picture1.ScaleWidth
-
100,
Picture1.ScaleHeight
-
100)
Picture1.CurrentX
=
Picture1.ScaleWidth
Picture1.CurrentY
=
Picture1.ScaleHeight
Picture1.Print
Picture1.ScaleWidth-100
Picture1.AutoRedraw
=
True
‘必要时,用存储在内存中的图象进行重绘
3.绘制曲线并保存,我们这里以正弦曲线作为绘制曲线的数据来源,具体应用是可以采用由串口或其他仪器采集得到的数据.首先我们绘制一条中线,然后在Timer控件的Time事件中绘制曲线:
Picture1.Line
(Picture1.ScaleLeft,
CInt(Picture1.ScaleHeight
/
2))-(Picture1.ScaleWidth,
CInt(Picture1.ScaleHeight
/
2))
‘绘制中线
Private
Sub
Timer1_Timer()
Dim
y1
As
Integer
y1
=
CInt(Sin((x
-
Picture1.Left)
/
20
/
180
*
pi)
*
Picture1.ScaleHeight
/
2)
y1
=
CInt((Picture1.ScaleHeight
+
1000)
/
2)
-
y1
Picture1.Line
(x,
y)-(x
+
20,
y1)
x
=
x
+
20
y
=
y1
If
x
=
Picture1.ScaleWidth
Then
SavePicture
Picture1.Image,
"c:\sin.bmp"
‘保存图画,
拖一个PictureBox1控件 创建一个Paint事件。在事件中加入 Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint ' Create pens. Dim redPen As New Pen(Color.Red, 3) Dim greenPen As New Pen(Color.Green, 3) ' Create points that define curve. Dim point1 As New Point(50, 50) Dim point2 As New Point(100, 25) Dim point3 As New Point(200, 5) Dim point4 As New Point(250, 50) Dim point5 As New Point(300, 100) Dim point6 As New Point(350, 200) Dim point7 As New Point(250, 250) Dim curvePoints As Point() = {point1, point2, point3, point4, _ point5, point6, point7} ' Draw lines between original points to screen. e.Graphics.DrawLines(redPen, curvePoints) ' Draw curve to screen. e.Graphics.DrawCurve(greenPen, curvePoints) End Sub 得到数据后,改point的数据。然后PictureBox1.Refresh()就行了