I am displaying a slide with multiple pie charts. The number of categories for each chart is variable. I need to be able to format the legend position so that it does not resize the pie charts depending on the number of categories. See the issue below.
I have tried excelChart.Legend.AllowOverlap = true; and it does not resize the charts, but it places the legends on top of the chart. There does not seem like there’s any positioning of the legend via the API.
How can I render each chart and maintain consistent size no matter the number of categories?
First, one thing I’m not clear about is that in your screenshot the charts have the same top position, but different bottom position.
I’m presuming that this is not the problem you are referring to, but nevertheless, can you send us your Excel file so that we can investigate it?
Anyway, if I understood correctly, you want to have the charts with different sizes but with the same plot area.
In other words, the drawing of a pie chart would be the same size but the legend would take more or less space.
Is that correct?
Unfortunately, there is no such option.
Only the entire chart’s size is constant, the plot area is affected by axes and legends.
The only suggestion I have for you is to create charts without a legend and then write the legend yourself in the cells below the chart.
They have a different bottom position due to the legend changing the size of the pie chart.
Yes, you are mostly correct with your understanding. Drawing of the plot area would be the same size and legends would present below them unscaled (overflow is ok, because there is plenty of room underneath). I thought your workaround would be the solution.
How do I access the pie chart colors to separately create the legend key colors?
var workbook = ExcelFile.Load("input.xlsx");
var worksheet = workbook.Worksheets[0];
var chart = worksheet.Charts[0];
int r = chart.Position.To.Row.Index;
int c = chart.Position.From.Column.Index;
foreach (var series in chart.Series)
{
++r;
worksheet.Cells[r, c].Value = "■ " + series.DisplayName;
DrawingColor color = DrawingColor.FromName(DrawingColorName.Black);
if (series.Fill.FillType == FillFormatType.Solid)
color = ((SolidFillFormat)series.Fill).Color;
else if (series.Outline.Fill.FillType == FillFormatType.Solid)
color = ((SolidFillFormat)series.Outline.Fill).Color;
worksheet.Cells[r, c].GetCharacters(0, 1).Font.Color = SpreadsheetColor.FromArgb(color.Argb);
}
workbook.Save("output.xlsx");