How to make data label position appear on the outside of chart for Doughnut charts

How does one set the data label position to outside for Doughnut charts? all setting appear to be used for pie charts.


See 2nd chart for example of desired result.


Hi,

Thanks for your posting, screenshots and using Aspose.Cells.

Please try this code. It should fix your issue. Let us know your feedback.

C#
ch.NSeries[0].DataLabels.Position = LabelPositionType.OutsideEnd;

No effect. As your documentation states, the property setting description says that it only applies to bar and 2d/3d pie charts. See attachment.

Hi,

Thanks for your feedback and using Aspose.Cells.

Please create your desired excel file with the chart manually using Microsoft Excel and post it here for our further investigation. We will load your excel file and observe various properties that needs to be set to get the desired results and provide you a sample code to achieve this. Thanks for your cooperation.

I explained my desired results. What else do you need?

Hi,

Thanks for using Aspose.Cells.

We have looked into your issue further and found that doughnet chart data labels cannot be positioned outside using Microsoft Excel. If something is not possible with Microsoft Excel, it will automatically be not possible with Aspose.Cells. I have also attached the screenshot highlighting my point for your reference.

Bummer, but good to know that’s not possible in Excel. Thank you for your time.

Hi,

I have evaluated your requirements a bit.

Well, you may devise custom approach as a workaround to accomplish your task. I have written the following sample code to accomplish your task, kindly refer to it and you may add/update your code accordingly for your requirements. The code uses the template file (attached) which is created by following the approach mentioned here for your desired chart’s data source and creates your desired chart separately by Aspose.Cells APIs in the same sheet in the workbook.

e.g

Sample code:

[C#]

Workbook workbook = new Workbook(“e:\test2\Donut+With+Leagend+Lines.xlsx”);

Worksheet sheet = workbook.Worksheets[0];

int chartIndex = 0;

chartIndex = sheet.Charts.Add(ChartType.Doughnut, 1, 1, 25, 10);

Chart chart = sheet.Charts[chartIndex];

//Set properties of chart

chart.PlotArea.Area.Formatting = FormattingType.None; 

chart.PlotArea.Border.IsVisible = false;

//Set properties of nseries

chart.NSeries.Add(“B2:B9”, true);

chart.NSeries.CategoryData = “A2:A9”;

chart.NSeries.IsColorVaried = true;

//Set the ChartArea

ChartArea chartarea = chart.ChartArea;

//Show legend 

chart.ShowLegend = true;

//Insert another series of Pie chart type.

int index = chart.NSeries.Add(“B2:B9”, true);

Series series = chart.NSeries[index];

series.Type = ChartType.Pie;

series.HasLeaderLines = true;

DataLabels datalabels = series.DataLabels;

datalabels.ShowPercentage = true;

datalabels.Position = LabelPositionType.OutsideEnd;

series.Area.Formatting = FormattingType.None;

//Remove its legent entry (if you want).

series.LegendEntry.IsDeleted = true;

//You need to move DataLabels a little leftward or rightward depending on their position

//to show leader lines

chart.Calculate();

int DELTA = 100;

for (int i = 0; i < series.Points.Count; i++)

{

int X = series.Points[i].DataLabels.X;

Debug.WriteLine(X);

//e.g if it is greater than 2000, then move the X position rightward

//otherwise move the X position leftward

if (X > 2000)

series.Points[i].DataLabels.X = X + DELTA;

else

series.Points[i].DataLabels.X = X - DELTA;

}

//Save the excel file

workbook.Save(“e:\test2\doughnut_chart1.xlsx”);

[VB]

Dim workbook As New Workbook(“e:\test2\Donut+With+Leagend+Lines.xlsx”)

Dim sheet As Worksheet = workbook.Worksheets(0)

Dim chartIndex As Integer = 0

chartIndex = sheet.Charts.Add(ChartType.Doughnut, 1, 1, 25, 10)

Dim chart As Chart = sheet.Charts(chartIndex)

'Set properties of chart

chart.PlotArea.Area.Formatting = FormattingType.None

chart.PlotArea.Border.IsVisible = False

'Set properties of nseries

chart.NSeries.Add(“B2:B9”, True)

chart.NSeries.CategoryData = “A2:A9”

chart.NSeries.IsColorVaried = True

'Set the ChartArea

Dim chartarea As ChartArea = chart.ChartArea

'Show legend 

chart.ShowLegend = True

'Insert another series of Pie chart type.

Dim index As Integer = chart.NSeries.Add(“B2:B9”, True)

Dim series As Series = chart.NSeries(index)

series.Type = ChartType.Pie

series.HasLeaderLines = True

Dim datalabels As DataLabels = series.DataLabels

datalabels.ShowPercentage = True

datalabels.Position = LabelPositionType.OutsideEnd

series.Area.Formatting = FormattingType.None

'Remove its legent entry (if you want).

series.LegendEntry.IsDeleted = True

'You need to move DataLabels a little leftward or rightward depending on their position

'to show leader lines

chart.Calculate()

Dim DELTA As Integer = 100

For i As Integer = 0 To series.Points.Count - 1

Dim X As Integer = series.Points(i).DataLabels.X

Debug.WriteLine(X)

'e.g if it is greater than 2000, then move the X position rightward

'otherwise move the X position leftward

If X > 2000 Then

series.Points(i).DataLabels.X = X + DELTA

Else

series.Points(i).DataLabels.X = X - DELTA

End If

Next i

'Save the excel file

workbook.Save(“e:\test2\doughnut_chart1.xlsx”)

Hope, this helps you a bit.

Thank you.

Thank you Amjad…This helped provide me with some ideas to accomplish what I needed to for my requirements. Much appreciated!

Hi,


Good to know that it helps you to accomplish your task. Feel free to write back if you have further comments or questions, we will be happy to assist you soon.

Thank you.