+ Reply to Thread
Results 1 to 15 of 15

VBA Resize Chart Area and Plot Area

  1. #1
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    VBA Resize Chart Area and Plot Area

    Trying to center align a resized Plot Area in a resized Chart Area.

    1. CHART AREA
    The Chart Area's Border Line Weight = 6.
    So I order to have the Chart Area cover the exact selected cell range horizontally, I resized the Chart Area.
    I did this by counting the Pixels from Left, with the cells 65 Pixels wide:
    Please Login or Register  to view this content.
    2. PLOT AREA
    Found this code by Andy Pope:
    https://www.excelbanter.com/charts-c...plot-area.html
    Please Login or Register  to view this content.
    I want the Plot Area to be as wide as possible (as wide as the Chart Area allows) and place it in the Chart Area's Center.
    So I added these lines, using my Pixelcount from the resized Chart Area:

    Please Login or Register  to view this content.
    Somehow, this doesn't add up, the numbers don't work. The Plot Area is not centered.
    It seems, that the resized Chart Area causes the irritation.
    So I removed "Plot Resize" from the "Create" Macro, recorded another one to have some values
    and finally ran these recorded values in a second Macro:
    Please Login or Register  to view this content.
    This seems to do the job, more or less, at Zoom 400, you can see, it's not exact.
    And I have to record values by eyeballing and run a second Macro.

    How can I make this work in one run? Without using the mouse?
    (This is what I'm really starting to like about VBA: not using the mouse.)

    And if that's not possible, how can I make these two lines work in the second Macro:
    (Couldn't work out, what to add at the beginning, to make it work by itself.)
    Please Login or Register  to view this content.
    Thanks a lot!
    Toni
    Attached Files Attached Files
    Last edited by briskie; 03-24-2024 at 02:51 PM.

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: VBA Resize Chart Area and Plot Area

    Check this code:
    Please Login or Register  to view this content.
    Artik

  3. #3
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    Hi Artik,

    very nice!

    1. How did you calculate this value?
    Please Login or Register  to view this content.
    2. Your code works perfectly with an unformatted, standard Chart Area.
    Might there be a way to add a custom border weight into the calculation?
    Maybe even as a variable?

    So let's say:
    - Cell Width = 65 Pixels
    - Chart Area Width = 2 Cells Wide = 130 Pixels
    - Border Weight = 6 Pixels

    Without any resizing, the Chart Area covers the neighboring cells, left and right, with half of the Border Weight = 3 Pixels.
    So I resize the Chart Area to exactly and only cover the selected Cell Range.
    This is the first thing to do:
    Please Login or Register  to view this content.
    So now the Chart Width is resized from 130 Pixels wide to 124 Pixels wide.

    Second thing to do: Resize the Plot Area Width accordingly.

    So might there be a way to say:
    a.
    “Chart Area Width = Chart Area Width - 2x Border Width/2” (with Border Width 6 this would be: 2x6=12/2=6)
    And then say:
    b.
    "Plot Area Width = Chart Area Width:
    Please Login or Register  to view this content.
    The problem is the combination of resizing the chart area first and then resizing the plot area in there.
    So the “Plot Area resize procedure” needs to know, that the Chart Area has been resized.
    Is this even possible? Or does it need two separate Macros for Excel to not be confused?

    The idea is to align a number of charts with wider borders without overlapping the neighboring cells.
    And then have all Plot Areas perfectly aligned in those Chart Areas.

    Thanks again!
    Toni

  4. #4
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    Finally managed to resize the Plot Area's Width to exactly sit between the Chart Borders.
    But hitting the right number for .PlotArea.Left was a matter of trial and error. (Not too satisfying.)
    And this example is based on a cell width of 47 Pixels and needs to be adapted to your cell width.
    Which means for now: record a Macro, move the Plot Area and go from there.

    And: I've realised, that this only works with Ticklabels Font Size = 8 or less.
    I've placed "AutoScaleFont = False" in 4 different places:
    .ChartTitle
    .ChartArea
    .Ticklabels x-Axis
    .Ticklabels y-Axis
    Still, the Plot Area's Size shrinks, starting with Ticklabels Font Size = 9.

    So the first question is more or less solved, if you have a "standard" cell width and have some patience,
    you can dial into the right .PlotArea.Left value. Should anybody have an actual mathematical solution, I'd be all ears.
    I wonder, if the Border Weight can somehow be included into the equation.

    So now, what's left to solve, is AutoScaleFont.
    What am I missing?

    Thank you!

  5. #5
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    Tweaked around a bit more, got the values for Font Size 10:
    Please Login or Register  to view this content.
    Aren't these strange?

  6. #6
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: VBA Resize Chart Area and Plot Area

    Do not use absolute values. All dimensions can be calculated.
    Try the following code:
    Please Login or Register  to view this content.
    Artik

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Resize Chart Area and Plot Area

    This appears to work for me.

    Please Login or Register  to view this content.
    Ages ago the .chart.chartarea values were useful and relative to the internal sizes of the chartobject. Nowadays they appear to reflect the parent object values.
    Cheers
    Andy
    www.andypope.info

  8. #8
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    Hi Artik.

    This is totally amazing!

    So sweet, to have the border weight included in the chart area setting.
    And not having to deal with counting pixels anymore is so much more fun.

    Two things:
    1. Where to place AutoScaleFont = False?
    Unfortunately, the Plot Area still resizes, depending on the Label Font Size.
    2. Might it also be possible, as an option, to have the Plot Area exactly touch the border?
    With "zero air"? So that each bar would be exactly centered in their spaces?
    I was able to do this manually, but with absolute values only.

    @Andy: I've tried both codes, both work for me.
    Should there be a difference in the outcome?

    Thanks Artik, thanks Andy!

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Resize Chart Area and Plot Area

    Just different code.

    If you want the plot area to align with the cell edges then you would need to make the chart object wider/taller by the objects border width.
    Last edited by Andy Pope; 03-27-2024 at 11:58 AM.

  10. #10
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    Thanks Andy

    Sorry, not to touch the cell edges, to touch the border.
    I've been able to do this by recording a Macro and use those absolute values.
    So just wondering, if this could also go into the equation somehow.

    Like this (for super narrow spaces):

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Resize Chart Area and Plot Area

    As mentioned you need to take in to account the chart border size.

    Please Login or Register  to view this content.
    This should position chart border against cell edges. And plot area against inside border

  12. #12
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    Ok. So the chart object is the plot area.
    The code does exactly that, thanks again! But only, if the axis labels are turned off (visible=false).

    I've placed "AutoScaleFont = False" in 4 different places:

    .ChartTitle
    .ChartArea
    .Ticklabels x-Axis
    .Ticklabels y-Axis

    Still, the Plot Area's Size is affected by the Ticklabels Font Size.

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: VBA Resize Chart Area and Plot Area

    You can try using the PlotAreas Inside dimensions.

    Please Login or Register  to view this content.
    When you apply a border to the plot area it is actually the Inside dimensions that are formatted.
    You can see the out plotarea by selecting it and resizing. The dotted outline is the plotarea.

    I'm sure historically the plotarea inside values were readonly

  14. #14
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    Ok, tried that as well, same "bug": the plot area only stays in place with x-axis labels off.
    So maybe just go with Textbox Labels?

  15. #15
    Registered User
    Join Date
    03-10-2024
    Location
    Berlin
    MS-Off Ver
    mac2016
    Posts
    91

    Re: VBA Resize Chart Area and Plot Area

    A bit strange: I've been applying the code in different worksheets with similarly narrow cell widths.
    In some cases, the plot area jumps, in others not.
    So in some cases textbox labels will do, in others the axis labels work just fine (with mentioned font sizes).

    Thank you very much Artik!
    Thank you very much Andy!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 12-04-2023, 07:50 PM
  2. [SOLVED] Chart to resize plot area based on horizontal axis data
    By jeffreybrown in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-11-2018, 12:00 PM
  3. Replies: 4
    Last Post: 02-05-2016, 11:26 AM
  4. Replies: 0
    Last Post: 08-19-2013, 06:22 PM
  5. Resize plot area in a chart area
    By Darlene in forum Excel General
    Replies: 0
    Last Post: 07-10-2006, 09:54 AM
  6. Resize chart area without resizing plot area
    By Janwillem van Dijk in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 08-01-2005, 09:05 PM
  7. [SOLVED] Resize chart area without resizing plot area
    By Janwillem van Dijk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2005, 09:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1