Setting Custom Data Labels for DonutChart

TheTiredEngineer

New Member
Joined
Jul 31, 2024
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Im trying to format my data labels in a neat way to keep the chart from being so cluttered. I want to have some of the labels to the left of the actual chart and some on the right. Also trying to format so that it removes the zero value points and set the ones that are there to a custom number format of
Excel Formula:
"0;;;"

The code works to remove the zero value points but reformatting the good ones is breaking the code. Heres my code:

Excel Formula:
 pieChart.ApplyDataLabels _
    ShowValue:=True, _
    AutoText:=True, _
    LegendKey:=False, _
    HasLeaderLines:=False, _
    ShowSeriesName:=False, _
    ShowCategoryName:=True, _
    ShowPercentage:=False, _
    ShowBubbleSize:=False, _
    NumberFormat:="0;;"
     
'Loop through each data label and text for threshold
  For Each srs In pieChart.SeriesCollection
    For x = 1 To UBound(srs.Values)
      If Abs(srs.Values(x)) = Threshold Then
        srs.Points(x).DataLabel.Delete
      End If
    Next x
  Next srs
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
so first line shall end with:
VBA Code:
  ShowPercentage:=False, _
    ShowBubbleSize:=False

probably your pieChart object is (we can't see the assignment of an object to pieChart in earlier code).
If it is true, then add a line:

VBA Code:
 pieChart.FullSeriesCollection(1).DataLabels.NumberFormat = "0;;"

You may first try in immediate window:
VBA Code:
    ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).DataLabels.NumberFormat = "0;;"
and
VBA Code:
    ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).DataLabels.NumberFormat = "0.0;;"
to make sure this approach works
 
Upvote 0
so first line shall end with:
VBA Code:
  ShowPercentage:=False, _
    ShowBubbleSize:=False

probably your pieChart object is (we can't see the assignment of an object to pieChart in earlier code).
If it is true, then add a line:

VBA Code:
 pieChart.FullSeriesCollection(1).DataLabels.NumberFormat = "0;;"

You may first try in immediate window:
VBA Code:
    ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).DataLabels.NumberFormat = "0;;"
and
VBA Code:
    ActiveSheet.ChartObjects("Chart 1").Chart.FullSeriesCollection(1).DataLabels.NumberFormat = "0.0;;"
to make sure this approach works
OK that worked. But then moving the data labels to left and right, hows that supposed to be done?

Heres what Im trying:
Excel Formula:
pieChart.datalabel.Position = (coords)
 
Upvote 0
As for trying ...
I stronly encourage to look for such details by experimenting with macro recording and analysing where in structure (object model) are given methods/parameters.

Ad rem:

You can use coordinates (case a) or do a relative movement (case b - probably this one would be better suited for you)

like (assuming your pieChart object is a chart, and you want to move label at first segment:

VBA Code:
'case a
With pieChart.FullSeriesCollection(1).Points(1).DataLabel
' coordinates are measured from topleft corner in directions: right and down
    .Left = 100
   .Top = 50
End With

VBA Code:
'case b
With pieChart.FullSeriesCollection(1).Points(1).DataLabel
   .Left = .Left - 10
End With


And extra info - may be will be useful.

swithing off leading lines (swithing on with True):
VBA Code:
pieChart.FullSeriesCollection(1).HasLeaderLines = False


Checking the width (and similar way to check height) of your whole chart:
VBA Code:
Debug.Print pieChart.ChartArea.Width

Here in version displaying results in Immediate Window.
Final comment: I strongly encourage/recommend using this window and breakpoints or line by line (key F8) step execution.
 
Upvote 0
Solution
As for trying ...
I stronly encourage to look for such details by experimenting with macro recording and analysing where in structure (object model) are given methods/parameters.

Ad rem:

You can use coordinates (case a) or do a relative movement (case b - probably this one would be better suited for you)

like (assuming your pieChart object is a chart, and you want to move label at first segment:

VBA Code:
'case a
With pieChart.FullSeriesCollection(1).Points(1).DataLabel
' coordinates are measured from topleft corner in directions: right and down
    .Left = 100
   .Top = 50
End With

VBA Code:
'case b
With pieChart.FullSeriesCollection(1).Points(1).DataLabel
   .Left = .Left - 10
End With


And extra info - may be will be useful.

swithing off leading lines (swithing on with True):
VBA Code:
pieChart.FullSeriesCollection(1).HasLeaderLines = False


Checking the width (and similar way to check height) of your whole chart:
VBA Code:
Debug.Print pieChart.ChartArea.Width

Here in version displaying results in Immediate Window.
Final comment: I strongly encourage/recommend using this window and breakpoints or line by line (key F8) step execution.
good advice about the record macro. Thanks I got it working!
 
Upvote 0
I got it working initially but it broke and datalabels refuse to align to the left. Is there a way to create a textbox and put those datalabels into the textbox to make them easier to align?
 
Upvote 0
As for alignment - double test the sequence of actions and proper calling in objects hiererchy.

Textbox for data labels - well, it is possible, but you will have to take full responsibility for their behaviour, while standard labels are "managed" by the chart itself.
Moreover it is a lot of work to calculate their locations, insert them etc. So my suggestion is to do it standard way.
 
Upvote 0

Forum statistics

Threads
1,221,527
Messages
6,160,342
Members
451,638
Latest member
MyFlower

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top