开发者

Calculate angle of two points on Line chart

开发者 https://www.devze.com 2023-03-30 19:24 出处:网络
I want to get the angle of two points on a Line chart. I know how to calculate an angle, the problem is that I need the x and y of the seriescollection.point and I have no idea how to get it.

I want to get the angle of two points on a Line chart. I know how to calculate an angle, the problem is that I need the x and y of the seriescollection.point and I have no idea how to get it.

Can someone help me with it?

EDIT:

Jean-François Corbett showed me how to get the points, I meant from top and left, and not point on the graph (on X scale and Y scale) though it can work. I calculate it 开发者_JS百科wrong. how can I calculate the angles in the picture below?

Calculate angle of two points on Line chart


You ask how to get the (x,y) coordinates of points in a chart series. Here is how:

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

EDIT As far as I can tell from the edited question, OP now wants the pixel coordinates of each point, with origin at the top left of the plot. To do so, you just need to scale by the axis width and span. The x axis is a bit tricky in the case of line plots (which I hate), because there is no min or max scale property; have to use the number of "categories" instead. The following code does this scaling:

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

Note that y increases along the negative y direction on the plot, since you want the origin to be at the top left.

Using this x and y, you can calculate your angle as seen on the screen.


The X and Y values are not directly accessible from the Point object, (as best as I can tell), but they represent actual values passed to the graph. Try accessing them from the worksheet where they are stored.

If that is unavailable, try Series.values, which returns an array of Y-values, and Series.XValues, which returns an array of X-values. (See MSDN Reference)

0

精彩评论

暂无评论...
验证码 换一张
取 消