How to Use Chart Control in VB.NET 2010

This article shows how to use the Chart control in VB.NET.

I have created a GraphChart project using VB.NET (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 an array from a DataGrid with (DataBindXY) and showing the following chart:

    chart

  3. frmGraph2: Using the DataBindXY method the same as the previous form but by binding the chart to data from a MDB file.

  4. frmGraph3: Using the DataBindTable method by binding the chart to data from a MDB file.

  5. frmGraph4: Using the AddXY method by binding the chart to data from a MDB file.

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

About the Code

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

  1. Private Sub LoadSchoolData()  
  2.         'Following lines to connect with access database file 'MySchool.mdb':  
  3.         MyDataFile = Application.StartupPath & "\DataFile\MySchool.mdb"  
  4.         strConnection = "provider=microsoft.jet.oledb.4.0;data source=" _  
  5.          & MyDataFile & ";" & "Jet OLEDB:Database Password=" & MyPass & ";"  
  6.   
  7.         Try  
  8.             'Define database query:  
  9.             strSql = "SELECT EducationalYear, StudentGrade, COUNT(*) AS Total FROM StudentData " _  
  10.             & "GROUP BY EducationalYear, StudentGrade " _  
  11.             & "ORDER BY EducationalYear; "  
  12.   
  13.             'Define Data Adapter:  
  14.             Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter(strSql, strConnection)  
  15.             tblStudents = New DataTable  
  16.             datAdp.Fill(tblStudents)  
  17.   
  18.             'Set DataSource of DataGrid:  
  19.             GridSchool.DataSource = tblStudents  
  20.             lblSample.Text = "Number of students from 2011 to 2014:"  
  21.         Catch ex As Exception  
  22.             MessageBox.Show(ex.ToString())  
  23.         End Try  
  24.     End Sub  


But the statement "strSql" varies from one form to another.

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

The following procedure creates a ChartArea in the form "frmGraph1":
  1. Private Sub CreateChartArea()  
  2.         'Clear ChartAreas if found:  
  3.         MyChart.ChartAreas.Clear()  
  4.         'Create new ChartArea  
  5.         MyChartArea = New ChartArea()  
  6.         'Add ChartArea:  
  7.         MyChart.ChartAreas.Add("MyChartArea")  
  8.   
  9.         'Set BackColor to Chart and Graph area:  
  10.         MyChart.BackColor = Color.LightGray  
  11.         MyChart.ChartAreas("MyChartArea").BackSecondaryColor = Color.Green  
  12.         MyChart.ChartAreas("MyChartArea").BackGradientStyle = GradientStyle.TopBottom  
  13.         'Hide vertical lines:  
  14.         MyChart.ChartAreas("MyChartArea").AxisX.MajorGrid.Enabled = False  
  15.         'Set horizontal lines to Dash style:  
  16.         MyChart.ChartAreas("MyChartArea").AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash  
  17.         'Set X-Axis to four Series:  
  18.         MyChart.ChartAreas("MyChartArea").AxisX.Maximum = 5  
  19.         'Remove default Labels from X-Axis:  
  20.         MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.IsEndLabelVisible = False  
  21.         'Display X-Axis labels with 30 degree:  
  22.         MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.Angle = 30  
  23.         'Set X-Axis labels font:  
  24.         MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.Font = New Font("Arial", 10, FontStyle.Bold)  
  25.         'Set X-Axis labels color to blue:  
  26.         MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.ForeColor = Color.Blue  
  27.         'Set Y-Axis title:  
  28.         MyChart.ChartAreas("MyChartArea").AxisY.Title = "Number of students"  
  29.         'Set Y-Axis title font:  
  30.         MyChart.ChartAreas("MyChartArea").AxisY.TitleFont = New Font("Arial", 10, FontStyle.Bold)  
  31.         'Set Y-Axis title color:  
  32.         MyChart.ChartAreas("MyChartArea").AxisY.TitleForeColor = Color.Blue  
  33.   
  34.         'You can use ChartAreas index instead ChartAreas name:  
  35.         'Example: MyChart.ChartAreas(0).BackColor = Color.Lavender  
  36. End Sub  
You can read the procedure "DrawGraph" that draws the chart in all the forms.

The following procedure draws the chart in the form "frmGraph4".
  1. Private Sub DrawGraph() 'Binding to Data with (AddXY) methods:  
  2.         'Dim RowNum As DataRow  
  3.         For Each RowNum As DataRow In tblStudents.Rows  
  4.             'Add a new series  
  5.             Dim SeriesName As String = RowNum("StudentGrade").ToString()  
  6.             MyChart.Series.Add(SeriesName)  
  7.             'Set Graph Type as Column:  
  8.             MyChart.Series(SeriesName).ChartType = SeriesChartType.Column  
  9.             'Set Graph shape as Cylinder:  
  10.             MyChart.Series(SeriesName)("DrawingStyle") = "Cylinder"  
  11.             'Add Y value  
  12.             For COlNum As Integer = 1 To (tblStudents.Columns.Count) - 1  
  13.                 Dim ColName As String = tblStudents.Columns(COlNum).ColumnName  
  14.                 Dim YValue As Integer = Convert.ToInt32(RowNum(ColName))  
  15.                 MyChart.Series(SeriesName).Points.AddXY(ColName, YValue)  
  16.             Next COlNum  
  17.         Next RowNum  
  18.         'Set Graph color to Series  
  19.         MyChart.Series(0).Color = Color.Blue  
  20.         MyChart.Series(1).Color = Color.Yellow  
  21.         MyChart.Series(2).Color = Color.Red  
  22. End Sub  
Summary

It is desirable to beautify your data with charts, 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 about this code, please tell me. Thanks for the C# Corner team and thanks for all.