Adding shapes to Chart Plot Area via VBA...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
Hi All,

I would like to add a square shape to a Chart Plot Area starting at where the vertical and horizontal axes meet. If my data was static then this would be a simple job, but the data is dynamic (and so are the axes) and therefore I need the square to grow and shrink depending on what is shown.

Is it possible to use some code to do this?

Thanks,

Matty
 
The error bars method works for me? Doesn't it work for you?

It does, but only to a point. The problem is that it only gives me lines, when what I want is full square. Moreover, because my x axis is set to Auto, the horizontal error bar doesn't always extend right over to the y axis (my chart contains both negative and positive values along the x axis, and when set to Auto, Excel does what it wants in terms of setting the lowest values to show on the axis).

That's unless I'm completely missing something...

Cheers,

Matty
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi again

What's the chart type?
For many chart types, another way that will draw the rectangle is simply to add a series with 5 points (the corners + the first corner)

For ex., add a series with

(0,0)
(0,1)
(1,1)
(1,0)
(0,0)

This line will draw a rectangle that starts where the the axes meet, like you said you want.

I'm using a Bubble chart, which doesn't seem compatible with other chart types. Your suggestion was the first thing I tried, but I soon realised this wasn't going to work due to the compatibility issues.

Cheers,

Matty
 
Upvote 0
OK. If this is not solved until tonight (GMT) I'll try to post an example of how to add the shape.
I guess you tried Andrew's suggestion and it doesn't solve your problem?
 
Upvote 0
Hi Chaps,

Below is is an example of what I'm after:

29oplqw.png


The problem is the fact that both the x and y axes will change, yet always need to reference 0 on x axis and 8 and on the y axis and fill the entire gap in between.

Hope this makes sense.

Cheers,

Matty
 
Upvote 0
Hi Matty

I assumed a chart named MyChart in Sheet1.

This is an example based on the chart you posted, that you can adapt.

Code:
Sub ChartAddRect()
Dim cht As Chart
Dim XAxis As Axis, YAxis As Axis
Dim rect As Rectangle
Dim dRLeft As Double, dRTop As Double, dRWidth As Double, dRHeight As Double

Set cht = Worksheets("Sheet1").ChartObjects("MyChart").Chart
Set XAxis = cht.Axes(xlCategory)
Set YAxis = cht.Axes(xlValue)

' position and location of the rectangle
dRLeft = XAxis.Left
dRWidth = (0 - XAxis.MinimumScale) / (XAxis.MaximumScale - XAxis.MinimumScale) * XAxis.Width
dRHeight = (8 - YAxis.MinimumScale) / (YAxis.MaximumScale - YAxis.MinimumScale) * YAxis.Height
dRTop = YAxis.Top + YAxis.Height - dRHeight

' draw the rectangle
Set rect = cht.Rectangles.Add(dRLeft, dRTop, dRWidth, dRHeight)
rect.Name = "MyRect"

End Sub
 
Upvote 0
This is an example based on the chart you posted, that you can adapt.

Thanks pgc01 - it works well!

If I need to adapt the code to create the square in the top right (so 8 - 14 on the y axis and 0 - 25 on the x axis) instead of the bottom left, what would this look like? I've had a play myself, but can't quite get the syntax right.

Thanks,

Matty
 
Upvote 0
Hi Matty

Try:

Code:
' position and location of the rectangle
dRWidth = (XAxis.MaximumScale - 0) / (XAxis.MaximumScale - XAxis.MinimumScale) * XAxis.Width
dRHeight = (YAxis.MaximumScale - 8) / (YAxis.MaximumScale - YAxis.MinimumScale) * YAxis.Height
dRTop = YAxis.Top
dRLeft = XAxis.Left + XAxis.Width - dRWidth
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,591
Members
449,174
Latest member
chandan4057

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