How to Create and Edit Excel Charts in C#
In Excel, a chart is a graphical representation of data. It is a visual tool used to display and analyze data in a more understandable and meaningful way. Excel provides a variety of chart types, such as bar charts, line charts, pie charts, and more, each of which is suited for different types of data and analysis.
IronXL supports column, scatter, line, pie, bar, and area charts with configurable series names, legend positions, chart titles, and chart positions.
How to Create and Edit Excel Charts in C#
- Download the C# library to create and edit charts
- Prepare the data for creating the chart
- Use the
CreateChart
method to set the chart type and position - Use the
AddSeries
method to add data series - Plot the chart using the
Plot
method
Get started with IronXL
Start using IronXL in your project today with a free trial.
Create Charts Example
IronXL supports column, scatter, line, pie, bar, and area charts. To create a chart, we have to specify a few things separately.
- Start by using the
CreateChart
method to specify the type of chart and its location in the worksheet. - Add the series with the
AddSeries
method. This method also accepts a single column of data since that is sufficient for some chart types. The first parameter represents values for the horizontal axis. The second parameter represents values for the vertical axis. - Optionally, specify the series name, chart name, and legend position.
- Invoke the
Plot
method to plot the chart. The method will plot the chart using all the data that was added. Multiple calls to this method lead to plotting multiple charts instead of modifying the existing chart.
Let's create some charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:

Column Chart
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-column-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
// Load the workbook from an existing Excel file
WorkBook workBook = WorkBook.Load("chart.xlsx");
// Access the default worksheet in the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Create a column chart in the worksheet
// Specify the top-left corner at cell E5 and the bottom-right corner at cell T10
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
// Define the range for the X-axis data
string xAxis = "A2:A7";
// Add a series to the chart using data from columns B, C, and D
// and set their respective titles based on the worksheet header.
// Add the first data series from column B
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Add the second data series from column C
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;
// Add the third data series from column D
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;
// Set the title of the chart
chart.SetTitle("Column Chart");
// Define the position of the chart legend
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart with the data and formatting specified
chart.Plot();
// Save the modified workbook to a new file with the chart included
workBook.SaveAs("columnChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
' Load the workbook from an existing Excel file
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
' Access the default worksheet in the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Create a column chart in the worksheet
' Specify the top-left corner at cell E5 and the bottom-right corner at cell T10
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)
' Define the range for the X-axis data
Private xAxis As String = "A2:A7"
' Add a series to the chart using data from columns B, C, and D
' and set their respective titles based on the worksheet header.
' Add the first data series from column B
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Add the second data series from column C
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue
' Add the third data series from column D
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue
' Set the title of the chart
chart.SetTitle("Column Chart")
' Define the position of the chart legend
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart with the data and formatting specified
chart.Plot()
' Save the modified workbook to a new file with the chart included
workBook.SaveAs("columnChart.xlsx")

