How to Change Pie Chart Datapoint Color in WinForms Presentation?
The PowerPoint framework is a feature rich .NET PowerPoint class library that can be used by developers to create, read, edit and convert PowerPoint files to PDF and images programmatically without Office or interop dependencies.
Steps to change each datapoint color for Pie chart in the Presentation document:
- Create a new C# console application project.
- Install the Syncfusion.Presentation.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Presentation; using Syncfusion.OfficeChart; using System.Drawing;
VB.NET
Imports Syncfusion.Presentation Imports Syncfusion.OfficeChart Imports System.Drawing
- Use the following code example to change each datapoint color for Pie chart in the Presentation document
C#
//Create a Presentation instance IPresentation pptxDoc = Presentation.Create(); //Add a blank slide to the Presentation ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank); //Add chart to the slide with position and size IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500); //Set chart type chart.ChartType = OfficeChartType.Pie; //Set chart title chart.ChartTitle = "Best Selling Products"; chart.ChartTitleArea.FontName = "Calibri"; chart.ChartTitleArea.Size = 14; //Set data for chart chart.ChartData.SetValue(1, 1, ""); chart.ChartData.SetValue(1, 2, "Sales"); chart.ChartData.SetValue(2, 1, "Phyllis Lapin"); chart.ChartData.SetValue(2, 2, 141.396); chart.ChartData.SetValue(3, 1, "Stanley Hudson"); chart.ChartData.SetValue(3, 2, 80.368); chart.ChartData.SetValue(4, 1, "Bernard Shah"); chart.ChartData.SetValue(4, 2, 71.155); chart.ChartData.SetValue(5, 1, "Patricia Lincoln"); chart.ChartData.SetValue(5, 2, 47.234); chart.ChartData.SetValue(6, 1, "Camembert Pierrot"); chart.ChartData.SetValue(6, 2, 46.825); chart.ChartData.SetValue(7, 1, "Thomas Hardy"); chart.ChartData.SetValue(7, 2, 42.593); chart.ChartData.SetValue(8, 1, "Hanna Moos"); chart.ChartData.SetValue(8, 2, 41.819); chart.ChartData.SetValue(9, 1, "Alice Mutton"); chart.ChartData.SetValue(9, 2, 32.698); chart.ChartData.SetValue(10, 1, "Christina Berglund"); chart.ChartData.SetValue(10, 2, 29.171); chart.ChartData.SetValue(11, 1, "Elizabeth Lincoln"); chart.ChartData.SetValue(11, 2, 25.696); //Create a new chart series with the name “Sales” IOfficeChartSerie pieSeries = chart.Series.Add("Sales"); pieSeries.Values = chart.ChartData[2, 2, 11, 2]; //set the color for each data point in the pie chart pieSeries.DataPoints[0].DataFormat.Fill.ForeColor = Color.DarkSlateBlue; pieSeries.DataPoints[1].DataFormat.Fill.ForeColor = Color.CadetBlue; pieSeries.DataPoints[2].DataFormat.Fill.ForeColor = Color.Red; //Set data label pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside; //Set background color chart.ChartArea.Fill.ForeColor = Color.FromArgb(242, 242, 242); chart.PlotArea.Fill.ForeColor = Color.FromArgb(242, 242, 242); chart.ChartArea.Border.LinePattern = OfficeChartLinePattern.None; //Set category labels chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 11, 1]; //Save the document pptxDoc.Save("Output.pptx"); //Close the document pptxDoc.Close();
VB.NET
'Create a Presentation instance Dim pptxDoc As IPresentation = Presentation.Create() 'Add a blank slide to the Presentation Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank) 'Add chart to the slide with position and size Dim chart As IPresentationChart = slide.Charts.AddChart(100, 10, 700, 500) 'Set chart type chart.ChartType = OfficeChartType.Pie 'Set chart title chart.ChartTitle = "Best Selling Products" chart.ChartTitleArea.FontName = "Calibri" chart.ChartTitleArea.Size = 14 'Set data for chart chart.ChartData.SetValue(1, 1, "") chart.ChartData.SetValue(1, 2, "Sales") chart.ChartData.SetValue(2, 1, "Phyllis Lapin") chart.ChartData.SetValue(2, 2, 141.396) chart.ChartData.SetValue(3, 1, "Stanley Hudson") chart.ChartData.SetValue(3, 2, 80.368) chart.ChartData.SetValue(4, 1, "Bernard Shah") chart.ChartData.SetValue(4, 2, 71.155) chart.ChartData.SetValue(5, 1, "Patricia Lincoln") chart.ChartData.SetValue(5, 2, 47.234) chart.ChartData.SetValue(6, 1, "Camembert Pierrot") chart.ChartData.SetValue(6, 2, 46.825) chart.ChartData.SetValue(7, 1, "Thomas Hardy") chart.ChartData.SetValue(7, 2, 42.593) chart.ChartData.SetValue(8, 1, "Hanna Moos") chart.ChartData.SetValue(8, 2, 41.819) chart.ChartData.SetValue(9, 1, "Alice Mutton") chart.ChartData.SetValue(9, 2, 32.698) chart.ChartData.SetValue(10, 1, "Christina Berglund") chart.ChartData.SetValue(10, 2, 29.171) chart.ChartData.SetValue(11, 1, "Elizabeth Lincoln") chart.ChartData.SetValue(11, 2, 25.696) 'Create a new chart series with the name “Sales” Dim pieSeries As IOfficeChartSerie = chart.Series.Add("Sales") pieSeries.Values = chart.ChartData(2, 2, 11, 2) 'set the color for each data point in the pie chart pieSeries.DataPoints(0).DataFormat.Fill.ForeColor = Color.DarkSlateBlue pieSeries.DataPoints(1).DataFormat.Fill.ForeColor = Color.CadetBlue pieSeries.DataPoints(2).DataFormat.Fill.ForeColor = Color.Red 'Set data label pieSeries.DataPoints.DefaultDataPoInteger.DataLabels.IsValue = True pieSeries.DataPoints.DefaultDataPoInteger.DataLabels.Position = OfficeDataLabelPosition.Outside 'Set background color chart.ChartArea.Fill.ForeColor = Color.FromArgb(242, 242, 242) chart.PlotArea.Fill.ForeColor = Color.FromArgb(242, 242, 242) chart.ChartArea.Border.LinePattern = OfficeChartLinePattern.None 'Set category labels chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData(2, 1, 11, 1) 'Save the document pptxDoc.Save("Output.pptx") 'Close the document pptxDoc.Close()
A complete working sample to change scatter chart marker color for each data points in Presentation using C# can be downloaded from here
By executing the program, you will get the output Word document as follows.
Explore more about the rich set of Syncfusion® PowerPoint Framework features.
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.
Conclusion
I hope you enjoyed learning about how to change pie chart datapoint color in WinForms Presentation.
You can refer to our WinForms Presentation feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Presentation documentation to understand how to create and manipulate data. You can also explore our WinForms 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!