Articles in this section
Category / Section

How to Bind Excel Data in WPF Charts?

5 mins read

This article explains how to bind data from an Excel file to a Syncfusion WPF SfChart control. By following the steps outlined below, you will be able to load Excel data and display it in the chart.

Steps to Bind Excel Data in WPF Charts

1. Install Required NuGet Packages

To work with Excel data in WPF SfChart, you need to install the following NuGet packages.

Syncfusion.SfChart.WPF for the WPF chart control.
Syncfusion.XlsIO.WPF for reading Excel files.

You can install these packages using the NuGet Package Manager.

2. Create the Data Model

Define a data model to hold the Excel data. For example, a class ProductSales can represent each row of data.

[XAML]

public class ProductSales
{
   public string Month { get; set; }
   public double Value { get; set; }
} 

3. Add Excel File to the Project and set Excel File Properties

  1. Right-click on the project in the Solution Explorer.
  2. Select Add > Existing Item… and browse to the Excel file (e.g., Data.xlsx) you want to include.
  3. Right-click on the added Excel file in the Solution Explorer and select Properties.
  4. Set the Build Action property to Embedded resource and the Copy to Output Directory property to Copy if newer or (Copy always if you want the file copied every time you build).

Excel file data to bind in WPF SfChart

4. Read Data from the Excel File

In this step, define a ViewModel class that holds the data collections for the chart and includes a method to read data from the Excel file, converting it into a collection of ProductSales objects. The following example utilizes the Syncfusion.XlsIO.WPF library.

[C#]

using Syncfusion.XlsIO;
using System.Collections.ObjectModel;

. . .

public class ViewModel
{
   public ObservableCollection<ProductSales> ProductAData { get; set; }
   public ObservableCollection<ProductSales> ProductBData { get; set; }
   public ObservableCollection<ProductSales> ProductCData { get; set; }

   public ViewModel()
   {
       // Initialize data collections
       ProductAData = new ObservableCollection<ProductSales>();
       ProductBData = new ObservableCollection<ProductSales>();
       ProductCData = new ObservableCollection<ProductSales>();
       
       // Load Excel data
       LoadExcelData("Resource\\Data.xlsx");
   }

   private void LoadExcelData(string filePath)
   {
       using (ExcelEngine excelEngine = new ExcelEngine())
       {
           IApplication application = excelEngine.Excel;
           IWorkbook workbook = application.Workbooks.Open(filePath);
           IWorksheet worksheet = workbook.Worksheets[0]; // First worksheet
           int lastRow = worksheet.UsedRange.LastRow; // Get the last row with data dynamically
           for (int i = 2; i <= lastRow; i++) // Assuming headers are in Row 1
           {
               string month = worksheet[$"A{i}"].Text;
               ProductAData.Add(new ProductSales { Month = month, Value = worksheet[$"B{i}"].Number });
               ProductBData.Add(new ProductSales { Month = month, Value = worksheet[$"C{i}"].Number });
               ProductCData.Add(new ProductSales { Month = month, Value = worksheet[$"D{i}"].Number });
           }
   
           workbook.Close();
       }
   }
}

5. Configuring the Syncfusion WPF Chart

Let’s configure the Syncfusion WPF Charts control using this documentation.

[XAML]

<syncfusion:SfChart Header="Product Sales Report">
. . .

   <syncfusion:SfChart.PrimaryAxis>
       <syncfusion:CategoryAxis Header="Month" />
   </syncfusion:SfChart.PrimaryAxis>

   <syncfusion:SfChart.SecondaryAxis>
       <syncfusion:NumericalAxis Header="Sales" />
   </syncfusion:SfChart.SecondaryAxis>
   
   <syncfusion:ColumnSeries ItemsSource="{Binding ProductAData}" 
                            XBindingPath="Month" 
                            YBindingPath="Value" />

   <syncfusion:ColumnSeries ItemsSource="{Binding ProductBData}" 
                            XBindingPath="Month" 
                            YBindingPath="Value"/>

   <syncfusion:ColumnSeries ItemsSource="{Binding ProductCData}" 
                            XBindingPath="Month" 
                            YBindingPath="Value" />
   
. . .
</syncfusion:SfChart> 

Output

The following image illustrates how to successfully bind Excel data to the WPF Chart.

Bind Excel data in WPF Chart

For more details, please refer to the project on GitHub.

Conclusion

I hope you enjoyed learning how to bind Excel data in WPF Chart control.
You can refer to our WPF Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart Examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied