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 chart type and position - Use the
AddSeries
method to add data series - Plot the chart using the
Plot
method
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronXL on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming Excel with C#.
Install-Package IronXL.Excel
Consider installing the IronXL DLL directly. Download and manually install it for your project or GAC form: IronXL.zip
Manually install into your project
Download DLLCreate Charts Example
IronXL supports column, scatter, line, pie, bar, and area chart. 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 locations in the worksheet. - Add the series with the
AddSeries
method. This method also accept a single column of data since that is sufficient for some chart type. The first parameter represents value for horizontal axis. The second parameter represents value for 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 call of this method leads 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;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;
// Set the chart title
chart.SetTitle("Column Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("columnChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue
' Set the chart title
chart.SetTitle("Column Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
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
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;
// Set the chart title
chart.SetTitle("Line Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("lineChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue
' Set the chart title
chart.SetTitle("Line Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
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;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Set the chart title
chart.SetTitle("Pie Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Set the chart title
chart.SetTitle("Pie Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("pieChart.xlsx")
Edit Chart Example
There are a few things you can edit in the 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;
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the chart
IChart chart = workSheet.Charts[0];
// Edit the legend position
chart.SetLegendPosition(LegendPosition.Top);
// Edit the chart title
chart.SetTitle("Edited Chart");
workBook.SaveAs("editedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the chart
Private chart As IChart = workSheet.Charts(0)
' Edit the legend position
chart.SetLegendPosition(LegendPosition.Top)
' Edit the chart title
chart.SetTitle("Edited Chart")
workBook.SaveAs("editedChart.xlsx")
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;
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the chart
List<IChart> chart = workSheet.Charts;
// Remove the chart
workSheet.RemoveChart(chart[0]);
workBook.SaveAs("removedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Imports System.Collections.Generic
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the chart
Private chart As List(Of IChart) = workSheet.Charts
' Remove the chart
workSheet.RemoveChart(chart(0))
workBook.SaveAs("removedChart.xlsx")