DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

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

Top comments (0)