哪位帮忙给个使用MSChart的实例,学习一下,谢谢!

冰天天 2009-07-07 01:55:18
哪位帮忙给个使用MSChart的实例,学习一下,谢谢!
...全文
400 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
biptwhisky 2010-12-23
  • 打赏
  • 举报
回复
谢谢分享
贝隆 2009-07-07
  • 打赏
  • 举报
回复
正好有一个,发过好几遍了。
Option Explicit
'对于二维散点图来说,第一列代表了X轴坐标,第二列代表了Y轴坐标
'因此在定义二维数据时,第二维定义为0到1,第一维代表了第几点数据,可根据数据点数变化
'下面以数据点数有21点为列子
Dim MyData(360, 1) As Variant
Dim DataT(360, 1) As Variant
Private Const PI = 3.1415926
Dim intC As Integer
Dim dblStep As Double

Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then Command1.Caption = "停止示波器"
If Not Timer1.Enabled Then Command1.Caption = "启动示波器"
End Sub

Private Sub Form_Load()
Dim I As Integer
dblStep = 8 * PI / 360
'-----x轴坐标值-----Y轴坐标值----------
For I = 0 To 360
MyData(I, 0) = I
MyData(I, 1) = 149 * Sin(dblStep * I) + 150
Next I
For I = 0 To 360
DataT(I, 0) = I
DataT(I, 1) = 149 * Cos(dblStep * I) + 150
Next I
intC = 1
'波形图外观设置
With MSChart1
.TitleText = "速度 m/min"
' '设置图线的外观
.Plot.SeriesCollection(1).Pen.Width = 40
.Plot.SeriesCollection(1).Pen.Style = VtPenStyleSolid
' '设置XY轴
.Plot.Axis(VtChAxisIdX).ValueScale.Auto = False
.Plot.Axis(VtChAxisIdY).ValueScale.Auto = False
'// 设置最大值
.Plot.Axis(VtChAxisIdX).ValueScale.Maximum = 360
.Plot.Axis(VtChAxisIdY).ValueScale.Maximum = 300
'// 设置最小值
.Plot.Axis(VtChAxisIdY).ValueScale.Minimum = 0
.Plot.Axis(VtChAxisIdX).ValueScale.Minimum = 0
'//
.Plot.Axis(VtChAxisIdX).ValueScale.MajorDivision = 6 'X轴主要网格数量
.Plot.Axis(VtChAxisIdY).ValueScale.MajorDivision = 6 '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
MSChart1.Plot.AutoLayout = False
MSChart1.Plot.UniformAxis = False
MSChart1.chartType = VtChChartType2dXY '设置图形为二维散点图
MSChart1.ChartData = MyData '数据
End With
End Sub

Private Sub Timer1_Timer()
Dim intP As Integer
For intP = 0 To 359
MyData(intP, 1) = MyData(intP + 1, 1)
Next intP
MyData(360, 1) = 149 * Sin(intC * dblStep) + 150
intC = (intC + 1) Mod 360
MSChart1.ChartData = MyData
End Sub
打死不掉牙 2009-07-07
  • 打赏
  • 举报
回复
MSDN上就有啊,自己查下
使用数组和 ChartData 属性绘制数据
绘制图表最简单的方法就是创建数字型的数组,然后将 ChartData 属性设为该数组,如下例所示:

' 这段代码可以粘贴到一个Form的 Load
' 事件中,该Form包含名为“MSChart1”的
' MSChart 控件。
Dim arrPrices(1 to 10)
Dim i As Integer
For i = 1 to 10
arrPrices(i)= i * 2
Next i
MSChart1.ChartData = arrPrices

上面的代码将产生简单的单系列图表。图表中的一个“系列”就是一个相关的数据点集。例如,典型的系列可以是一年中商品的价格。下面的图表显示了一个单系列图表。



要创建更复杂的多系列图表,必须创建多维数组,如下例所示:

' 系列的数目是由第二个维数决定的。
' 在本例中,图表将有两个系列,
' 每个系列有五个数据点。
Dim arrPriceQuantity(1 to 5, 1 to 2)
Dim i as Integer
For i = 1 to 5
arrPriceQuantity(i, 1) = i ' Series 1
arrPriceQuantity(i, 2) = 0 - i ' Series 2
Next i
MsChart1.ChartData = arrPriceQuantity

这个例子产生的图表如下:



向图表中添加标签
创建多维数组时,可以将第一个系列赋值为字符串;当数组赋值给 ChartData 属性时,字符串将会成为行的标签。下面的代码显示了这个特点。

Dim arrValues(1 to 5, 1 to 3)
Dim i as Integer
For i = 1 to 5
arrValues(i, 1) = "Label " & i ' Labels
arrValues(i, 2) = 0 + i ' Series 1 values.
arrValues(i, 3) = 2 * i ' Series 2 values.
Next i
MsChart1.ChartData = arrValues

上面的代码产生的图表如下:



正如所看见的那样,使用 ChartData 属性创建图表的方法快捷而且简便。但是,使用数组的问题是要将数据取到数组中。这类数据的大多数用户可能更想使用某种电子表格程序,例如 Microsoft Excel,或某种数据库程序,如 Microsoft Access,来存贮和检索数据。

设置或返回数据点
一旦使用来自电子表格或其它数据源的数组创建了图表,可能也希望设置或返回某个指定数据点的值。要做到这一点,可以首先设置 Row 和(如果可以的话) Column 属性,然后设置或返回 Data 属性。例如,在简单的(单系列的)图表中,下面的代码将会改变第三个数据点。

With MSChart1
' 将第三个数据点改为50。
.Row = 3
.Data = 50
End With

如果图表有不止一个系列,那么可以使用 Column 属性来指定系列,然后再象上面那样设置 Row 和 Data 属性。

With MSChart1
' 将第四个系列的第二
' 个数据点设为42。
.Column = 4
.Row = 2
.Data = 42
End With

使用 PointActivated 事件修改数据点
如果已经开始研究 MSChart 控件,那么就会发现它包含了大量的事件。利用这些事件可以对图表编程,使图表能够响应几乎所有的用户操作。作为这种可编程性的例子,下面的示例中将使用 PointActivated 事件来说明如何利用 Series 和 DataPoint 参数修改数据点。(PointActivated 事件在数据点被双击时发生。)Series 和 DataPoint 参数对应于 Column 和 Row 属性,因此可以用来设置 Data 属性:

Private Sub MSChart1_PointActivated(Series As _
Integer, DataPoint As Integer, MouseFlags As _
Integer, Cancel As Integer)
With MSChart1
.Column = Series
.Row = DataPoint
.Data = InputBox _
("Change the data point:", , .Data)
End With
End Sub

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