Hi there, I am trying to generate the Pie chart with leader lines. But I couldn’t able to do it with Aspose Cells. Is it possible with Aspose Cells? I have attached a sample pie chart which is the format I was looking with Aspose Cells product. Can you help me out?
Hi,
Hi Srinu,
Thanks for your posting and using Aspose.Cells.
In order to show Leader Lines, you will have to move Pie Chart Data Labels little. It is the Excel behavior. If you will not change the position little bit, it will not show the leader lines no matter how much you shrink or expand the Pie Chart.
We have created a sample Pie Chart with the following sample code. Please read its comments which will help you to understand the code fully. I have attached the output Pie Chart image and output Excel file created by it for your reference.
C#
//Create workbook in XLSX format to get good look of chart
Workbook workbook = new Workbook(FileFormatType.Xlsx);
//Set the default font of the workbook
Style style = workbook.DefaultStyle;
style.Font.Name = “Calibri”;
style.Font.Size = 10;
workbook.DefaultStyle = style;
//Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Add sample data
worksheet.Cells[“A1”].PutValue(“Retail”);
worksheet.Cells[“A2”].PutValue(“Services”);
worksheet.Cells[“A3”].PutValue(“Info & Communication”);
worksheet.Cells[“A4”].PutValue(“Transport Equip”);
worksheet.Cells[“A5”].PutValue(“Construction”);
worksheet.Cells[“A6”].PutValue(“Other Products”);
worksheet.Cells[“A7”].PutValue(“Wholesale”);
worksheet.Cells[“A8”].PutValue(“Land Transport”);
worksheet.Cells[“A9”].PutValue(“Air Transport”);
worksheet.Cells[“A10”].PutValue(“Electric Appliances”);
worksheet.Cells[“A11”].PutValue(“Securities”);
worksheet.Cells[“A12”].PutValue(“Textiles & Apparel”);
worksheet.Cells[“A13”].PutValue(“Machinery”);
worksheet.Cells[“A14”].PutValue(“Metal Products”);
worksheet.Cells[“A15”].PutValue(“Cash”);
worksheet.Cells[“A16”].PutValue(“Banks”);
worksheet.Cells[“B1”].PutValue(10.4);
worksheet.Cells[“B2”].PutValue(5.2);
worksheet.Cells[“B3”].PutValue(6.4);
worksheet.Cells[“B4”].PutValue(10.4);
worksheet.Cells[“B5”].PutValue(7.9);
worksheet.Cells[“B6”].PutValue(4.1);
worksheet.Cells[“B7”].PutValue(3.5);
worksheet.Cells[“B8”].PutValue(5.7);
worksheet.Cells[“B9”].PutValue(3);
worksheet.Cells[“B10”].PutValue(14.7);
worksheet.Cells[“B11”].PutValue(3.6);
worksheet.Cells[“B12”].PutValue(2.8);
worksheet.Cells[“B13”].PutValue(7.8);
worksheet.Cells[“B14”].PutValue(2.4);
worksheet.Cells[“B15”].PutValue(1.8);
worksheet.Cells[“B16”].PutValue(10.1);
//Create Pie Chart
int id = worksheet.Charts.Add(ChartType.Pie, 3, 3, 23, 13);
Chart chart = worksheet.Charts[id];
chart.NSeries.Add(“B1:B16”, true);
chart.NSeries.CategoryData = “A1:A16”;
chart.ShowLegend = false;
DataLabels dataLabels = chart.NSeries[0].DataLabels;
dataLabels.ShowCategoryName = true;
dataLabels.ShowPercentage = true;
dataLabels.Position = LabelPositionType.OutsideEnd;
dataLabels.Separator = DataLablesSeparatorType.Comma;
chart.NSeries[0].HasLeaderLines = 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 < chart.NSeries[0].Points.Count; i++)
{
int X = chart.NSeries[0].Points[i].DataLabels.X;
Debug.WriteLine(X);
//If it is greater than 2000, then move the X position rightward
//otherwise move the X position leftward
if (X > 2000)
chart.NSeries[0].Points[i].DataLabels.X = X + DELTA;
else
chart.NSeries[0].Points[i].DataLabels.X = X - DELTA;
}
//Save the chart image
Aspose.Cells.Rendering.ImageOrPrintOptions anOption = new Aspose.Cells.Rendering.ImageOrPrintOptions();
anOption.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
anOption.HorizontalResolution = 200;
anOption.VerticalResolution = 200;
chart.ToImage(“output.png”, anOption);
//Also save the workbook to see chart inside the Excel
workbook.Save(“output.xlsx”);
Hi Shakeel Faiz,
Thank you very much. This is what I was looking for. I will update you back in case if I have any questions…
Hi Srinu,
Thanks for your posting and using Aspose.Cells.
Sure, please feel free to ask us if you encounter any issue or get some questions relating to Aspose.Cells. We will be glad to look into it and help you further.