Hi all
I have generated code to produce Open, High, Low, Close stock charts (Primary Axis) with two moving average lines (Secondary Axis) sharing a common Y axis (dates).
I wanted to create a fill with boundries equalling:
- bottom boundry = Min on Y axis
- top boundry = Close price
- left boundry = First High/Low line which I thought would be equal to Min on X axis
- right boundry = Last High/Low line which I thought would be equal to Max on X axis.
Jon Peltier had created some code to do exactly that:
[pre]
[/pre]
This is close, but the shape resizes slightly wider than high/low lines on the left and quiet a bit wider than the high/low lines on the right, but is a closer fit than the original shape produced. I have tried to change to:
ActiveChart.Shapes.Range(Array("Freeform 1").Width = Xwidth - 61.5
ActiveChart.Shapes.Range(Array("Freeform 1").Left = Xleft + 6[/code][/pre]
which lines up 1 stock perfectly but not others.
What is it that the " - 61.5" and the " + 6" are referring to that I am missing so that I can include in the above code to make it work on every graph generated?
Any help/advice would be greatly appreciated
Cheers
Shaun
I have generated code to produce Open, High, Low, Close stock charts (Primary Axis) with two moving average lines (Secondary Axis) sharing a common Y axis (dates).
I wanted to create a fill with boundries equalling:
- bottom boundry = Min on Y axis
- top boundry = Close price
- left boundry = First High/Low line which I thought would be equal to Min on X axis
- right boundry = Last High/Low line which I thought would be equal to Max on X axis.
Jon Peltier had created some code to do exactly that:
[pre]
Code:
Dim myCht As Chart
Dim mySrs As Series
Dim Npts As Integer, Ipts As Integer
Dim myBuilder As FreeformBuilder
Dim myShape As Shape
Dim Xnode As Double, Ynode 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
Set myCht = ActiveChart
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
Set mySrs = myCht.SeriesCollection(4)
Npts = mySrs.Points.Count
' first point
Xnode = Xleft + (mySrs.XValues(1) - Xmin) * Xwidth / (Xmax - Xmin)
Ynode = Ytop + Yheight
Set myBuilder = myCht.Shapes.BuildFreeform(msoEditingAuto, Xnode, Ynode)
' remaining points
For Ipts = 1 To Npts
Xnode = Xleft + (mySrs.XValues(Ipts) - Xmin) * Xwidth / (Xmax - Xmin)
Ynode = Ytop + (Ymax - mySrs.Values(Ipts)) * Yheight / (Ymax - Ymin)
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode
Next
Xnode = Xleft + (mySrs.XValues(Npts) - Xmin) * Xwidth / (Xmax - 'Xmin)
Ynode = Ytop + Yheight
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode
Xnode = Xleft + (mySrs.XValues(1) - Xmin) * Xwidth / (Xmax - Xmin)
Ynode = Ytop + Yheight
myBuilder.AddNodes msoSegmentLine, msoEditingAuto, Xnode, Ynode
Set myShape = myBuilder.ConvertToShape
With myShape.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0.8
.Solid
End With
With myShape.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
With myShape.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
End With
With myShape.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 1
End With
Though it never seems to line up correctly.
The issue I am having with it is that I then have to manually adjust the shape to "line up" with the first x Axis point and last X axis point. I have tried to remedy this by:
[pre][code]ActiveSheet.ChartObjects("OHLC").Activate
ActiveChart.Shapes.Range(Array("Freeform 1")).Select
ActiveChart.Shapes.Range(Array("Freeform 1")).Width = Xwidth
ActiveChart.Shapes.Range(Array("Freeform 1")).Left = Xleft
This is close, but the shape resizes slightly wider than high/low lines on the left and quiet a bit wider than the high/low lines on the right, but is a closer fit than the original shape produced. I have tried to change to:
ActiveChart.Shapes.Range(Array("Freeform 1").Width = Xwidth - 61.5
ActiveChart.Shapes.Range(Array("Freeform 1").Left = Xleft + 6[/code][/pre]
which lines up 1 stock perfectly but not others.
What is it that the " - 61.5" and the " + 6" are referring to that I am missing so that I can include in the above code to make it work on every graph generated?
Any help/advice would be greatly appreciated
Cheers
Shaun