|
楼主 |
发表于 2019-1-22 16:29
|
显示全部楼层
2.5 图表缩放
图表缩放使用了2种技术,第一个示例使用了控件的MSChart1.Plot.LocationRect属性直接修改绘图区的矩形大小,缩放之后,按住鼠标可拖动图表查看。设置了3个功能:放大、缩小和还原,因此设置了几个变量保存原始设置。注意点是要保持绘图区的高宽比,避免在缩放过程中图形变形,还有就是LocationRect的坐标系是以图表控件的左上角为原点的。第二个示例是根据用户选中的数据点,获取其相邻的其他数据点数据,用另一个窗体显示出来,实现细节的展示。
第一个缩放示例(附件窗体3)的完整代码如下,数据是散点图中的数据:
Private XVSMinDiv%, XVSMajDiv%
Private YVSMinDiv%, YVSMajDiv%
Private XCSDivPerTk%, XCSDivPerLb%
Private Sub CommandButton1_Click()
Dim AspectRatio!, PlotH!, PlotW!
Dim XIncrement!, YIncrement!, R!
With MSChart1
.Plot.AutoLayout = False
R = 0.2'一次缩放原大小的20%
With .Plot.LocationRect
PlotH = .Max.Y - .Min.Y
PlotW = .Max.X - .Min.X
AspectRatio = PlotH / PlotW '绘图区高宽比
XIncrement = PlotW * R 'X轴增量
YIncrement = (PlotW + XIncrement) * AspectRatio - PlotH 'Y轴增量
.Min.Set .Min.X - XIncrement / 2, .Min.Y - YIncrement / 2
.Max.Set .Max.X + XIncrement / 2, .Max.Y + YIncrement / 2
End With
If .ChartType = VtChChartType2dXY Then
With .Plot.Axis(VtChAxisIdX).ValueScale
.Auto = False
.MajorDivision = Application.RoundUp(.MajorDivision * (1 + R), 0)
.MinorDivision = 0
End With
.Plot.Axis(VtChAxisIdX).Labels(1).Format = "0.0"
Else
With .Plot.Axis(VtChAxisIdX).CategoryScale
.Auto = False
.DivisionsPerTick = Application.Max(Int(.DivisionsPerTick / (1 + R)), 1)
.DivisionsPerLabel = .DivisionsPerTick
End With
End If
With .Plot.Axis(VtChAxisIdY).ValueScale
.Auto = False
.MajorDivision = Application.RoundUp(.MajorDivision * (1 + R), 0)
.MinorDivision = 0
End With
.Plot.Axis(VtChAxisIdY).Labels(1).Format = "0.0"
End With
End Sub
Private Sub CommandButton2_Click()
With MSChart1
.Plot.AutoLayout = True
With .Plot.Axis(VtChAxisIdX).ValueScale
.Auto = False
.MajorDivision = XVSMajDiv
.MinorDivision = XVSMinDiv
End With
With .Plot.Axis(VtChAxisIdX).CategoryScale
.Auto = False
.DivisionsPerLabel = XCSDivPerLb
.DivisionsPerTick = XCSDivPerTk
End With
With .Plot.Axis(VtChAxisIdY).ValueScale
.Auto = False
.MajorDivision = YVSMajDiv
.MinorDivision = YVSMinDiv
End With
End With
End Sub
Private Sub CommandButton3_Click()
Dim AspectRatio!, PlotH!, PlotW!
Dim XIncrement!, YIncrement!, R!
With MSChart1
.Plot.AutoLayout = False
R = 0.2
With .Plot.LocationRect
PlotH = .Max.Y - .Min.Y
PlotW = .Max.X - .Min.X
AspectRatio = PlotH / PlotW
XIncrement = PlotW * R
YIncrement = PlotH - (PlotW - XIncrement) * AspectRatio
.Min.Set .Min.X + XIncrement / 2, .Min.Y + YIncrement / 2
.Max.Set .Max.X - XIncrement / 2, .Max.Y - YIncrement / 2
End With
If .ChartType = VtChChartType2dXY Then
With .Plot.Axis(VtChAxisIdX).ValueScale
.Auto = False
.MajorDivision = Application.Max(Int(.MajorDivision / (1 + R)), 1)
.MinorDivision = 0
End With
.Plot.Axis(VtChAxisIdX).Labels(1).Format = "0.0"
Else
With .Plot.Axis(VtChAxisIdX).CategoryScale
.Auto = False
.DivisionsPerTick = Application.RoundUp(.DivisionsPerTick * (1 + R), 0)
.DivisionsPerLabel = .DivisionsPerTick
End With
End If
With .Plot.Axis(VtChAxisIdY).ValueScale
.Auto = False
.MajorDivision = Application.Max(Int(.MajorDivision / (1 + R)), 1)
.MinorDivision = 0
End With
.Plot.Axis(VtChAxisIdY).Labels(1).Format = "0.0"
End With
End Sub
Private Sub UserForm_Initialize()
Dim arr()
arr = Range("a1:b200")
With MSChart1
.TitleText = "变化趋势图"
.Title.VtFont.VtColor.Blue = 255
.Plot.Axis(VtChAxisIdX).AxisTitle.Text = "时间(单位:秒)"
.Plot.SeriesCollection(1).Pen.Width = 20
.Plot.SeriesCollection(1).Pen.style = VtPenStyleSolid
With .Plot.Axis(VtChAxisIdX).ValueScale
.Auto = False
.Maximum = 10
.Minimum = 0
.MajorDivision = 6
.MinorDivision = 0
XVSMinDiv = .MinorDivision
XVSMajDiv = .MajorDivision
End With
With .Plot.Axis(VtChAxisIdX).CategoryScale
XCSDivPerTk = .DivisionsPerTick
XCSDivPerLb = .DivisionsPerLabel
End With
With .Plot.Axis(VtChAxisIdY).ValueScale
.Auto = False
.Maximum = 400
.Minimum = -800
.MajorDivision = 12
.MinorDivision = 0
YVSMinDiv = .MinorDivision
YVSMajDiv = .MajorDivision
End With
.Plot.Axis(VtChAxisIdX).AxisGrid.MajorPen.style = VtPenStyleDotted
.Plot.Axis(VtChAxisIdY).AxisGrid.MajorPen.style = VtPenStyleDotted
.Plot.UniformAxis = False
.ChartType = VtChChartType2dXY
.ChartData = arr
End With
End Sub
效果图:
第二个缩放示例(附件窗体2)完整代码如下,核心就是取得选中的数据点周围的数据:
Private Sub MSChart1_PointSelected(Series As Integer, DataPoint As Integer, MouseFlags As Integer, Cancel As Integer)
Dim PartData(), XARR(), YARR()
Dim XMAX!, XMIN!, YMAX!, YMIN!
Dim pt%, i%, j%, k%
With MSChart1
If .rowCount < 7 Then Exit Sub
ReDim PartData(5, .columnCount)
Select Case DataPoint
Case 1, 2
pt = 1
Case .rowCount - 1, .rowCount
pt = .rowCount - 4
Case Else
pt = DataPoint - 2
End Select
For i = 0 To .columnCount
PartData(0, i) = .ChartData(0, i) '列标签
Next
For i = pt To pt + 4
k = k + 1
PartData(k, 0) = .ChartData(i, 0) '行标签
For j = 1 To .columnCount
PartData(k, j) = .ChartData(i, j)
Next
Next
End With
XARR = Application.Index(PartData, 0, 2)
XMAX = Application.Max(XARR)
XMIN = Application.Min(XARR)
YARR = Application.Index(PartData, 0, 3)
YMAX = Application.Max(YARR)
YMIN = Application.Min(YARR)
With UserForm6.MSChart1
.ChartType = MSChart1.ChartType
.Plot.UniformAxis = False
.ChartData = PartData
With .Plot.Axis(VtChAxisIdX).ValueScale
.Auto = False
.Maximum = XMAX
.Minimum = XMIN
.MajorDivision = 10
.MinorDivision = 2
End With
.Plot.Axis(VtChAxisIdX).Pen.VtColor.Red = 255
With .Plot.Axis(VtChAxisIdY).ValueScale
.Auto = False
.Maximum = YMAX
.Minimum = YMIN
.MajorDivision = 10
.MinorDivision = 2
End With
End With
UserForm6.Show
End Sub
Private Sub UserForm_Initialize()
Dim arr()
arr = Range("a1:b200")
With MSChart1
.TitleText = "变化趋势图"
.Title.VtFont.VtColor.Blue = 255
.Plot.Axis(VtChAxisIdX).AxisTitle.Text = "时间(单位:秒)"
With .Plot.SeriesCollection(1)
.Pen.Width = 20
.SeriesMarker.Auto = False
.SeriesMarker.Show = True
.DataPoints(-1).Marker.style = VtMarkerStyle3dBall
.DataPoints(-1).Marker.Size = 120
.DataPoints(-1).Marker.FillColor.Set 0, 0, 100
End With
With .Plot.Axis(VtChAxisIdX).ValueScale
.Auto = False
.Maximum = 10
.Minimum = 0
.MajorDivision = 6
.MinorDivision = 0
End With
With .Plot.Axis(VtChAxisIdY).ValueScale
.Auto = False
.Maximum = 400
.Minimum = -800
.MajorDivision = 12
.MinorDivision = 0
End With
.AllowSeriesSelection = False
.Plot.UniformAxis = False
.ChartType = VtChChartType2dXY
.ChartData = arr
End With
End Sub
如果觉得MSChart控件拖动鼠标体验不好,又苦于没有滚动条,可以把控件放到框架控件中,利用框架的滚动条。这里就不写代码了。
|
|