Line Chart
Since a line chart can represent as much information as a column chart, switching between the two is very simple. You only need to change the chart type.
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-line-chart.cs
// Import the necessary namespaces from IronXL for working with Excel files and charts.
using IronXL;
using IronXL.Drawing.Charts;
// Load an existing Excel workbook from the specified file.
WorkBook workBook = WorkBook.Load("chart.xlsx");
// Fetch the default worksheet from the loaded workbook.
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Create a chart in the worksheet of type Column at the specified position and size.
// The numbers correspond to left column, top row, right column, and bottom row positions.
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
// Define the range for the X Axis labels, taken from column A, rows 2 to 7.
string xAxis = "A2:A7";
// Add the first series to the chart using data from column B.
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue; // Set the series title from cell B1.
// Add the second series to the chart using data from column C.
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue; // Set the series title from cell C1.
// Add the third series to the chart using data from column D.
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue; // Set the series title from cell D1.
// Set the title for the chart.
chart.SetTitle("Line Chart");
// Position the legend of the chart at the bottom.
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart to make it visible in the worksheet.
chart.Plot();
// Save the modified workbook to a new file.
workBook.SaveAs("lineChart.xlsx");
' Import the necessary namespaces from IronXL for working with Excel files and charts.
Imports IronXL
Imports IronXL.Drawing.Charts
' Load an existing Excel workbook from the specified file.
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
' Fetch the default worksheet from the loaded workbook.
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Create a chart in the worksheet of type Column at the specified position and size.
' The numbers correspond to left column, top row, right column, and bottom row positions.
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)
' Define the range for the X Axis labels, taken from column A, rows 2 to 7.
Private xAxis As String = "A2:A7"
' Add the first series to the chart using data from column B.
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue ' Set the series title from cell B1.
' Add the second series to the chart using data from column C.
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue ' Set the series title from cell C1.
' Add the third series to the chart using data from column D.
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue ' Set the series title from cell D1.
' Set the title for the chart.
chart.SetTitle("Line Chart")
' Position the legend of the chart at the bottom.
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart to make it visible in the worksheet.
chart.Plot()
' Save the modified workbook to a new file.
workBook.SaveAs("lineChart.xlsx")

Pie Chart
For a pie chart, only one column of data is needed.
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
// This example demonstrates an approach for creating and manipulating a pie chart using the IronXL library.
// Load the Excel workbook from the specified file
WorkBook workBook = WorkBook.Load("chart.xlsx");
// Get the default worksheet from the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Create a pie chart on the worksheet at the specified position (columns 5 to 20, rows 5 to 10)
// The ChartType.Pie enumerates the type of chart to be created; position parameters define the chart's location and size
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);
// Define the range for the x-axis data. The x-axis for a pie chart typically represents the categories.
string xAxis = "A2:A7";
// Add a series to the chart using the x-axis range and y-axis range
// Here, the y-axis data is defined, which usually represents the data values associated with the x-axis categories
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
// Set the series title using the value from cell B1. This helps in understanding what the series represents.
series.Title = workSheet["B1"].StringValue;
// Set the title of the chart. This provides an overall context for what the chart is displaying.
chart.SetTitle("Pie Chart");
// Set the position of the chart legend to the bottom. The legend helps identify the data series.
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart with the defined series and settings. This renders the chart visually in the worksheet.
chart.Plot();
// Save the workbook to a new file, ensuring that changes to the chart are stored.
workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
' This example demonstrates an approach for creating and manipulating a pie chart using the IronXL library.
' Load the Excel workbook from the specified file
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
' Get the default worksheet from the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Create a pie chart on the worksheet at the specified position (columns 5 to 20, rows 5 to 10)
' The ChartType.Pie enumerates the type of chart to be created; position parameters define the chart's location and size
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)
' Define the range for the x-axis data. The x-axis for a pie chart typically represents the categories.
Private xAxis As String = "A2:A7"
' Add a series to the chart using the x-axis range and y-axis range
' Here, the y-axis data is defined, which usually represents the data values associated with the x-axis categories
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
' Set the series title using the value from cell B1. This helps in understanding what the series represents.
series.Title = workSheet("B1").StringValue
' Set the title of the chart. This provides an overall context for what the chart is displaying.
chart.SetTitle("Pie Chart")
' Set the position of the chart legend to the bottom. The legend helps identify the data series.
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart with the defined series and settings. This renders the chart visually in the worksheet.
chart.Plot()
' Save the workbook to a new file, ensuring that changes to the chart are stored.
workBook.SaveAs("pieChart.xlsx")

