I have the following code which attempts to add a datalabel to a point in a combined donut/pie-chart:
For Each co In .ChartObjects With co.Chart.FullSeriesCollection("Grøn pil").Points(2) .HasDataLabel = True With .DataLabel .Position = xlLabelPositionOutsideEnd .Format.AutoShapeType = msoShapeRectangle .Format.Line.Visible = msoTrue End With End With Next co
However, the code aborts on the line
.Position = xlLabelPositionOutsideEnd with the error message “Run-time error 2147467259 (80004005)”. Method ‘Position’ of object ‘DataLabel’ failed”.
Looking at the chart, the label has been added, but it is still positioned inside the chart.
As you can see I’ve already positioned a label outside the chart for a different series, which is represented as a pie chart. While the series I am trying to add the label to is represented as a donut-chart.
Can’t I have both the labels for the donut- and pie-chart on the outside? Isn’t
xlLabelPositionOutsideEnd a valid position for labels of a donut-chart? Or is the problem something else which eludes me?
Any help would be greatly appreciated!
I don’t think it’s possible to do exactly you want to do the way you want to do it! The option to place the labels outside the chart is not available on the doughnut chart options:
like they do on a pie chart:
However, you could perform a trick using a pie chart and a white circle to make it look like a doughnut by doing the following:
Sub AddCircle() 'Get chart size and position: Dim CH01 As Chart: Set CH01 = ThisWorkbook.Sheets("Sheet1").ChartObjects("Chart1").Chart Dim OB01 As ChartObject: Set OB01 = CH01.Parent Dim x As Double: x = 0 'horizontal coordinate Dim y As Double: y = 0 'vertical coordinate Dim w As Double: w = 0 'width Dim h As Double: h = 0 'height x = OB01.Left y = OB01.Top w = OB01.Width h = OB01.Height 'Adding the circle: ThisWorkbook.Sheets("Sheet1").Shapes.AddShape(msoShapeOval, x + w / 2 - 20, y + h / 2 - 20, 40, 40).Name = "Circle01" 'Formatting the circle: With ThisWorkbook.Sheets("Sheet1").Shapes("Circle01") .LINE.Visible = msoFalse .Fill.ForeColor.RGB = RGB(255, 255, 255) End With End Sub
And it works very nicely:
Had some fun “solving” this one…
Working with sinus and cosinus we can also calculate the outside position of the label. Following a VB snippet, how this can be done:
Sub Macro1() Dim cx Dim cy Dim x Dim y Dim radius Dim angle Dim new_radius Dim new_x Dim new_y ActiveSheet.ChartObjects("Chart 1").Activate ActiveSheet.ChartObjects("Chart 1").Select cx = Selection.width / 2 cy = Selection.height / 2 For i = 1 To ActiveChart.FullSeriesCollection(1).Points.Count Step 1 ActiveChart.FullSeriesCollection(1).Points(i).DataLabel.Select x = Selection.left + (Selection.width / 2) y = Selection.top + (Selection.height / 2) radius = Sqr(((x - cx) ^ 2) + ((y - cy) ^ 2)) angle = WorksheetFunction.Atan2(y - cy, x - cx) new_radius = radius + 40 new_x = cx + (Sin(angle) * new_radius) new_y = cy + (Cos(angle) * new_radius) Selection.left = new_x - (Selection.width / 2) Selection.top = new_y - (Selection.height / 2) Next i End Sub