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
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
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?
Originally Posted by shg
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.
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.
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 understand every word you've said. However, collectively, it starts to confuse me ...
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).I guess I'm looking for example code that I may be able to tweak to suit my needs.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks