DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

3 1

Create Excel Line Chart

IronXL supports creation and editing of Charts for Excel documents in the modern XLSX file format.

This example shows how to create a Line Chart. Other chart types are also supported.

C#:

using IronXL;
using IronXL.Drawing.Charts;

WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.DefaultWorkSheet;

//Set the chart type and it's position  on the worksheet.
var chart = sheet.CreateChart(ChartType.Line, 10, 10, 18, 20);

//Add the series to the chart
//The first parameter represents the address of the range for horizontal(category) axis.
//The second  parameter represents the address of the range for vertical(value) axis.
var series = chart.AddSeries("B3:B8", "A3:A8");

//Set the chart title.
series.Title = "Line Chart";

//Set the legend position.
//Can be removed by setting it to None.
chart.SetLegendPosition(LegendPosition.Bottom);

//We can change the position of the chart.
chart.Position.LeftColumnIndex = 2;
chart.Position.RightColumnIndex = chart.Position.LeftColumnIndex + 3;

//Plot all the data that was added to the chart before.
//Multiple call of this method leads to plotting multiple charts instead of modifying the existing chart.
//Yet there is no possibility to remove chart or edit it's series/position.
//We can just create new one.
chart.Plot();

workbook.SaveAs("CreateLineChart.xlsx");
Enter fullscreen mode Exit fullscreen mode

VB:

Imports IronXL
Imports IronXL.Drawing.Charts

Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.DefaultWorkSheet

'Set the chart type and it's position  on the worksheet.
Private chart = sheet.CreateChart(ChartType.Line, 10, 10, 18, 20)

'Add the series to the chart
'The first parameter represents the address of the range for horizontal(category) axis.
'The second  parameter represents the address of the range for vertical(value) axis.
Private series = chart.AddSeries("B3:B8", "A3:A8")

'Set the chart title.
series.Title = "Line Chart"

'Set the legend position.
'Can be removed by setting it to None.
chart.SetLegendPosition(LegendPosition.Bottom)

'We can change the position of the chart.
chart.Position.LeftColumnIndex = 2
chart.Position.RightColumnIndex = chart.Position.LeftColumnIndex + 3

'Plot all the data that was added to the chart before.
'Multiple call of this method leads to plotting multiple charts instead of modifying the existing chart.
'Yet there is no possibility to remove chart or edit it's series/position.
'We can just create new one.
chart.Plot()

workbook.SaveAs("CreateLineChart.xlsx")
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay