Create Excel Chart

IronXL supports the 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.

using IronXL;
using System;

class Program
{
    static void Main()
    {
        // Load an existing workbook or create a new one
        WorkBook workbook = WorkBook.Load("Sample.xlsx");

        // Access the first worksheet
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Adding values to cells for the chart
        sheet["A1"].Value = "Category";
        sheet["B1"].Value = "Values";
        sheet["A2"].Value = "Category 1";
        sheet["B2"].Value = 10;
        sheet["A3"].Value = "Category 2";
        sheet["B3"].Value = 20;
        sheet["A4"].Value = "Category 3";
        sheet["B4"].Value = 30;

        // Create a new line chart
        var chart = sheet.CreateChart(ChartType.Line);

        // Set the data range for the chart
        chart.SetData(sheet["A1:B4"]);

        // Set chart title and legend
        chart.Title = "Sample Line Chart";
        chart.ShowLegend = true;

        // Plot the chart within the worksheet
        chart.Plot();

        // Save the changes to the workbook
        workbook.SaveAs("Sample_With_Chart.xlsx");

        Console.WriteLine("Excel file with line chart is created.");
    }
}
using IronXL;
using System;

class Program
{
    static void Main()
    {
        // Load an existing workbook or create a new one
        WorkBook workbook = WorkBook.Load("Sample.xlsx");

        // Access the first worksheet
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Adding values to cells for the chart
        sheet["A1"].Value = "Category";
        sheet["B1"].Value = "Values";
        sheet["A2"].Value = "Category 1";
        sheet["B2"].Value = 10;
        sheet["A3"].Value = "Category 2";
        sheet["B3"].Value = 20;
        sheet["A4"].Value = "Category 3";
        sheet["B4"].Value = 30;

        // Create a new line chart
        var chart = sheet.CreateChart(ChartType.Line);

        // Set the data range for the chart
        chart.SetData(sheet["A1:B4"]);

        // Set chart title and legend
        chart.Title = "Sample Line Chart";
        chart.ShowLegend = true;

        // Plot the chart within the worksheet
        chart.Plot();

        // Save the changes to the workbook
        workbook.SaveAs("Sample_With_Chart.xlsx");

        Console.WriteLine("Excel file with line chart is created.");
    }
}
Imports IronXL
Imports System

Friend Class Program
	Shared Sub Main()
		' Load an existing workbook or create a new one
		Dim workbook As WorkBook = WorkBook.Load("Sample.xlsx")

		' Access the first worksheet
		Dim sheet As WorkSheet = workbook.DefaultWorkSheet

		' Adding values to cells for the chart
		sheet("A1").Value = "Category"
		sheet("B1").Value = "Values"
		sheet("A2").Value = "Category 1"
		sheet("B2").Value = 10
		sheet("A3").Value = "Category 2"
		sheet("B3").Value = 20
		sheet("A4").Value = "Category 3"
		sheet("B4").Value = 30

		' Create a new line chart
		Dim chart = sheet.CreateChart(ChartType.Line)

		' Set the data range for the chart
		chart.SetData(sheet("A1:B4"))

		' Set chart title and legend
		chart.Title = "Sample Line Chart"
		chart.ShowLegend = True

		' Plot the chart within the worksheet
		chart.Plot()

		' Save the changes to the workbook
		workbook.SaveAs("Sample_With_Chart.xlsx")

		Console.WriteLine("Excel file with line chart is created.")
	End Sub
End Class
$vbLabelText   $csharpLabel

The above C# program demonstrates how to use IronXL to create a line chart in an Excel workbook. The code performs the following steps:

  1. Load a Workbook: Use WorkBook.Load() to load an existing Excel file or create a new one if it doesn't exist.
  2. Access a Worksheet: Use the default worksheet in the workbook to enter data.
  3. Enter Data: Manually enter data that will be used to plot the chart.
  4. Create a Chart: Initialize a new line chart using CreateChart with the specified ChartType.
  5. Define Data Range: Use SetData to specify the range of cells containing the data for the chart.
  6. Customize Chart: Add a title and display the legend for clarification.
  7. Plot and Save: Call Plot() to insert the chart into the worksheet and save the updates to the Excel file.
  8. Inform: Output confirmation that the process is completed.

Make sure to install the IronXL package via NuGet to run this code successfully.