Hello,
I have the following chart that I need to convert to an image:
Using the ConvertToImage PresentationRenderer method results in this image with the 100% label cut-off:
How can I fix this? I'm using the Syncfusion .Net Core Presentation and PresentationRenderer packages.
Thank you!
//Creates a Presentation instance
IPresentation pptxDoc = Presentation.Create();
// Create an instance of Presentation Renderer for converting chart to image
pptxDoc.PresentationRenderer = new PresentationRenderer();
//Adds a blank slide to the Presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Adds chart to the slide with position and size
IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500);
//Specifies the chart title
chart.ChartTitle = "Sales Analysis";
//Sets chart data - Row1
chart.ChartData.SetValue(1, 2, "Jan");
chart.ChartData.SetValue(1, 3, "Feb");
chart.ChartData.SetValue(1, 4, "March");
//Sets chart data - Row2
chart.ChartData.SetValue(2, 1, 2010);
chart.ChartData.SetValue(2, 2, 600);
chart.ChartData.SetValue(2, 3, 700);
chart.ChartData.SetValue(2, 4, 800);
//Sets chart data - Row3
chart.ChartData.SetValue(3, 1, 2011);
chart.ChartData.SetValue(3, 2, 800);
chart.ChartData.SetValue(3, 3, 700);
chart.ChartData.SetValue(3, 4, 600);
//Sets chart data - Row4
chart.ChartData.SetValue(4, 1, 2012);
chart.ChartData.SetValue(4, 2, 600);
chart.ChartData.SetValue(4, 3, 700);
chart.ChartData.SetValue(4, 4, 800);
//Sets the data range of the category axis
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 4, 1];
//Creates a new chart series with the name
IOfficeChartSerie seriesJan = chart.Series.Add("Jan");
//Sets the data range of chart series – start row, start column, end row, end column
seriesJan.Values = chart.ChartData[2, 2, 4, 2];
//Creates a new chart series with the name
IOfficeChartSerie seriesFeb = chart.Series.Add("Feb");
//Sets the data range of chart series – start row, start column, end row, end column
seriesFeb.Values = chart.ChartData[2, 3, 4, 3];
//Creates a new chart series with the name
IOfficeChartSerie seriesMarch = chart.Series.Add("March");
//Sets the data range of chart series – start row, start column, end row, end column
seriesMarch.Values = chart.ChartData[2, 4, 4, 4];
//Specifies the chart type
chart.ChartType = OfficeChartType.Bar_Stacked_100;
using (var stream = new MemoryStream())
{
pptxDoc.PresentationRenderer.ConvertToImage(chart, stream);
slide.Shapes.AddPicture(stream, 100, 10, 700, 500);
}
//Save the PowerPoint file
pptxDoc.Save("Output.pptx");
//Close the PowerPoint instance
pptxDoc.Close();
This generates the following image with the cut off last axis label
I'm using the latest Syncfusion.Presentation.Net.Core and Syncfusion.PresentationRenderer.Net.Core nuget package versions (v19.1.0.57)
IPresentation pptxDoc = Presentation.Create();
// Create an instance of Presentation Renderer for converting chart to image
pptxDoc.PresentationRenderer = new PresentationRenderer();
//Adds a blank slide to the Presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Adds chart to the slide with position and size
IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500);
//Specifies the chart title
chart.ChartTitle = "Sales Analysis";
//Sets chart data - Row1
chart.ChartData.SetValue(1, 2, "Jan");
chart.ChartData.SetValue(1, 3, "Feb");
chart.ChartData.SetValue(1, 4, "March");
//Sets chart data - Row2
chart.ChartData.SetValue(2, 1, 2010);
chart.ChartData.SetValue(2, 2, 600);
chart.ChartData.SetValue(2, 3, 700);
chart.ChartData.SetValue(2, 4, 800);
//Sets chart data - Row3
chart.ChartData.SetValue(3, 1, 2011);
chart.ChartData.SetValue(3, 2, 800);
chart.ChartData.SetValue(3, 3, 700);
chart.ChartData.SetValue(3, 4, 600);
//Sets chart data - Row4
chart.ChartData.SetValue(4, 1, 2012);
chart.ChartData.SetValue(4, 2, 600);
chart.ChartData.SetValue(4, 3, 700);
chart.ChartData.SetValue(4, 4, 800);
//Sets the data range of the category axis
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 4, 1];
chart.PrimaryValueAxis.IsAutoMax = false;
chart.PrimaryValueAxis.MaximumValue = 1.01;
//Creates a new chart series with the name
IOfficeChartSerie seriesJan = chart.Series.Add("Jan");
//Sets the data range of chart series – start row, start column, end row, end column
seriesJan.Values = chart.ChartData[2, 2, 4, 2];
//Creates a new chart series with the name
IOfficeChartSerie seriesFeb = chart.Series.Add("Feb");
//Sets the data range of chart series – start row, start column, end row, end column
seriesFeb.Values = chart.ChartData[2, 3, 4, 3];
//Creates a new chart series with the name
IOfficeChartSerie seriesMarch = chart.Series.Add("March");
//Sets the data range of chart series – start row, start column, end row, end column
seriesMarch.Values = chart.ChartData[2, 4, 4, 4];
//Specifies the chart type
chart.ChartType = OfficeChartType.Bar_Stacked_100;
using (var stream = new MemoryStream())
{
pptxDoc.PresentationRenderer.ConvertToImage(chart, stream);
slide.Shapes.AddPicture(stream, 100, 10, 700, 500);
}
FileStream outputStream = new FileStream("Sample.pptx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
pptxDoc.Save(outputStream);
pptxDoc.Close();
outputStream.Flush();
outputStream.Dispose();
|
IPresentation pptxDoc = Presentation.Create(); // Create an instance of Presentation Renderer for converting chart to image pptxDoc.PresentationRenderer = new PresentationRenderer(); //Adds a blank slide to the Presentation ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank); //Adds chart to the slide with position and size IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Specifies the chart title chart.ChartTitle = "Sales Analysis"; //Sets chart data - Row1 chart.ChartData.SetValue(1, 2, "Jan"); chart.ChartData.SetValue(1, 3, "Feb"); //Sets chart data - Row2 chart.ChartData.SetValue(2, 1, 2010); chart.ChartData.SetValue(2, 2, 600); chart.ChartData.SetValue(2, 3, 400); //Sets chart data - Row3 chart.ChartData.SetValue(3, 1, 2011); chart.ChartData.SetValue(3, 2, 500); chart.ChartData.SetValue(3, 3, 500); //Sets chart data - Row4 chart.ChartData.SetValue(4, 1, 2012); chart.ChartData.SetValue(4, 2, 300); chart.ChartData.SetValue(4, 3, 700); //Sets the data range of the category axis chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 3, 1]; chart.PrimaryValueAxis.IsAutoMax = false; chart.PrimaryValueAxis.MaximumValue = 1001; chart.PrimaryValueAxis.MajorUnit = 100; //Creates a new chart series with the name IOfficeChartSerie seriesJan = chart.Series.Add("Jan"); //Sets the data range of chart series – start row, start column, end row, end column seriesJan.Values = chart.ChartData[2, 2, 4, 2]; //Creates a new chart series with the name IOfficeChartSerie seriesFeb = chart.Series.Add("Feb"); //Sets the data range of chart series – start row, start column, end row, end column seriesFeb.Values = chart.ChartData[2, 3, 4, 3]; //Specifies the chart type chart.ChartType = OfficeChartType.Bar_Stacked; using (var stream = new MemoryStream()) { pptxDoc.PresentationRenderer.ConvertToImage(chart, stream); slide.Shapes.AddPicture(stream, 100, 10, 700, 500); } FileStream outputStream = new FileStream("Sample.pptx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); pptxDoc.Save(outputStream); pptxDoc.Close(); outputStream.Flush(); outputStream.Dispose(); |
chart.ChartData.SetValue(4, 3, 700);
//Sets the data range of the category axis
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 3, 1];
chart.PrimaryValueAxis.IsAutoMax = false; chart.PrimaryValueAxis.MaximumValue = 1010;
chart.PrimaryValueAxis.MajorUnit = 100;
//Creates a new chart series with the name IOfficeChartSerie seriesJan = chart.Series.Add("Jan");
//Sets the data range of chart series – start row, start column, end row, end column
seriesJan.Values = chart.ChartData[2, 2, 4, 2]; |