I have some chart in a power point template. These charts have data-labels that are formatted "#.##0", to achieve thousand separation on numbers.
This formatting disappears once i insert data into the chart. Therefore, i have saved the data-labels before i edit the data, and then reinsert the formatting after I'm done editing data:
Saving before formatting is lost:
- Code: Select all
private static List<SeriesInformation> SaveSeriesFormatting(IChart iChart)
{
var seriesFormats = new List<SeriesInformation>();
foreach (ChartSeriesDataFormat serie in iChart?.Series)
{
seriesFormats.Add(
new SeriesInformation(
serie.Fill.FillType,
serie.Fill.Pattern.BackgroundColor.Color,
serie.Fill.Pattern.ForegroundColor.Color,
serie.Fill.Pattern.PatternType,
serie.DataPoints,
serie.DataLabels,
serie.Distance));
}
return seriesFormats;
}
Inserting formatting after data has been edited:
- Code: Select all
private static void ApplySeriesFormatting(List<SeriesInformation> seriesFormats, IChart chart)
{
var defaultSeriesFormat = seriesFormats[0];
for (int index = 0; index < chart.Series.Count; index++)
{
var seriesFormat = index < seriesFormats.Count ? seriesFormats[index] : defaultSeriesFormat;
// explode pie charts
chart.Series[index].Distance = seriesFormat.Distance;
// reinsert datalabels
var dataLabel = seriesFormat.DataLabels;
var chartDataLabel = chart.Series[index].DataLabels;
chartDataLabel.Fill.FillType = dataLabel.Fill.FillType;
chartDataLabel.Fill.SolidColor.Color = dataLabel.Fill.SolidColor.Color;
chartDataLabel.TextProperties.Paragraphs[0].DefaultCharacterProperties.Format.FontHeight =
dataLabel.TextProperties.Paragraphs[0].DefaultCharacterProperties.Format.FontHeight;
chartDataLabel.CategoryNameVisible = dataLabel.CategoryNameVisible;
chartDataLabel.BubbleSizeVisible = dataLabel.BubbleSizeVisible;
chartDataLabel.LabelValueVisible = dataLabel.LabelValueVisible;
chartDataLabel.LegendKeyVisible = dataLabel.LegendKeyVisible;
chartDataLabel.PercentValueVisible = dataLabel.PercentValueVisible;
chartDataLabel.SeriesNameVisible = dataLabel.SeriesNameVisible;
chartDataLabel.LeaderLinesVisible = dataLabel.LeaderLinesVisible;
chartDataLabel.NumberFormat = dataLabel.NumberFormat;
if ((int) dataLabel.Position != -1) chartDataLabel.Position = dataLabel.Position;
}
}
This code works except for number formatting. I can see that the actual number formatting that i want is saved and applied in these two methods, but when i look in the power point, the numbers are not formatted.