How to change the slice color of Pie chart in Presentation using C# in ASP.NET Core?
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 change the slice color of Pie Chart in PowerPoint Presentation using C#.
Steps to change the slice color of Pie chart using C#
- Create a new C# Console application project
- Install the Syncfusion.Presentation.Net.Core NuGet package as a reference to your project from Nuget.org.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.
- Include the following namespace in the Program.cs file.
using Syncfusion.Presentation;
using Syncfusion.OfficeChart;
using Syncfusion.Drawing;
- Create a Simple Pie Chart in Presentation
//Load or open an PowerPoint Presentation.
using IPresentation pptxDoc = Presentation.Create();
//Add 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, 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 Datalabels
IOfficeChartSerie serie = chart.Series[0];
serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
serie.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.BestFit;
//Save the output PowerPoint Presentation
using FileStream outputStream = new(Path.GetFullPath("Output.pptx"), FileMode.Create, FileAccess.ReadWrite);
pptxDoc.Save(outputStream);
- Use the following code snippet to change the slice color.
//Change the color of each slice
serie.DataPoints[0].DataFormat.Fill.ForeColor = Color.Cornsilk;
serie.DataPoints[1].DataFormat.Fill.ForeColor = Color.Violet;
serie.DataPoints[2].DataFormat.Fill.ForeColor = Color.Pink;
A complete working sample to format chart title using C# can be downloaded from Github.
By executing the program, you will get the PowerPoint Presentation as follows.
See here to learn more about working with charts in PowerPoint presentation. Take a moment to peruse the documentation, where you can find basic presentation document processing options along with features like clone and merge slides and encrypt and decrypt PowerPoint presentation and most importantly PDF and image conversion with code examples.
Explore more about the rich set of Syncfusion PowerPoint Framework features.
Conclusion
I hope you enjoyed learning how to change the slice color of Pie chart in Presentation using C# in ASP.NET Core.
You can refer to our .NET PowerPoint Library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
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!