计算折线图上两点的角度

时间:2011-08-23 20:33:08

标签: vba excel-vba excel-2007 excel

我想在折线图上获得两个点的角度。 我知道如何计算角度,问题是我需要seriescollection.point的x和y,我不知道如何得到它。

有人可以帮我吗?

编辑:

Jean-FrançoisCorbett向我展示了如何获得积分,我的意思是从顶部和左侧,而不是指向图形(在X刻度和Y刻度上)虽然它可以工作。 我错了。如何计算下图中的角度?

enter image description here

2 个答案:

答案 0 :(得分:3)

你问如何获得图表系列中点的(x,y)坐标。方法如下:

Dim c As Chart
Dim s As Series
Dim x As Variant
Dim y As Variant

Set c = ActiveChart
Set s = c.SeriesCollection.Item(1)

x = s.XValues
y = s.Values

编辑据编辑问题我可以看出,OP现在需要每个点的像素坐标,原点位于图的左上角。为此,您只需按轴宽和跨度进行缩放。在线图(我讨厌)的情况下,x轴有点棘手,因为没有最小或最大比例属性;必须使用“类别”的数量。以下代码执行此缩放:

Dim c As Chart
Dim s As Series
Dim xa As Axis
Dim ya As Axis
Dim x As Variant
Dim y As Variant
Dim i As Long

Set c = ActiveChart
Set s = c.SeriesCollection.Item(1)
Set xa = c.Axes(xlCategory)
Set ya = c.Axes(xlValue)

x = s.XValues
y = s.Values
For i = LBound(x) To UBound(x)
    ' Scale x by number of categories, equal to UBound(x) - LBound(x) + 1
    x(i) = (i - LBound(x) + 0.5) / (UBound(x) - LBound(x) + 1) * xa.Width
    ' Scale y by axis span
    y(i) = ya.Height - y(i) / (ya.MaximumScale - ya.MinimumScale) * ya.Height
Next i

请注意,y在绘图上沿负y方向增加,因为您希望原点位于左上角。

使用此xy,您可以计算出屏幕上显示的角度。

答案 1 :(得分:0)

XY值无法直接从Point对象访问(我尽我所知),但它们代表传递给图表的实际值。尝试从存储它们的工作表中访问它们。

如果不可用,请尝试Series.values,它返回一个Y值数组,并Series.XValues,它返回一个X值数组。 (见MSDN Reference

相关问题