DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

1 1

Create Excel Bar Chart

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

This example shows how to create a bar 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 its position on the worksheet.
var chart = sheet.CreateChart(ChartType.Bar, 10, 2, 18, 5);


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

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


//Set the legend position.
//Can be removed by settind it to null.
chart.SetLegendPosition(LegendPosition.Left);


//Plot all the data that was added to the chart before.
//Multiple calls 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 the new one.
chart.Plot();

workbook.SaveAs("CreateBarChart.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 its position on the worksheet.
Private chart = sheet.CreateChart(ChartType.Bar, 10, 2, 18, 5)


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

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


'Set the legend position.
'Can be removed by settind it to null.
chart.SetLegendPosition(LegendPosition.Left)


'Plot all the data that was added to the chart before.
'Multiple calls 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 the new one.
chart.Plot()

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

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