How to create a chart and save as an image using ASP.NET Core Presentation library
Syncfusion Presentation is a .NET PowerPoint Library used to create, read, edit and convert PowerPoint files to PDF and images programmatically without Microsoft Office or interop dependencies. Using this library, you can create a chart and save as an image using C#.
Steps to create a chart and save as an image
- Create a new C# console application project.
- Install the Syncfusion.PresentationRenderer.Net.Core NuGet package as a reference to your .NET applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.OfficeChart;
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;
- Use the following code example to create a chart and save as an image using the PowerPoint library.
C#
//Creates a Presentation instance
IPresentation pptxDoc = Presentation.Create();
//Adds a blank slide to the Presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a Pie chart
IPresentationChart pieChart = CreatePieChart(slide);
//Initialize the PresentationRenderer
pptxDoc.PresentationRenderer = new PresentationRenderer();
// Converts the chart to image.
Stream image = new FileStream("../../../ChartToImage.jpg", FileMode.Create, FileAccess.ReadWrite);
pptxDoc.PresentationRenderer.ConvertToImage(pieChart, image);
//Closes the presentation
pptxDoc.Close();
image.Close();
- Add the below helper methods to create a chart.
C#
IPresentationChart CreatePieChart(ISlide slide)
{
//Adds chart to the slide with position and size
IPresentationChart chart = slide.Charts.AddChart(100, 10, 500, 300);
chart.ChartType = OfficeChartType.Pie;
//Assign data
chart.DataRange = chart.ChartData[2, 1, 6, 2];
chart.IsSeriesInRows = false;
//Set data to the chart- RowIndex, columnIndex, and data
chart.ChartData.SetValue(1, 1, "Food");
chart.ChartData.SetValue(2, 1, "Fruits");
chart.ChartData.SetValue(3, 1, "Vegetables");
chart.ChartData.SetValue(4, 1, "Dairy");
chart.ChartData.SetValue(5, 1, "Protein");
chart.ChartData.SetValue(6, 1, "Grains");
chart.ChartData.SetValue(1, 2, "Percentage");
chart.ChartData.SetValue(2, 2, 36);
chart.ChartData.SetValue(3, 2, 14);
chart.ChartData.SetValue(4, 2, 13);
chart.ChartData.SetValue(5, 2, 28);
chart.ChartData.SetValue(6, 2, 9);
//Set Chart Title
chart.ChartTitle = "Pie Chart";
return chart;
}
A complete working sample of creating a chart and saving as an image using the PowerPoint library using C# can be downloaded from GitHub.
By executing the program, you will get the image as follows.
Take a moment to peruse the documentation, where you can find basic PowerPoint Presentation processing options along with features like, find and replace text, working with charts, animation, smart arts and many more in the PowerPoint Presentation, and most importantly PDF conversion with code examples.
Explore more about the rich set of Syncfusion PowerPoint Framework features.
Conclusion
I hope you enjoyed learning about how to create a chart and save as an image using ASP.NET Core Presentation library.
You can refer to our ASP.NET Core Presentation feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core Presentation example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!