别用控件了,用我的这段代码吧
直接粘贴到窗体上运行即可,不用任何控件就可以绘图
可以绘制任何直角坐标系的曲线图,不需要控件,也可以调整坐标的位置
Option Explicit
Private Const X_Axe = 0.5 'X轴原点位于窗口的水平位置比例
Private Const Y_Axe = 0.6 'Y轴原点位于窗口的垂直位置比例
Private Const ScaleRate = 30 '多少像素代表一个单位长度
Private Const DrawColor = vbRed '曲线颜色
Private ErrExp As Boolean '当表达式发生错误时,会置True(比如函数在此点无有效值)
Private Sub Form_Load()
Me.Show
Me.Cls
Call DrawAxe
Call DrawCoordinate
End Sub
Private Function Expression(ByVal X As Double) As Double '公式函数可以是任意内容
ErrExp = False '进入时,必须重置此标志为False
Expression = Sin(X)
' If X > 0 Then '这里代码任意,也可以是Expression = 2 * X ^ 2 + 2 * X + 1之类的
' Expression = Log(X)
' Else
' Expression = 0
' ErrExp = True
' End If
End Function
Private Sub DrawCoordinate() '绘制曲线过程
Dim i As Long
Dim Last(1 To 2) As Long, This(1 To 2) As Long
Dim X_offset As Long
Dim Y_offset As Long
Me.ScaleMode = vbPixels
X_offset = Me.ScaleWidth * X_Axe
Y_offset = Me.ScaleHeight * Y_Axe
'设置一个初始值
Last(1) = 0
Last(2) = -Expression((0 - X_offset) / ScaleRate) * ScaleRate
i = 0
While i <= Me.ScaleWidth
'取下一点的值
This(1) = i
This(2) = -Expression((i - X_offset) / ScaleRate) * ScaleRate
'判断表达式是否出错
If ErrExp = True Then
'出错的情况下,循环直到没有错误或者超出范围为止
While ErrExp = True And i <= Me.ScaleWidth
i = i + 1
This(1) = i
This(2) = -Expression((i - X_offset) / ScaleRate) * ScaleRate
Wend
'重置起点
Last(1) = This(1)
Last(2) = This(2)
End If
'画线
Me.Line (Last(1), Last(2) + Y_offset)-(This(1), This(2) + Y_offset), DrawColor
Last(1) = This(1)
Last(2) = This(2)
i = i + 1
Wend
End Sub
Private Sub DrawAxe() '绘制坐标的过程
Dim X_offset As Long
Dim Y_offset As Long
Dim i As Long
Me.ScaleMode = vbPixels '取单位长度为像素
X_offset = Me.ScaleWidth * X_Axe '计算坐标轴轴位置
Y_offset = Me.ScaleHeight * Y_Axe
'绘制坐标轴
Me.Line (X_offset, 0)-(X_offset, Me.ScaleHeight)
Me.Line (0, Y_offset)-(Me.ScaleWidth, Y_offset)
'绘制坐标线
For i = X_offset + ScaleRate To Me.ScaleWidth Step ScaleRate
Me.Line (i, 0)-(i, Me.ScaleHeight), vbWhite
Next i
For i = X_offset - ScaleRate To 0 Step -ScaleRate
Me.Line (i, 0)-(i, Me.ScaleHeight), vbWhite
Next i
For i = Y_offset + ScaleRate To Me.ScaleHeight Step ScaleRate
Me.Line (0, i)-(Me.ScaleWidth, i), vbWhite
Next i
For i = Y_offset - ScaleRate To 0 Step -ScaleRate
Me.Line (0, i)-(Me.ScaleWidth, i), vbWhite
Next i
End Sub
Private Sub Form_Paint()
Call Form_Resize
End Sub
Private Sub Form_Resize()
Me.Cls
Call DrawAxe
Call DrawCoordinate
End Sub
将MSChart1.ChartData = MyData '数据
放到for循环后面就好了
最后部分应该是
For i = 0 To 360
MyData(i, 0) = i '-----x轴坐标值--
MyData(i, 1) = 149 * Sin(dblStep * i) + 150 '---Y轴坐标值----------
Next i
MSChart1.ChartData = MyData '数据
哇,好难,我一点都不懂。我只是个初中生,而且是10届的