I am creating an Excel export that contains several pie charts which are dynamically created based on data from a database that is dumped into a range of cells within the spreadsheet. I need the data labels for the pie charts to be on the outside end of the pie chart. The data labels by default are set to best fit.
I have tried to manipulate the data label positions with eLabelPosition
which has an OutEnd
property but have had no luck. Doing this causes the data labels to completely disappear.
Below is the method I am using to generate my pie charts.
private void AddPieChart(ExcelWorksheet worksheet, int firstDataRow, int firstDataColumn, int lastDataRow, int nextColumn, int firstColumn)
{
ExcelPieChart pieChart = worksheet.Drawings.AddChart(worksheet.Name.ToString() + " Pie Chart", eChartType.Pie3D) as ExcelPieChart;
var serie = pieChart.Series.Add(ExcelCellBase.GetAddress(firstDataRow + 3, firstDataColumn, lastDataRow + 3, firstDataColumn),
ExcelCellBase.GetAddress(firstDataRow + 3, firstColumn, lastDataRow + 3, firstColumn));
pieChart.DataLabel.ShowCategory = true;
pieChart.DataLabel.ShowLeaderLines = true;
pieChart.DataLabel.Font.Bold = true;
pieChart.DataLabel.Font.Size = 12;
pieChart.Legend.Remove();
pieChart.SetSize(500, 350);
pieChart.SetPosition(lastDataRow + 5, 0, nextColumn - 1, 0);
pieChart.Border.Fill.Color = Color.White;
pieChart.Series.Chart.RoundedCorners = false;
var pieSerie = (ExcelPieChartSerie)serie;
pieSerie.DataLabel.Position = eLabelPosition.OutEnd;
}
It appears that if you have DataLabel
properties assigned to your ExcelPieChart
and then also attempt to assign the DataLabel
properties on the ExcelPieChartSerie
is does not apply the property to the ExcelPieChartSerie
. In order to fix this remove any of the DataLabel
properties on the ExcelPieChart
and instead add them to the ExcelPieChartSerie
as shown below.
private void AddPieChart(ExcelWorksheet worksheet, int firstDataRow, int firstDataColumn, int lastDataRow, int nextColumn, int firstColumn)
{
ExcelPieChart pieChart = worksheet.Drawings.AddChart(worksheet.Name.ToString() + " Pie Chart", eChartType.Pie3D) as ExcelPieChart;
var serie = pieChart.Series.Add(ExcelCellBase.GetAddress(firstDataRow + 3, firstDataColumn, lastDataRow + 3, firstDataColumn),
ExcelCellBase.GetAddress(firstDataRow + 3, firstColumn, lastDataRow + 3, firstColumn));
pieChart.Legend.Remove();
pieChart.SetSize(500, 350);
pieChart.SetPosition(lastDataRow + 5, 0, nextColumn - 1, 0);
pieChart.Border.Fill.Color = Color.White;
pieChart.Series.Chart.RoundedCorners = false;
var pieSerie = (ExcelPieChartSerie)serie;
pieSerie.DataLabel.ShowCategory = true;
pieSerie.DataLabel.ShowLeaderLines = true;
pieSerie.DataLabel.Font.Bold = true;
pieSerie.DataLabel.Font.Size = 12;
pieSerie.DataLabel.Position = eLabelPosition.OutEnd;
}