中如何自动绘制曲线?数据点很多!
纵轴为采集上来的数据(这里已经解决,也是在Timer1里完成的)变量txtReadVal0.Text,横轴采用Timer1的时间间隔,
3 个解决方案
#1
这个用自定义控件好实现一点,
建立一个arrlist,存储曲线上各点
重写自定义控件的onpaint,连接各点
在Timer1.Tick 把产生的点添加到arrlist中,同时刷新自定义控件
建立一个arrlist,存储曲线上各点
重写自定义控件的onpaint,连接各点
在Timer1.Tick 把产生的点添加到arrlist中,同时刷新自定义控件
#2
怎么定义控件,能给个例子吗?
我这有一个:但是没看懂:
(Main.vb:)
Imports Microsoft.Win32
Public Class Main
Inherits System.Windows.Forms.Form
Dim radDrawLine1, radDrawLine2 As DrawLine
Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
radDrawLine1 = New DrawLine(Panel1, Color.Blue)
radDrawLine2 = New DrawLine(Panel1, Color.Brown)
End Sub
Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntStart.Click
Me.Timer1.Enabled = Not Me.Timer1.Enabled
If Me.Timer1.Enabled Then
Me.bntStart.Text = "Stop"
Else
Me.bntStart.Text = "Start"
Me.Label1.Text = ""
Me.Label2.Text = ""
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Y1, Y2 As Single
Randomize()
Y1 = (100 * Rnd() + 1)
Me.Label1.Text = Str(Y1)
Randomize()
Y2 = (200 * Rnd() + 1)
Me.Label2.Text = Str(Y2)
Me.radDrawLine1.Ready(Y1)
Me.radDrawLine2.Ready(Y2)
Me.radDrawLine1.start()
Me.radDrawLine2.start()
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
End Class
(MyClass.vb:如下:)
Public Class DrawLine 'by xisat
REM 实例初始化时须提供panel及pencolor,绘一条line直接调用方法.ready().start()
REM 两条line时,可在同一个实例两个ready后start,绘出互补线
REM 两条以上line,使用多个实例
Dim m_x, m_sweepy, m_m As Single
Dim penx As System.Drawing.Pen '刷子
Dim penfore As System.Drawing.Pen
Dim penback As System.Drawing.Pen
Dim Ografix As Graphics '图形化定义
Dim sngheight As Single 'panel高度,上端线条最大值
'类属性变量
Dim Sweep_value As Single
Dim Panelx As System.Windows.Forms.Panel
Dim Pen_color As System.Drawing.Color
Public WriteOnly Property Panx() As System.Windows.Forms.Panel
Set(ByVal Value As System.Windows.Forms.Panel)
Panelx = Value
End Set
End Property
Public Property Svalue() As Single
Get
Return Sweep_value
End Get
Set(ByVal Value As Single)
Sweep_value = Value
End Set
End Property
Public Property pcolor() As System.Drawing.Color
Get
Return Pen_color
End Get
Set(ByVal Value As System.Drawing.Color)
Pen_color = Value
End Set
End Property
Public Sub New(ByVal px As System.Windows.Forms.Panel, ByVal pc As System.Drawing.Color)
Me.Panx = px '传递panel
Me.pcolor = pc
penx = New System.Drawing.Pen(Pen_color, 3) '初始化刷子
Ografix = Panelx.CreateGraphics() '图形化初始
Ografix.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
penfore = New System.Drawing.Pen(Color.White)
penback = New System.Drawing.Pen(Panelx.BackColor)
End Sub
Public Sub Ready(ByVal S_value As Single)
Dim sngy As Single
sngheight = Panelx.Height
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 29, m_sweepy, m_x, sngy)
m_sweepy = sngy '保留上次的y值
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub start()
m_x += 30
If m_x > Panelx.Width Then
m_x = 0
Ografix.Clear(Panelx.BackColor)
End If
End Sub
End Class
我这有一个:但是没看懂:
(Main.vb:)
Imports Microsoft.Win32
Public Class Main
Inherits System.Windows.Forms.Form
Dim radDrawLine1, radDrawLine2 As DrawLine
Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
radDrawLine1 = New DrawLine(Panel1, Color.Blue)
radDrawLine2 = New DrawLine(Panel1, Color.Brown)
End Sub
Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntStart.Click
Me.Timer1.Enabled = Not Me.Timer1.Enabled
If Me.Timer1.Enabled Then
Me.bntStart.Text = "Stop"
Else
Me.bntStart.Text = "Start"
Me.Label1.Text = ""
Me.Label2.Text = ""
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Y1, Y2 As Single
Randomize()
Y1 = (100 * Rnd() + 1)
Me.Label1.Text = Str(Y1)
Randomize()
Y2 = (200 * Rnd() + 1)
Me.Label2.Text = Str(Y2)
Me.radDrawLine1.Ready(Y1)
Me.radDrawLine2.Ready(Y2)
Me.radDrawLine1.start()
Me.radDrawLine2.start()
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
End Class
(MyClass.vb:如下:)
Public Class DrawLine 'by xisat
REM 实例初始化时须提供panel及pencolor,绘一条line直接调用方法.ready().start()
REM 两条line时,可在同一个实例两个ready后start,绘出互补线
REM 两条以上line,使用多个实例
Dim m_x, m_sweepy, m_m As Single
Dim penx As System.Drawing.Pen '刷子
Dim penfore As System.Drawing.Pen
Dim penback As System.Drawing.Pen
Dim Ografix As Graphics '图形化定义
Dim sngheight As Single 'panel高度,上端线条最大值
'类属性变量
Dim Sweep_value As Single
Dim Panelx As System.Windows.Forms.Panel
Dim Pen_color As System.Drawing.Color
Public WriteOnly Property Panx() As System.Windows.Forms.Panel
Set(ByVal Value As System.Windows.Forms.Panel)
Panelx = Value
End Set
End Property
Public Property Svalue() As Single
Get
Return Sweep_value
End Get
Set(ByVal Value As Single)
Sweep_value = Value
End Set
End Property
Public Property pcolor() As System.Drawing.Color
Get
Return Pen_color
End Get
Set(ByVal Value As System.Drawing.Color)
Pen_color = Value
End Set
End Property
Public Sub New(ByVal px As System.Windows.Forms.Panel, ByVal pc As System.Drawing.Color)
Me.Panx = px '传递panel
Me.pcolor = pc
penx = New System.Drawing.Pen(Pen_color, 3) '初始化刷子
Ografix = Panelx.CreateGraphics() '图形化初始
Ografix.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
penfore = New System.Drawing.Pen(Color.White)
penback = New System.Drawing.Pen(Panelx.BackColor)
End Sub
Public Sub Ready(ByVal S_value As Single)
Dim sngy As Single
sngheight = Panelx.Height
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 29, m_sweepy, m_x, sngy)
m_sweepy = sngy '保留上次的y值
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub start()
m_x += 30
If m_x > Panelx.Width Then
m_x = 0
Ografix.Clear(Panelx.BackColor)
End If
End Sub
End Class
#3
原来的程序是在Timer1下自动采集数据并保存到数据库中,我只想在原来的程序中Timer1_Tick下加上自动绘图的功能,不可能在添加一个Timer。
MyClass.vb需要怎么修改呢?
MyClass.vb需要怎么修改呢?
#1
这个用自定义控件好实现一点,
建立一个arrlist,存储曲线上各点
重写自定义控件的onpaint,连接各点
在Timer1.Tick 把产生的点添加到arrlist中,同时刷新自定义控件
建立一个arrlist,存储曲线上各点
重写自定义控件的onpaint,连接各点
在Timer1.Tick 把产生的点添加到arrlist中,同时刷新自定义控件
#2
怎么定义控件,能给个例子吗?
我这有一个:但是没看懂:
(Main.vb:)
Imports Microsoft.Win32
Public Class Main
Inherits System.Windows.Forms.Form
Dim radDrawLine1, radDrawLine2 As DrawLine
Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
radDrawLine1 = New DrawLine(Panel1, Color.Blue)
radDrawLine2 = New DrawLine(Panel1, Color.Brown)
End Sub
Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntStart.Click
Me.Timer1.Enabled = Not Me.Timer1.Enabled
If Me.Timer1.Enabled Then
Me.bntStart.Text = "Stop"
Else
Me.bntStart.Text = "Start"
Me.Label1.Text = ""
Me.Label2.Text = ""
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Y1, Y2 As Single
Randomize()
Y1 = (100 * Rnd() + 1)
Me.Label1.Text = Str(Y1)
Randomize()
Y2 = (200 * Rnd() + 1)
Me.Label2.Text = Str(Y2)
Me.radDrawLine1.Ready(Y1)
Me.radDrawLine2.Ready(Y2)
Me.radDrawLine1.start()
Me.radDrawLine2.start()
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
End Class
(MyClass.vb:如下:)
Public Class DrawLine 'by xisat
REM 实例初始化时须提供panel及pencolor,绘一条line直接调用方法.ready().start()
REM 两条line时,可在同一个实例两个ready后start,绘出互补线
REM 两条以上line,使用多个实例
Dim m_x, m_sweepy, m_m As Single
Dim penx As System.Drawing.Pen '刷子
Dim penfore As System.Drawing.Pen
Dim penback As System.Drawing.Pen
Dim Ografix As Graphics '图形化定义
Dim sngheight As Single 'panel高度,上端线条最大值
'类属性变量
Dim Sweep_value As Single
Dim Panelx As System.Windows.Forms.Panel
Dim Pen_color As System.Drawing.Color
Public WriteOnly Property Panx() As System.Windows.Forms.Panel
Set(ByVal Value As System.Windows.Forms.Panel)
Panelx = Value
End Set
End Property
Public Property Svalue() As Single
Get
Return Sweep_value
End Get
Set(ByVal Value As Single)
Sweep_value = Value
End Set
End Property
Public Property pcolor() As System.Drawing.Color
Get
Return Pen_color
End Get
Set(ByVal Value As System.Drawing.Color)
Pen_color = Value
End Set
End Property
Public Sub New(ByVal px As System.Windows.Forms.Panel, ByVal pc As System.Drawing.Color)
Me.Panx = px '传递panel
Me.pcolor = pc
penx = New System.Drawing.Pen(Pen_color, 3) '初始化刷子
Ografix = Panelx.CreateGraphics() '图形化初始
Ografix.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
penfore = New System.Drawing.Pen(Color.White)
penback = New System.Drawing.Pen(Panelx.BackColor)
End Sub
Public Sub Ready(ByVal S_value As Single)
Dim sngy As Single
sngheight = Panelx.Height
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 29, m_sweepy, m_x, sngy)
m_sweepy = sngy '保留上次的y值
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub start()
m_x += 30
If m_x > Panelx.Width Then
m_x = 0
Ografix.Clear(Panelx.BackColor)
End If
End Sub
End Class
我这有一个:但是没看懂:
(Main.vb:)
Imports Microsoft.Win32
Public Class Main
Inherits System.Windows.Forms.Form
Dim radDrawLine1, radDrawLine2 As DrawLine
Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
radDrawLine1 = New DrawLine(Panel1, Color.Blue)
radDrawLine2 = New DrawLine(Panel1, Color.Brown)
End Sub
Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntStart.Click
Me.Timer1.Enabled = Not Me.Timer1.Enabled
If Me.Timer1.Enabled Then
Me.bntStart.Text = "Stop"
Else
Me.bntStart.Text = "Start"
Me.Label1.Text = ""
Me.Label2.Text = ""
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Y1, Y2 As Single
Randomize()
Y1 = (100 * Rnd() + 1)
Me.Label1.Text = Str(Y1)
Randomize()
Y2 = (200 * Rnd() + 1)
Me.Label2.Text = Str(Y2)
Me.radDrawLine1.Ready(Y1)
Me.radDrawLine2.Ready(Y2)
Me.radDrawLine1.start()
Me.radDrawLine2.start()
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
End Sub
End Class
(MyClass.vb:如下:)
Public Class DrawLine 'by xisat
REM 实例初始化时须提供panel及pencolor,绘一条line直接调用方法.ready().start()
REM 两条line时,可在同一个实例两个ready后start,绘出互补线
REM 两条以上line,使用多个实例
Dim m_x, m_sweepy, m_m As Single
Dim penx As System.Drawing.Pen '刷子
Dim penfore As System.Drawing.Pen
Dim penback As System.Drawing.Pen
Dim Ografix As Graphics '图形化定义
Dim sngheight As Single 'panel高度,上端线条最大值
'类属性变量
Dim Sweep_value As Single
Dim Panelx As System.Windows.Forms.Panel
Dim Pen_color As System.Drawing.Color
Public WriteOnly Property Panx() As System.Windows.Forms.Panel
Set(ByVal Value As System.Windows.Forms.Panel)
Panelx = Value
End Set
End Property
Public Property Svalue() As Single
Get
Return Sweep_value
End Get
Set(ByVal Value As Single)
Sweep_value = Value
End Set
End Property
Public Property pcolor() As System.Drawing.Color
Get
Return Pen_color
End Get
Set(ByVal Value As System.Drawing.Color)
Pen_color = Value
End Set
End Property
Public Sub New(ByVal px As System.Windows.Forms.Panel, ByVal pc As System.Drawing.Color)
Me.Panx = px '传递panel
Me.pcolor = pc
penx = New System.Drawing.Pen(Pen_color, 3) '初始化刷子
Ografix = Panelx.CreateGraphics() '图形化初始
Ografix.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
penfore = New System.Drawing.Pen(Color.White)
penback = New System.Drawing.Pen(Panelx.BackColor)
End Sub
Public Sub Ready(ByVal S_value As Single)
Dim sngy As Single
sngheight = Panelx.Height
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 29, m_sweepy, m_x, sngy)
m_sweepy = sngy '保留上次的y值
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal P_color As System.Drawing.Color, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
penx.Color = P_color
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub Ready(ByVal S_value As Single, ByVal linetype As Boolean)
Dim sngy As Single
sngheight = Panelx.Height
Ografix.DrawLine(penback, m_x, 0, m_x, sngheight)
sngy = sngheight - S_value
Ografix.DrawLine(penx, m_x - 1, m_sweepy, m_x, sngy)
m_sweepy = sngy
Ografix.DrawLine(penfore, m_x + 1, 0, m_x + 1, sngheight)
End Sub
Public Sub start()
m_x += 30
If m_x > Panelx.Width Then
m_x = 0
Ografix.Clear(Panelx.BackColor)
End If
End Sub
End Class
#3
原来的程序是在Timer1下自动采集数据并保存到数据库中,我只想在原来的程序中Timer1_Tick下加上自动绘图的功能,不可能在添加一个Timer。
MyClass.vb需要怎么修改呢?
MyClass.vb需要怎么修改呢?