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.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


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.

  1. Start by using the CreateChart method to specify the type of chart and its location in the worksheet.
  2. 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.
  3. Optionally, specify the series name, chart name, and legend position.
  4. 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:

Data

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")
$vbLabelText   $csharpLabel
Column chart

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")
$vbLabelText   $csharpLabel
Line chart

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")
$vbLabelText   $csharpLabel
Pie chart

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;

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")
$vbLabelText   $csharpLabel
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")
$vbLabelText   $csharpLabel

Frequently Asked Questions

How can I create Excel charts in C# without using Interop?

You can create Excel charts in C# without Interop by using the IronXL library. First, download the library from NuGet, then use methods such as CreateChart to specify the chart type and its location in the worksheet.

What steps are involved in customizing Excel charts using C#?

To customize Excel charts using C#, first prepare your data, then use the AddSeries method to add data series. Customize chart properties such as titles and legend positions by accessing the chart through the worksheet's Charts property.

How do I switch between different chart types in C#?

Switching between different chart types in C# is straightforward. Simply change the chart type parameter in the CreateChart method to switch between column, scatter, line, pie, bar, or area charts.

What is the method to save changes after editing an Excel chart in C#?

After editing an Excel chart, use the SaveAs method to save changes to the workbook, ensuring all modifications are preserved.

Can I remove an existing chart from an Excel worksheet using C#?

Yes, you can remove an existing chart by retrieving it through the worksheet's Charts property and then using the RemoveChart method to delete it.

What are the benefits of using IronXL for chart creation in .NET applications?

IronXL allows for efficient chart creation and editing in .NET applications without relying on Excel Interop, which enhances performance and reduces complexity. It supports various chart types and provides methods for easy customization.

How can I plot a chart in C# after adding data series?

After adding data series using the AddSeries method, you can plot the chart by calling the Plot method, which generates the chart using the provided data.

Is it possible to edit the properties of a chart in C# after it has been created?

Yes, you can edit the properties of a chart in C# by accessing it via the worksheet's Charts property and modifying attributes like the chart title, legend position, and more.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.