Edit Chart Example
There are a few things you can edit in an existing chart. You can edit the legend position and chart title. To edit the chart, you must first retrieve the chart by accessing the Charts property and select the targeted chart for editing. From there, access the properties of the chart to make your edits.
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
// This code uses the IronXL library to manipulate Excel files and charts.
// Load the workbook from an Excel file named "pieChart.xlsx"
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
// Use the default worksheet in the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the first chart from the worksheet's charts collection
// Check if there are any charts in the collection to avoid runtime errors
if (workSheet.Charts.Count > 0)
{
IChart chart = workSheet.Charts[0];
// Set the chart's legend position to the top
chart.SetLegendPosition(LegendPosition.Top);
// Set the chart's title to "Edited Chart"
chart.SetTitle("Edited Chart");
// Save the modified workbook to a new file named "editedChart.xlsx"
workBook.SaveAs("editedChart.xlsx");
}
else
{
Console.WriteLine("No charts found in the worksheet.");
}
Imports IronXL
Imports IronXL.Drawing.Charts
' This code uses the IronXL library to manipulate Excel files and charts.
' Load the workbook from an Excel file named "pieChart.xlsx"
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
' Use the default worksheet in the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the first chart from the worksheet's charts collection
' Check if there are any charts in the collection to avoid runtime errors
If workSheet.Charts.Count > 0 Then
Dim chart As IChart = workSheet.Charts(0)
' Set the chart's legend position to the top
chart.SetLegendPosition(LegendPosition.Top)
' Set the chart's title to "Edited Chart"
chart.SetTitle("Edited Chart")
' Save the modified workbook to a new file named "editedChart.xlsx"
workBook.SaveAs("editedChart.xlsx")
Else
Console.WriteLine("No charts found in the worksheet.")
End If

Before

After
Remove Chart Example
To remove an existing chart from a spreadsheet, first retrieve the chart from the Charts property of the worksheet object. You will receive a list of charts from the Charts property. Pass the targeted chart object to the RemoveChart
method to remove it.
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
using System.Collections.Generic;
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
// Access the default worksheet within the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the list of charts in the worksheet
List<IChart> charts = workSheet.Charts;
// Check if there is at least one chart to remove
if (charts != null && charts.Count > 0)
{
// Remove the first chart from the worksheet
workSheet.RemoveChart(charts[0]);
}
else
{
// Inform the user that no charts were found to remove
Console.WriteLine("No chart found to remove in the worksheet.");
}
// Save the workbook after modifying it
workBook.SaveAs("removedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Imports System.Collections.Generic
' Load the Excel workbook
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
' Access the default worksheet within the workbook
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the list of charts in the worksheet
Private charts As List(Of IChart) = workSheet.Charts
' Check if there is at least one chart to remove
If charts IsNot Nothing AndAlso charts.Count > 0 Then
' Remove the first chart from the worksheet
workSheet.RemoveChart(charts(0))
Else
' Inform the user that no charts were found to remove
Console.WriteLine("No chart found to remove in the worksheet.")
End If
' Save the workbook after modifying it
workBook.SaveAs("removedChart.xlsx")
Frequently Asked Questions
What types of charts can be created using a C# library?
IronXL supports creating column, scatter, line, pie, bar, and area charts.
How do I start creating charts in C#?
First, download the IronXL library from NuGet. Then use the CreateChart method to specify the type and location of the chart in the worksheet.
What method is used to add data series to a chart?
The AddSeries method is used to add data series to a chart. It accepts parameters for both the horizontal and vertical axis data.
How can I plot a chart after adding data?
Use the Plot method to plot the chart after adding the necessary data. This method uses all the data that was added to produce the chart.
How can you switch between a column chart and a line chart?
Switching between a column chart and a line chart is simple; you only need to change the chart type parameter when creating the chart.
Can you edit existing charts?
Yes, you can edit existing charts by accessing the Charts property of the worksheet, selecting the targeted chart, and modifying properties like the chart title and legend position.
What is the process to remove a chart from a worksheet?
To remove a chart, retrieve it from the Charts property of the worksheet object, then pass the targeted chart object to the RemoveChart method.
What should be done after modifying or creating charts to save changes?
After creating or modifying charts, save the workbook using the SaveAs method to ensure all changes are preserved.