How to Use Chart Control in C# 2010

I have created a GraphChart project using C# (2010), my project has the following six forms:

  1. frmChart: this is the main form to choose the type of the chart.

  2. frmGraph1: Binding the Chart to the Array from the DataGrid with the DataBindXY method.

  3. frmGraph2: Using the DataBindXY method as the previous form but binding the Chart to data from a MDB file and show the following chart:

    chart

  4. frmGraph3: Using the DataBindTable method to bind the Chart to the data from a MDB file.

  5. frmGraph4: Using the AddXY method to bind the Chart to the data from a MDB file.

  6. frmGraph5: Using DataSource and bind the Chart to the data from a MDB file with the DataBind method.

About the Code

I begin the code in all the forms with the following function:

  1. private void CreateChartArea()  
  2. {  
  3.       //Clear ChartAreas if found:  
  4.       MyChart.ChartAreas.Clear();  
  5.       //Create new ChartArea  
  6.       MyChartArea = new ChartArea();  
  7.       //Add ChartArea:  
  8.       MyChart.ChartAreas.Add("MyChartArea");  
  9.   
  10.       //Set BackColor to Chart and Graph area:  
  11.       MyChart.BackColor = Color.LightGray;  
  12.       MyChart.ChartAreas["MyChartArea"].BackSecondaryColor = Color.Orange;  
  13.       MyChart.ChartAreas["MyChartArea"].BackHatchStyle = ChartHatchStyle.Wave;  
  14.   
  15.       //Hide vertical lines:  
  16.       MyChart.ChartAreas["MyChartArea"].AxisX.MajorGrid.Enabled = false;  
  17.       //Set horizontal lines to Dash style:  
  18.       MyChart.ChartAreas["MyChartArea"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;  
  19.       //Set X-Axis to four Series:  
  20.       MyChart.ChartAreas["MyChartArea"].AxisX.Maximum = 5;  
  21.       //Set X-Axis labels font:  
  22.       MyChart.ChartAreas["MyChartArea"].AxisX.LabelStyle.Font = new Font("Arial", 10, FontStyle.Bold);  
  23.       //Set X-Axis labels color to blue:  
  24.       MyChart.ChartAreas["MyChartArea"].AxisX.LabelStyle.ForeColor = Color.Blue;  
  25.       //Set Y-Axis title:  
  26.       MyChart.ChartAreas["MyChartArea"].AxisY.Title = "Number of students";  
  27.       //Set Y-Axis title font:  
  28.       MyChart.ChartAreas["MyChartArea"].AxisY.TitleFont = new Font("Arial", 10, FontStyle.Bold);  
  29.       //Set Y-Axis title color:  
  30.       MyChart.ChartAreas["MyChartArea"].AxisY.TitleForeColor = Color.Blue;  
  31.   
  32.       //You can use ChartAreas index instead ChartAreas name:  
  33.       //Example: MyChart.ChartAreas[0].BackColor = Color.Lavender  
  34. }  
But the statement "strSql" varies from one form to another.

Any form has a function to create a "ChartArea", "Series", "Legend" and "Title" of the chart.

The following function creates a ChartArea in the form "frmGraph2": 
  1. private void DrawGraph() //Binding to Data with DataBind methods:  
  2. {  
  3.       //Define database query:  
  4.       string Family = "Single"//View Teachers who are single.  
  5.       strSql = "SELECT TeacherFamily, TeachingStuff, COUNT(*) AS Total FROM TeacherData " +  
  6.        "WHERE TeacherFamily = " + "'" + Family + "'" +  
  7.        "GROUP BY TeacherFamily, TeachingStuff " +  
  8.        "Order by TeacherFamily";  
  9.       //Create database connection:    
  10.       OleDbConnection DataCon = new OleDbConnection(strConnection);  
  11.       if (DataCon.State == ConnectionState.Open)  
  12.           DataCon.Close();  
  13.           DataCon.Open();  
  14.       //Create database command:      
  15.       OleDbCommand myCommand = new OleDbCommand(strSql, DataCon);  
  16.       //Set chart data source:  
  17.       MyChart.DataSource = myCommand.ExecuteReader();  
  18.       //Set series:  
  19.       MyChart.Series["TeacherSeries"].XValueMember = "TeachingStuff";  
  20.       MyChart.Series["TeacherSeries"].YValueMembers = "Total";  
  21.       //Binding to Data:  
  22.       MyChart.DataBind();  
  23.       myCommand = null;  
  24.       //Close connection:  
  25.       DataCon.Close();  
  26.  } 
Summary

It is desirable to beautify your data with a chart, so I invite you to go to the source file to read the code because I cannot list all the source code for the limited space available. If you have any idea for this code then please tell me. Thanks for the C# Corner team and thanks for all.


Recommended Free Ebook
Similar Articles