+ Reply to Thread
Results 1 to 16 of 16

Add a shape to the XAxis in a chart

  1. #1
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Add a shape to the XAxis in a chart

    Hi,
    I hope this is the right forum. Since I wanted to do this with VBA I put it here, even though it's a chart question.

    Here goes.
    I am trying to find out if it is possible to add a shape (rectangle) to cover the XAxis line in a scatter chart. The XAxis moves based on the min and max values of the YAxis so it is not always in the center (tried the half height trick).
    I get it to the chart but have to position it manually every time.
    Can I find the XAxis position relative to the chart height?

    It may not be possible but I thought I would ask anyway.
    Hopefully my question makes sense, it's hard to get what I am thinking on 'paper' sometimes

    Thanks in advance for any help or suggestions.
    I appreciate it.

    D
    Last edited by weebo35; 11-12-2020 at 04:46 PM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Add a shape to the XAxis in a chart

    I am not sure I understand exactly what you are trying to do. Perhaps a few observations/questions will help clarify:
    Can I find the XAxis position relative to the chart height?
    Yes, you can. You can use the .Top property to find the position of the axis relative to the chart area, or you can use the .CrossesAt property (combined with the .MaximumScale and/or .MinimumScale properties) to determine where the horizontal axis is positioned. It appears that your goal is to position the axis in the "center" (whether that is chart area center or center of vertical axis is unclear). The .Top property is read only, so it cannot be used to position the axis, but the .CrossesAt property is read/write, so you can use that property to move the axis.

    If your goal is to "hide" the axis so it is not visible, simply format the axis so it is invisible (line color is white if your chart has a white background for example).

    Help file for the Axis object with links to each property: https://docs.microsoft.com/en-us/off...excel.axis.top

    Does any of that help?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Re: Add a shape to the XAxis in a chart

    Hi MrShorty,

    Thank you so much for replying.
    I am not simply trying to remove the XAxis but to 'replace' it with a rectangle shape that has information on it. The shape will need to be where the XAxis currently is, regardless of the position. I do not need to center the XAxis on the chart because there may or not be more data above or below the XAxis.

    Does that help at all?

    Thanks!

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Add a shape to the XAxis in a chart

    My first thought is to use a dummy data point and its data label for this, rather than a "free" rectangle shape. Depending on exactly what is going on in this chart, VBA may not even be required.
    1) Enter/calculate X,Y positions for the dummy point in the spreadsheet. The big question that I see is how does your chart choose where the X axis is positioned -- what setting checked in the "horizontal axis crosses at" property of the format vertical axis dialog.
    2) Add this point to the chart as an additional data series and add a data label to this point. Enter the information into the data label or enter the information into a cell and use that cell as the data label text (feature natively supported in Excel 2013 and later, for 2010, you will either need to make your own VBA procedure that will link the text to the cell or use an add-in like Rob Bovey's free XY chart labeler add-in).

    When it's all set up, the position of the dummy point will be at/near the x axis position, and the attached data label will follow it around.

    The devil is in the details, so we would probably need some more detail about your sheet/chart to help with those details, but that's basically how I would expect to do this.

  5. #5
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Re: Add a shape to the XAxis in a chart

    Hi again,

    No, sorry, a rectangle is what it needs to be, not a data point.
    I dont know if this will help or not. Hope I did that right
    Attachment 703365

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Add a shape to the XAxis in a chart

    Your attachment failed to attach. Be sure to follow the instructions in the yellow banner at the top of the page.

  7. #7
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Re: Add a shape to the XAxis in a chart

    did this work?
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Re: Add a shape to the XAxis in a chart

    Hi there.
    I have re-read your previous post and it might be something that could work. It hadn't occurred to me. I just need to figure out how to create a line on the XAxis that is thick enough and color it to a heat scale (green, yellow, red) and this may just work.

    Any thoughts on that?

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Add a shape to the XAxis in a chart

    Your "line shape" that you have placed over the x axis contains no text, but looks like a "thick" line (about 7 or 8 points?) rather than the default thin line used by the x axis. I move your shape out of the way (eventually to delete once I'm done), then enter the format axis dialog. In the "line style" section, I choose a suitable width (as noted, about 7 or 8 points or whatever tickles your fancy). In the line color section, choose the gradient line option, then fiddle with the various settings to get the appropriate green-yellow-red gradient look that you want. The main advantage to using axis formatting rather than a superimposed shape object is that the chart automatically knows where the axis belongs, and there is no need for a routine that will reposition the object with each change/recalculate of the chart.

    As for the text boxes with the information above and below, again, I would prefer a data label attached to a dummy series rather than free floating text boxes. I enter 60 in two cells, 300 in an adjacent cell and -100 in another adjacent cell. Enter "information above" and "information below" in two more adjacent cells. Then add a new data series, then run my "add data labels" routine from Rob Bovey's add-in and tell it to use the text in C1 and C2 for the data label text positioned below the data point. Format the data series so they are invisible (no marker and no line). Again, the advantage of using the data label instead of a free floating text box is that the position of the data label is tied to the plotted data point. As the chart changes/recalculates, the data label text will follow its attached data point.

    I didn't spend a lot of time on this, but look at the direction I'm going and see if you like it.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Re: Add a shape to the XAxis in a chart

    yes that is what I am looking for but how do i do it in VBA?
    I can figure out the dummy values and text labels and even get the XAxis to get a width, but how do I get it to do the gradient in VBA? That part I am struggling with right now. I can't do 35 chats every time manually
    Love the solution! It's 95% there!
    Thank you!

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Add a shape to the XAxis in a chart

    That's not the kind of VBA I do, so I am not sure how to change the line gradient formatting using VBA. Usually I try recording a macro while making the changes and use the recorded code, but, in this case, the macro recorder doesn't tell me anything. Let me see if anyone else knows how to access these formatting options in VBA.

    In the meantime, it might help someone if you explain exactly what you need VBA to do. Does VBA need to build the entire chart from scratch or just make changes to the axis gradient coloring or some other scope?

  12. #12
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Re: Add a shape to the XAxis in a chart

    I just need the information for the piece I am requesting. The chart is built using VBA and it 95% complete. I tried the recorder as well, hence the desperate need for help.

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Add a shape to the XAxis in a chart

    I don't think that functionality is exposed in VBA unfortunately. There are quite a few gaps in the object model when it comes to charts.
    Rory

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

    Re: Add a shape to the XAxis in a chart

    You might be able to use copy/paste

    If you select the chart and Copy. Then select a chart without the gradient axis line and do Paste > Paste Special > Formats.
    It's possible that this may change something unintended, but that may be accessible via code so it can be reset.


    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  15. #15
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Add a shape to the XAxis in a chart

    Going in a different direction, another approach to adding the bar at the x axis might be to do a Scatter + (stacked?) bar combination chart where you have one or a few data points on the bar chart positioned at the x axis of the scatter chart to give the colored bar. In order to position the bar, you would probably use a date axis on bar's vertical axis and then have a routine that will read the max, min, crosses at properties of the scatter vertical axis to choose the vertical position of the bar. Is that too different from the current approach?

  16. #16
    Registered User
    Join Date
    08-11-2012
    Location
    canada
    MS-Off Ver
    365
    Posts
    14

    Re: Add a shape to the XAxis in a chart

    Wow! Thank you all for all the great ideas. I tried a combination of a few. I increased the x axis thickness and used dummy point for the axis names, then used the copy paste method to copy the format from a sample chart that had the correct coloring. Worked!!
    The result came out different than what I was initially looking for, but I appreciate the expertise that every one has provided. There are so many different ways to do 1 thing!

    Thanks again!

+ 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: 3
    Last Post: 08-17-2013, 05:13 AM
  2. Invert Xaxis and Yaxis
    By excelism in forum Excel General
    Replies: 1
    Last Post: 07-14-2010, 01:32 PM
  3. [SOLVED] Can you create a chart with columns and lines along the xaxis?
    By Nadia in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-24-2006, 07:05 PM
  4. [SOLVED] Chart in a Shape
    By QPapillon in forum Excel General
    Replies: 1
    Last Post: 06-26-2006, 11:20 AM
  5. Replies: 1
    Last Post: 04-04-2006, 03:30 PM
  6. [SOLVED] Thorny problem graphing XAxis dates
    By Don in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 09-22-2005, 12:05 AM
  7. [SOLVED] Bar Chart Date XAxis Thin Bars
    By MrC in forum Excel Charting & Pivots
    Replies: 8
    Last Post: 02-14-2005, 11:06 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