Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1540
  • Last Modified:

Display all labels outside of pie chart with leading lines

I am generating a pie chart using vba from MSAccess.  With the best fit property setting, most of the labels are displayed outside of the pie chart.  However, we still have to manually drag out those labels that are short enough to fit within the pie slice.  

I would like to know how to iterate through each slice and determine which of the labels needs to be dragged away from the slice. Most importantly, I need to clearly display the leading lines connecting the labels to their respective slices.

Thanks.
0
yvroger
Asked:
yvroger
  • 3
  • 3
2 Solutions
 
Rgonzo1971Commented:
Hi,

Instead of Best fit have tried Outside End

this code drag the labels by 2 points towards the outside so that you can see the lines

Sub macro()
ActiveChart.SeriesCollection(1).HasLeaderLines = True
ActiveChart.SeriesCollection(1).DataLabels.Position = xlLabelPositionOutsideEnd
Set chtArea = ActiveChart.ChartArea
MiddleOfAreaH = chtArea.Top + (chtArea.Height / 2)
MiddleOfAreaW = chtArea.Left + (chtArea.Width / 2)
For Each dtalbl In ActiveChart.SeriesCollection(1).DataLabels
If dtalbl.Top + (dtalbl.Height / 2) + chtArea.Top < MiddleOfAreaH Then
    dtalbl.Top = dtalbl.Top - 2
Else
    dtalbl.Top = dtalbl.Top + 2
End If

If dtalbl.Left + (dtalbl.Width / 2) + chtArea.Left < MiddleOfAreaW Then
    dtalbl.Left = dtalbl.Left - 2
Else
    dtalbl.Left = dtalbl.Left + 2
End If
Next
End Sub

Open in new window


Regards
0
 
yvrogerAuthor Commented:
Rgonzo, your solution moved me in the right direction.  However, the height property in "dtalbl.Height" does not seem to work.  I get the error: "object doesn't support this property or method".

Please assist.  Thanks.
0
 
Rgonzo1971Commented:
Hi,

Could you send a dummy example?

Regards
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
yvrogerAuthor Commented:
Before I come up with a dummy example let me ask you a question.

Weren't trying to use chtarea.Height and chtarea.Width instead of dtalbl.Height and chtarea.Width respectively?

For example the expression "If dtalbl.Top + (dtalbl.Height / 2) + chtArea.Top < MiddleOfAreaH" should have been "If dtalbl.Top + (chtarea.Height / 2) + chtArea.Top < MiddleOfAreaH", correct?
0
 
yvrogerAuthor Commented:
Is there a way to determine the angle of the data label from the chart axis?
0
 
Rgonzo1971Commented:
Hi,

Chart area comprises the pie chart as well as the labels that's why you cannot have a precise angle for the labels. (Maybe you could but it would be really complicated)

I calculate the labels middle because if they a relatively large or high the direction to where I want to move them will be different

Regards
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now