pbornemeier
Well-known Member
- Joined
- May 24, 2005
- Messages
- 3,911
In using code to insert additional lines the height of the plotarea in a chart, I found that the lines sometimes were not the correct height. I found that when the window zoom was not at 100 the Chart.PlotArea.InsideHeight Value was not always correct. Further work showed that the InsideWidth value was also not reliable unless at 100% zoom. The simple solution is to set the zoom to 100 before working with PlotArea.Inside measurements, but I hope someone can figure out why it is happening.
Create a line chart with more than 10 points in it and run the following code to insert a vertical line at the 10 point that fills the inside plotarea from top to bottom. If the zoom is other than 100, the line may not be the correct height.
Create any Chart in a worksheet and run the following code to see 3 differently sized rectangles that are all supposed to outline the plotarea
Create a line chart with more than 10 points in it and run the following code to insert a vertical line at the 10 point that fills the inside plotarea from top to bottom. If the zoom is other than 100, the line may not be the correct height.
Code:
Sub DrawVerticalLineAtPoint10()
Dim sngXPos As Single
ActiveWindow.Zoom = 100
ActiveSheet.ChartObjects(1).Select
sngXPos = ExecuteExcel4Macro("get.chart.item(1,1,""S1P10"")") 'Point 10 Left Value
ActiveChart.Shapes.AddLine(sngXPos, ActiveChart.PlotArea.InsideTop, _
sngXPos, ActiveChart.PlotArea.InsideTop + _
ActiveChart.PlotArea.InsideHeight).Name = "L_P10" 'InsideTop to InsideBottom
End Sub
Create any Chart in a worksheet and run the following code to see 3 differently sized rectangles that are all supposed to outline the plotarea
Code:
Sub PlotareaInsideHeightProblemDemo()
Dim pa
ActiveWindow.Zoom = 100 Correct Placement
With ActiveSheet.ChartObjects(1).Chart
Set pa = .PlotArea
With .Shapes.AddShape(msoShapeRectangle, _
pa.InsideLeft, pa.InsideTop, _
pa.InsideWidth, pa.InsideHeight)
.Fill.Transparency = 1
.Line.DashStyle = msoLineDashDot
.Line.ForeColor.rgb = rgb(255, 0, 0)
End With
ActiveWindow.Zoom = 90 'InsideHeight too small
Set pa = .PlotArea
With .Shapes.AddShape(msoShapeRectangle, _
pa.InsideLeft, pa.InsideTop, _
pa.InsideWidth, pa.InsideHeight)
.Fill.Transparency = 1
.Line.DashStyle = msoLineDashDot
.Line.ForeColor.rgb = rgb(0, 255, 0)
End With
ActiveWindow.Zoom = 50 'InsideHeight too big, InsideWidth too small
Set pa = .PlotArea
With .Shapes.AddShape(msoShapeRectangle, _
pa.InsideLeft, pa.InsideTop, _
pa.InsideWidth, pa.InsideHeight)
.Fill.Transparency = 1
.Line.DashStyle = msoLineDashDot
.Line.ForeColor.rgb = rgb(0, 0, 255)
End With
End With
ActiveWindow.Zoom = 100
Set pa = Nothing
End Sub