Articles in this section
Category / Section

How to bind the CSV data in WPF Chart (SfChart)?

3 mins read

This article explains how to bind the CSV data to the WPF Chart with the following steps.

Step 1: Initialize a data model that represents a data point for the Chart.

[Model]

public class Model
{
 public string Month { get; set; }
 public double Appointments { get; set; }
 public Model(string month, double appointments)
 {
  Month= month;
  Appointments = appointments;
 }
}

 

Step 2: Store the list of the data model (numbers and text) in a CSV file. Each line of the file is a data model. Each model consists of two fields, separated by commas.

[CSV Data]

Month,Appointments
Jan,360
Feb,490
Mar,250
Apr,670
May,575
Jun,430
Jul,680
Aug,345
Sep,563
Oct,420
Nov,634
Dec, 277

 

Step 3: Convert CSV data to List<Model>.

[ViewModel]

public class ViewModel
{
 public List<Model> Data { get; set; }
 
 public ViewModel()
 {
  Data = new List<Model>(ReadCSV("..\\..\\ChartData"));
 }
 
 public IEnumerable<Model> ReadCSV(string fileName)
 {
  List<string> lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName, ".csv")).ToList();
  //First row containing column names.
  lines.RemoveAt(0);
 
  return lines.Select(line =>
  {
   string[] data = line.Split(',');
   return new Model(Convert.ToString(data[0]), Convert.ToDouble(data[1]));
  });
 }
}

 

Step 4: Bind the converted List<Model> to the SfChart.

[XAML]

<chart:SfChart>
...
    <chart:ColumnSeries ItemsSource="{Binding Data}"
                                          XBindingPath="Month"
                                          YBindingPath="Appointments"/>
</chart:SfChart>

 

Download the complete sample in GitHub

Output:

Binding CSV data in WPF Chart

Refer to this Documentation, to acquire more knowledge on data binding in WPF Charts.

See Also,

How to bind the JSON data in WPF Chart?

How to bind the SQL Database to the WPF chart?

How to bind the data table in WPF Chart?

Conclusion

I hope you enjoyed learning about how to bind the CSV data in WPF Chart (SfChart).

You can refer to our WPF Charts’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Charts documentation to understand how to present and manipulate data. 

For current customers, you can check out our WPF 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 WPF Charts and other WPF components.

If you have any queries or require clarifications, please let us know in comments 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 sign in to leave a comment
Access denied
Access denied