We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to display Leader Lines

I cannot find anywhere an example how to show Leader Lines connecting DataLabels with chart in Excel chart. Would you please provide a sample code? Thanks


Vlado

Hi Vlado,

Thanks for considering Aspose.

Please use ASeries.HasLeaderLines to set it to true.

May the following code help you for your need:

Sample code:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//Set the name of worksheet
sheet.Name = "Data";
Cells cells = workbook.Worksheets[0].Cells;
//Put a value into a cell
cells["A1"].PutValue("Region");
cells["A2"].PutValue("France");
cells["A3"].PutValue("Germany");
cells["A4"].PutValue("England");
cells["A5"].PutValue("Sweden");
cells["A6"].PutValue("Italy");
cells["A7"].PutValue("Spain");
cells["A8"].PutValue("Portugal");
cells["B1"].PutValue("Sale");
cells["B2"].PutValue(70000);
cells["B3"].PutValue(55000);
cells["B4"].PutValue(30000);
cells["B5"].PutValue(40000);
cells["B6"].PutValue(35000);
cells["B7"].PutValue(32000);
cells["B8"].PutValue(10000);

int sheetIndex = workbook.Worksheets.Add();
sheet = workbook.Worksheets[sheetIndex];
//Set the name of worksheet
sheet.Name = "Chart";
//Create chart
int chartIndex = 0;
chartIndex = sheet.Charts.Add(ChartType.Pie, 1, 1, 25, 10);
Chart chart = sheet.Charts[chartIndex];
//Set properties of chart title
chart.Title.Text = "Sales By Region";
chart.Title.TextFont.Color = Color.Blue;
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Size = 12;
//Set properties of nseries
chart.NSeries.Add("Data!B2:B8", true);
chart.NSeries.CategoryData = "Data!A2:A8";
chart.NSeries.IsColorVaried = true;
//Set the DataLabels in the chart
DataLabels datalabels;

for (int i = 0; i < chart.NSeries.Count; i++)
{
chart.NSeries[i].HasLeaderLines = true;
datalabels = chart.NSeries[i].DataLabels;
datalabels.IsCategoryNameShown = true;
datalabels.IsValueShown = true;
datalabels.IsPercentageShown = true;
datalabels.IsLegendKeyShown = true;

}

//Save the excel file
workbook.Save("f:\\test\\out_pie_chrt.xls");

Thank you.

Amjad,

thank you for the code. I am not able to see the lines after I put them there. The reason is the labels are very close to the chart. When I move the labels manually out of the chart areal the lines appear there. How to do that from the code?

See the attached chart - US Equity label was moved just a little bit to left manually .

Hi,

Ms Excel only diplay the leader line when ASeries.HasLeaderLines is true and MS Excel think the leader line is needed. You can check the option is setted in MS Excel (select the series ->"Format Data Series"->"Data Labels" -> "Show Leader Line")

Now we do not support to set the position of the DataLabels now. We are looking into the position of the DataLabels in Charts2Image. It's related to the default position and pie radius in pie chart.