excel图表怎么用箭头线连接两个XY散点
时间:2022-01-28 12:10:36
本文介绍如何使用箭头线更加可视化地显示变化的方向,如图1所示。
图1
在Excel 2007及以后的版本中,通过设置系列3的箭头末端格式,很容易实现图1所示的效果。
下面,我们使用VBA来完成箭头线的绘制。
SubConnectTwoXYSeries()
Dim myCht As Chart
Dim mySrs1 As Series
Dim mySrs2 As Series
Dim Npts As Integer, Ipts As Integer
Dim myBuilder As FreeformBuilder
Dim myShape As Shape
Dim Ishp As Long
Dim Xnode1 As Double, Ynode1 As Double
Dim Xnode2 As Double, Ynode2 As Double
Dim Xmin As Double, Xmax As Double
Dim Ymin As Double, Ymax As Double
Dim Xleft As Double, Ytop As Double
Dim Xwidth As Double, Yheight As Double
‘ 必须先选取具有两个系列的图表
If ActiveChart Is Nothing Then
GoTo ExitSub
End If
If ActiveChart.SeriesCollection.Count < 2Then
GoTo ExitSub
End If
Set myCht = ActiveChart
Set mySrs1 = myCht.SeriesCollection(1)
Set mySrs2 = myCht.SeriesCollection(2)
Npts = mySrs1.Points.Count
‘ 两个系列必须具有相匹配数量的点
If mySrs2.Points.Count <> Npts Then
GoTo ExitSub
End If
‘ 移除任何原先的连接箭头线
For Ishp = myCht.Shapes.Count To 1 Step -1
If Left(myCht.Shapes(Ishp).Name, 12) =”ArrowSegment” Then
myCht.Shapes(Ishp).Delete
End If
Next
Xleft = myCht.PlotArea.InsideLeft
Xwidth = myCht.PlotArea.InsideWidth
Ytop = myCht.PlotArea.InsideTop
Yheight = myCht.PlotArea.InsideHeight
Xmin = myCht.Axes(1).MinimumScale
Xmax = myCht.Axes(1).MaximumScale
Ymin = myCht.Axes(2).MinimumScale
Ymax = myCht.Axes(2).MaximumScale
For Ipts = 1 To Npts
‘ 第1个数据点
Xnode1 = Xleft + (mySrs1.XValues(Ipts) -Xmin) * Xwidth / (Xmax – Xmin)
Ynode1 = Ytop + (Ymax -mySrs1.Values(Ipts)) * Yheight / (Ymax – Ymin)
‘ 第2个数据点
Xnode2 = Xleft + (mySrs2.XValues(Ipts) -Xmin) * Xwidth / (Xmax – Xmin)
Ynode2 = Ytop + (Ymax -mySrs2.Values(Ipts)) * Yheight / (Ymax – Ymin)
‘ 绘制连接线
Set myShape = myCht.Shapes.AddLine(Xnode1,Ynode1, Xnode2, Ynode2)
‘ 命名并格式形状为箭头线
With myShape
.Name = “ArrowSegment” &CStr(Ipts)
With .Line
‘ 在这里可以使用你喜欢的格式
.ForeColor.SchemeColor = 12 ‘ 蓝色
.EndArrowheadLength = msoArrowheadLong
.EndArrowheadWidth =msoArrowheadWidthMedium
.EndArrowheadStyle =msoArrowheadTriangle
End With
End With
Next
ExitSub:
EndSub
在运行程序前,先要选择图表。
运行程序后的结果如图2所示。
图2
虽然在Excel 2007及以上的版本中,很容易实现这样的功能,但我们仍然试图使用VBA程序实现同样的效果。通过VBA程序,让我们进一步熟悉图表元素及图表编程的技术。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Excel表格中IF函数最常见用法
![](https://img.aspxhome.com/file/2023/7/a142007_0s.png)
苹果秋季发布会都有哪些看点?
![](https://img.aspxhome.com/file/2023/8/45148_0s.png)
word中怎样给内容添加表格 word中给数据加上表格的设置方法
在Word2010公式中添加几何学符号
![](https://img.aspxhome.com/file/2023/9/29599_0s.jpg)
Win10系统怎么设置定时提醒更换电脑密码?来看详细的设置教程
![](https://img.aspxhome.com/file/2023/9/47469_0s.png)
Excel怎么插入气泡图
![](https://img.aspxhome.com/file/2023/6/54966_0s.gif)
word中图片如何调整大小
![](https://img.aspxhome.com/file/2023/5/32995_0s.jpg)
Word自带的截图功能如何使用?
![](https://img.aspxhome.com/file/2023/7/25367_0s.png)
Excel 2019打印宽高的设置图解教程
![](https://img.aspxhome.com/file/2023/4/a141054_0s.jpg)
Win10如何退出桌面磁贴模式?
![](https://img.aspxhome.com/file/2023/1/50261_0s.png)
Word如何快速生成一段文本
![](https://img.aspxhome.com/file/2023/1/34351_0s.gif)
word怎么绘制飘扬的五星红旗图形?
![](https://img.aspxhome.com/file/2023/1/22871_0s.png)
word 中怎么自定义快捷键/删除快捷键
![](https://img.aspxhome.com/file/2023/4/32644_0s.jpg)
Win10运行DNF全屏后黑屏怎么解决?
![](https://img.aspxhome.com/file/2023/2/50342_0s.png)
Win10设置管理员权限怎么获取?Win10设置管理员权限获取方法步骤
![](https://img.aspxhome.com/file/2023/1/50361_0s.png)
Excel单元格边界附加功能使用技巧
![](https://img.aspxhome.com/file/2023/9/39299_0s.jpg)
word图表中怎么插入题注?
![](https://img.aspxhome.com/file/2023/9/22909_0s.jpg)
word行距怎么设置?
![](https://img.aspxhome.com/file/2023/8/19348_0s.jpg)
只需几步 Excel自动插入序号方法介绍
![](https://img.aspxhome.com/file/2023/1/40831_0s.jpg)
Word怎么把竖着排列的文字变成横着排列
![](https://img.aspxhome.com/file/2023/9/34129_0s.png)