求助:vb调用SQL数据库中的表,将表中数据用mschart画出曲线图(附程序)

icelends icelends 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大侠:

本人问题RT,想要做个界面,将数据库中的程序用曲线显示出来,网上找了许多例子,都是ACCESS数据库的,借用了一下,弄了许久都不见曲线,故来求赐教,不胜感激,先谢过各位啦。
我的程序如下:总觉得数据库给mschart赋值未成功,希望各位给予赐教,不胜感激!


Private Sub Command1_Click()
Dim DblSec As Double, MaxSec As Double, MinSec As Double, TempSec As Double
Dim strconn As String ' 定义连接字符串

Dim MyCon As New ADODB.Connection
Dim Rst As New ADODB.Recordset
Dim MyData() As Double
strconn = " Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password =333666;Initial Catalog =node;DataSource= (local)"

MyCon.ConnectionString = strconn: MyCon.Open

Rst.Open "select * from stOdr", MyCon, adOpenKeyset, adLockOptimistic '表的名字为:gytagb,可根据需要来更改

'conn.Open strconn


ReDim MyData(Rst.RecordCount + 1, 1)
MaxSec = 0: MinSec = 0: MinSec = 0
For i = 0 To Rst.RecordCount + 1
MyData(i, 0) = Rst("时间").Value '第一列存放X轴数据
MyData(i, 1) = Rst("温度").Value '第二列存放Y轴数据
TempSec = DateDiff("s", "0:0:0", Rst.Fields("时间").Value)
If i = 0 Then
MaxSec = TempSec: MinSec = TempSec
End If
If TempSec > MaxSec Then MaxSec = TempSec '求最大值
If TempSec < MinSec Then MinSec = TempSec '求最小值
Rst.MoveNext
Next
Rst.Close: MyCon.Close
Set Rst = Nothing: Set MyCon = Nothing
'波形图外观设置
With MSChart1
.chartType = VtChChartType2dXY '设置图形为二维散点图'注意本语句的位置,如果放在最后X轴坐标将显示成小数而非时间格式
.TitleText = "温度"
' '设置图线的外观
.Plot.SeriesCollection(1).Pen.Width = 30
.Plot.SeriesCollection(1).Pen.Style = VtPenStyleSolid
'设置XY轴
.Plot.Axis(VtChAxisIdX).ValueScale.Auto = False
.Plot.Axis(VtChAxisIdY).ValueScale.Auto = False
'// 设置最大值
.Plot.Axis(VtChAxisIdX).ValueScale.Maximum = 40950 '这里根据你的数据库中的最大值设置一合适值
.Plot.Axis(VtChAxisIdY).ValueScale.Maximum = 40 '这里根据你的数据库中的最大值设置一合适值
'// 设置最小值
.Plot.Axis(VtChAxisIdX).ValueScale.Minimum = 40600
.Plot.Axis(VtChAxisIdY).ValueScale.Minimum = -10
'设置X轴标签显示格式
.Plot.Axis(VtChAxisIdX).Labels(1).Format = "yyyy-m-d hh:mm"
.Plot.Axis(VtChAxisIdX).ValueScale.MajorDivision = 12 'X轴主要网格数量
.Plot.Axis(VtChAxisIdY).ValueScale.MajorDivision = 10 'Y轴主要网格数量
.Plot.Axis(VtChAxisIdX).ValueScale.MinorDivision = 0 'X轴次要网格数量
.Plot.Axis(VtChAxisIdY).ValueScale.MinorDivision = 0 'Y轴次要网格数量
.Plot.Axis(VtChAxisIdX).AxisGrid.MajorPen.Style = VtPenStyleDotted
.Plot.Axis(VtChAxisIdY).AxisGrid.MajorPen.Style = VtPenStyleDotted
.Plot.Axis(VtChAxisIdX).Labels(1).VtFont.Size = 7
.Plot.AutoLayout = False
.Plot.UniformAxis = False

.ChartData = MyData '数据
End With
End Sub

唉 贴不上来图!就是运行后没有曲线 其他都有
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部