+ Reply to Thread
Results 1 to 5 of 5

Data Labels overlapping in XY Scatter chart

  1. #1
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    427

    Data Labels overlapping in XY Scatter chart

    Hi Team,

    Does anyone know of any way (VBA?) to automatically prevent data labels from overlapping in a XY Scatter chart - it's proper doing my noggin' in!!

    Terry

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

    Re: Data Labels overlapping in XY Scatter chart

    Short but useless answer -- I am optimistic that this can be done. Can you provide a sampling of what you have tried? Some sense of your skill level with VBA and Excel's object model?

    I have never tried to do it, but it seems like it should be as "simple" as:

    1) Loop through the DataLabel objects (https://msdn.microsoft.com/en-us/vba...l-object-excel ) and read the .Top, .Left, .Height, and .Width property values.
    2) From these properties, determine which DataLabels overlap. I put something like "rectangle overlap algorithm" into my favorite internet search engine and found several pages (all for other programming languages, but the principles should work in VBA). These two, for example:
    http://codesam.blogspot.com/2011/02/...intersect.html
    https://flylib.com/books/en/4.11.1.51/1/
    3) Determine where to place the overlapping DataLabels and come up with new values for the Top, Left, Height, Width properties. I did not spend a lot of effort trying to find this, but I did find references to a "rectangle packing problem" that seems to be more than what you want (and seems similar to the knapsack problem). This might just need to be trial and error, or maybe some judicious use of geometry to identify how best to move the rectangle(s) so they don't overlap (without moving too far from the data point).
    4) Assign those new values to the Top, Left, Height, Width properties.

    If I were developing this from my present level of skill and knowledge, I would start with steps 1 and 4 and get comfortable with the "grid" that Excel/VBA uses to position objects on a chart. Then spend some time on step 2 to see how well I can identify overlapping DataLabels (rectangles). By then, I hopefully have a better idea of how to choose where to move the DataLabels (rectangles) so they don't overlap.

    How much of this do you need help with?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

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

    Re: Data Labels overlapping in XY Scatter chart

    As MrShorty suggests it is possible to create some algorithm to determine overlap and move labels.

    I have yet to come up with a code that works 100%. Most over lapping is caused by dense data and long labels.
    Before you write any code I would suggest the following
    adjust scales to spread points as much as possible
    reduce text length of label
    reduce font size of label
    create a supplementary legend for text replacing long text with alpha code

    One of the down sides of moving the labels around is it can become confusing as to which label matches with which point. Latest charts do along for leader lines but that just adds more clutter to the chart.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    427

    Re: Data Labels overlapping in XY Scatter chart

    Hi Mr Shorty,

    I understand every word you've said. However, collectively, it starts to confuse me ...

    I've done multiple searches etc. looking for people who have had the same issues. There seems to be solutions for pie charts and bar charts, but little in the way of solutions for xy scatter charts.

    With regard to my comfort with VBA, think of it as the 80/20 principle. I probably know 20% of it (and that's being very generous) and it satisfies 80% of my needs. Every now and then I come across something like this and my mind gets blown. I then try to find solutions on the internet and try to apply them to my problem (that has worked in the past). If I solve it, I try to analyse what I did and how it worked and then try to apply that new found knowledge when I need it again. (I also, try to contribute to this forum, all too often, by the time I've read the question and worked out a possible answer, it's marked as solved!).

    I guess I'm looking for example code that I may be able to tweak to suit my needs.

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

    Re: Data Labels overlapping in XY Scatter chart

    I understand every word you've said. However, collectively, it starts to confuse me ...
    This is part of why I broke this task into four steps. I find that, when a large programming task seems to confuse or overwhelm me, that I need to "divide and conquer" the problem by breaking it down into what I hope are manageable sub-problems. In this case, seeing the problem from my own 20/80 skill set, I expect that I could figure out those four steps if I spent enough time/effort on each of them. Which of my four steps do you have trouble with? Or can you do all four, but cannot see how to combine those steps to solve the problem?

    I guess I'm looking for example code that I may be able to tweak to suit my needs.
    I haven't seen any, and if someone with Andy Pope's skill set does not feel like he can offer a 100% reliable solution, I am not sure if you will just find something to tweak. I would be curious what solutions you have found for bar and pie charts that you don't feel is adaptable to a scatter chart. The help file for the DataLabels object does not describe any differences in a DataLabel object based on chart type. I would expect the mechanics of moving a DataLabel object in a scatter chart to be basically the same as moving it on a bar/pie chart. The "tweaks" would be in deciding which direction and how far to move the object (which could very well be a very significant tweak).

    Sometimes the problem with programming in a dead/dying language (like VBA) is that you may need to look into other languages. If you can look at other programming languages, I would not be surprised if you can find examples in other languages (a game programmer, for example, who wants to know how to make sure his players' characters and any NPCs never occupy the same spot on the screen, maybe). My few internet searches suggests that this is not a new computer programming problem. Searching out how other programmers in other languages have solved it may provide insights into how the problem could be solved using VBA.

+ 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. Scatter Graph - Overlapping Data Labels
    By welshlion26 in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 01-17-2020, 06:50 AM
  2. [SOLVED] Use data labels in legend with X Y scatter chart
    By CostCare in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2016, 03:10 AM
  3. Using VBA to move data labels positions in a scatter chart
    By lafer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2014, 04:31 AM
  4. Macro to add labels to data points in an xy scatter chart
    By Scott Wagner in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 12-16-2005, 09:50 PM
  5. Data point labels in XY scatter chart
    By TonyGB007 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 12-09-2005, 05:20 AM
  6. Data labels on scatter chart
    By rmellison in forum Excel General
    Replies: 3
    Last Post: 10-18-2005, 11:05 AM
  7. [SOLVED] Excel 2000 Scatter Chart cannot use data labels
    By Tom in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 04-06-2005, 02:06 PM

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