Hi,
I am trying to format chart data points dynamically. Some times as percent with one decimal point or numbers with one decimal point.
I am using the data point CustomNumberFormat property by setting it to “0.0%”
This works when the files is saved as PPTX, but it does not when saved to HTML.
When saved to HTML the data points display as numbers with no percent sign.SligleSlideScatter.zip (154.8 KB)
I am attaching the sample input slide and the test code.
public void TestNumberFormats()
{
string sosurcePath = @"C:\temp\SligleSlideScatter.pptx";
string destinationPath = @"C:\temp\SligleSlideScatterOutput.pptx";
string destinationHtmlPath = @"C:\temp\SligleSlideScatterOutput.html";
Presentation p = new Presentation(sosurcePath);
IChart chart = p.Slides[0].Shapes[0] as IChart;
foreach(IChartSeries ser in chart.ChartData.Series)
{
foreach(IChartDataPoint dp in ser.DataPoints)
{
if (dp.Value != null && dp.Value.AsCell != null)
{
dp.Value.AsCell.CustomNumberFormat = "0.0%";
System.Diagnostics.Debug.Print(@"dp value custom number format {0}", dp.Value.AsCell.CustomNumberFormat);
System.Diagnostics.Debug.Print(@"dp value preset number format {0}", dp.Value.AsCell.PresetNumberFormat);
}
if (dp.XValue != null && dp.XValue.AsCell != null)
{
dp.XValue.AsCell.CustomNumberFormat = "0.0%";
System.Diagnostics.Debug.Print(@"dp Xvalue custom number format {0}", dp.XValue.AsCell.CustomNumberFormat);
System.Diagnostics.Debug.Print(@"dp Xvalue preset number format {0}", dp.XValue.AsCell.PresetNumberFormat);
}
if (dp.YValue != null && dp.YValue.AsCell != null)
{
dp.YValue.AsCell.CustomNumberFormat = "0.0%";
System.Diagnostics.Debug.Print(@"dp Yvalue custom number format {0}", dp.YValue.AsCell.CustomNumberFormat);
System.Diagnostics.Debug.Print(@"dp Yvalue preset number format {0}", dp.YValue.AsCell.PresetNumberFormat);
}
}
}
p.Save(destinationPath, SaveFormat.Pptx);
for (int i = 0; i < p.Slides.Count; i++)
{
using (MemoryStream stream = new MemoryStream())
{
p.Save(stream, new[] { i + 1 }, SaveFormat.Html);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
var html = reader.ReadToEnd();
File.WriteAllText(destinationHtmlPath, html);
}
}
}
}
Thank you.