Chart.PlotArea.Inside Values Incorrect Unless Set at Zoom = 100

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.
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
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,215,471
Messages
6,124,999
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top