Change the color of single data point in Excel chart in C#, VB.NET
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also, converts Excel documents to PDF files. Using this library, you can change the color of chart data points.
This sample explains how to change the color of a single data point in an Excel chart.
Steps to change the color of a data point in an Excel chart, programmatically:
- Create a new C# console application project.
Create a new C# console application project
- Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
Install the NuGet package to the project
- Add the Excel file with data for creating a chart into the project and make the file an Embedded Resource, by following the steps below:
- In Visual Studio, click the Project menu and select Add Existing Item. Find and select the Excel file you want to add to your project.
- In the Solution Explorer window, right-click on the Excel file you just added to your project and select Properties from the popup menu. The Properties tool window appears.
- In the Properties window, change the Build Action property to Embedded Resource.
- Build the project. All the Excel files will be compiled into your project’s assembly.
- A data point in an Excel chart is represented by IChartDataPoint and the ForeColor property of the IFill interface is used to fill the color in the data point.
C#
// Change the color of a data point chart.Series[0].DataPoints[4].DataFormat.Fill.ForeColor = Color.YellowGreen;
VB.NET
'Change the color of a data point chart.Series(0).DataPoints(4).DataFormat.Fill.ForeColor = Color.YellowGreen
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.XlsIO; using System.Drawing; using System.IO; using System.Reflection;
VB.NET
Imports System.Drawing Imports System.IO Imports System.Reflection Imports Syncfusion.XlsIO
- Include the following code snippet in the main method of the Program.cs file to change the color of a single data point in an Excel chart.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { // Instantiate the Excel application object IApplication application = excelEngine.Excel; // Load an existing Excel file into IWorkbook Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("DataPointColor.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic); // Get the first worksheet in the workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; // Add a chart to the worksheet IChartShape chart = worksheet.Charts.Add(); // Set the data range and title to the chart chart.DataRange = worksheet.Range["A2:C12"]; chart.ChartTitle = "Texas Books Unit Sales"; // Set the chart series in column for the assigned data region chart.IsSeriesInRows = false; // Set the position for the chart in the worksheet chart.TopRow = 2; chart.LeftColumn = 5; chart.BottomRow = 16; chart.RightColumn = 13; // Change the color of a data point chart.Series[0].DataPoints[4].DataFormat.Fill.ForeColor = Color.YellowGreen; // Save the Excel document workbook.SaveAs("Output.xlsx"); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Load an existing Excel file into IWorkbook Dim assembly As Assembly = GetType(Module1).GetTypeInfo().Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("VBSample.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic) 'Get the first worksheet in the workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Add a chart to the worksheet Dim chart As IChartShape = worksheet.Charts.Add() 'Set the data range and title to the chart chart.DataRange = worksheet.Range("A2:C12") chart.ChartTitle = "Texas Books Unit Sales" 'Set the chart series in column for the assigned data region chart.IsSeriesInRows = False 'Set the position for the chart in the worksheet chart.TopRow = 2 chart.LeftColumn = 5 chart.BottomRow = 16 chart.RightColumn = 13 'Change the color of a data point chart.Series(0).DataPoints(4).DataFormat.Fill.ForeColor = Color.YellowGreen 'Save the Excel document workbook.SaveAs("Output.xlsx") End Using
A complete working sample to change the color of a single data point in an Excel chart can be downloaded from DataPointColor.zip.
By executing the program, you will get the output Excel document as follows.
Output Excel document
Take a moment to peruse the documentation where you will find other options like creating and removing a chart, customizing chart and chart elements, sparkline, excel 2016 charts and creation of all supported chart types.
Click here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
An online sample link to create a chart worksheet.
See Also:
Set the axis interval in terms of days
Set the data label position for chart series
Change the color of gridlines in Excel chart
Copy and insert the chart in same worksheet
Position and re-size the plot area of chart
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about Change the color of single data point in Excel chart in C#, VB.NET.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!