Chart Control in Windows Forms Application

Introduction

In this article, we will see how to use the Chart control in Windows Forms Applications. As we know Charts and Graphs make data easier to understand and interpret. A chart is used to present the data in a visual form.
 
Let's Begin

Step 1

Open Visual Studio (I am using Visual Studio 2012) and create a new Windows Forms Application (select .NET Framework 4).
 
 
Step 2

Drop a Chart control from the Toolbox present in the Data tab.
 
 
 
 
Step 3

Go to Chart properties then click on Series.
 
 
 
Change the name of Series. Here, I set the name to Salary. We can also change the ChartType as well as appearance from the Series Collection Editor.
 
 
 
Go to the Form1.cs code and add the following lines of code:
  1. using System;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace DemoChart  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.   
  13.         private void Form1_Load(object sender, EventArgs e)  
  14.         {  
  15.             fillChart();  
  16.         }  
  17.         //fillChart method  
  18.         private void fillChart()  
  19.         {  
  20.             //AddXY value in chart1 in series named as Salary  
  21.             chart1.Series["Salary"].Points.AddXY("Ajay""10000");  
  22.             chart1.Series["Salary"].Points.AddXY("Ramesh""8000");  
  23.             chart1.Series["Salary"].Points.AddXY("Ankit""7000");  
  24.             chart1.Series["Salary"].Points.AddXY("Gurmeet""10000");  
  25.             chart1.Series["Salary"].Points.AddXY("Suresh""8500");  
  26.             //chart title  
  27.             chart1.Titles.Add("Salary Chart");  
  28.         }  
  29.     }  
  30. }  
Preview

 
 
Displaying Data in Chart control from the Database in Windows Forms Application

Step 1

Create a database (named Sample). Add a Table tbl_EmpSalary. The following is the table schema for creating tbl_EmpSalary.
 
 
 
Use the following preceding procedure. Add these lines of code to Form1.cs:
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5.   
  6. namespace DemoChart  
  7. {  
  8.     public partial class Form1 : Form  
  9.     {  
  10.         public Form1()  
  11.         {  
  12.             InitializeComponent();  
  13.         }  
  14.   
  15.         private void Form1_Load(object sender, EventArgs e)  
  16.         {  
  17.             fillChart();  
  18.         }  
  19.         //fillChart method  
  20.         private void fillChart()  
  21.         {  
  22.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");  
  23.             DataSet ds = new DataSet();  
  24.             con.Open();  
  25.             SqlDataAdapter adapt = new SqlDataAdapter("Select Name,Salary from tbl_EmpSalary", con);  
  26.             adapt.Fill(ds);  
  27.             chart1.DataSource = ds;  
  28.             //set the member of the chart data source used to data bind to the X-values of the series  
  29.             chart1.Series["Salary"].XValueMember = "Name";  
  30.             //set the member columns of the chart data source used to data bind to the X-values of the series  
  31.             chart1.Series["Salary"].YValueMembers = "Salary";  
  32.             chart1.Titles.Add("Salary Chart");  
  33.             con.Close();  
  34.   
  35.         }  
  36.     }  
  37. }  
Final Preview

 
 
I hope you like it. Thanks.  


Similar Articles