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. Try
  7. 'Define database query:
  8. strSql = "SELECT EducationalYear, StudentGrade, COUNT(*) AS Total FROM StudentData " _
  9. & "GROUP BY EducationalYear, StudentGrade " _
  10. & "ORDER BY EducationalYear; "
  11. 'Define Data Adapter:
  12. Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter(strSql, strConnection)
  13. tblStudents = New DataTable
  14. datAdp.Fill(tblStudents)
  15. 'Set DataSource of DataGrid:
  16. GridSchool.DataSource = tblStudents
  17. lblSample.Text = "Number of students from 2011 to 2014:"
  18. Catch ex As Exception
  19. MessageBox.Show(ex.ToString())
  20. End Try
  21. 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. 'Set BackColor to Chart and Graph area:
  9. MyChart.BackColor = Color.LightGray
  10. MyChart.ChartAreas("MyChartArea").BackSecondaryColor = Color.Green
  11. MyChart.ChartAreas("MyChartArea").BackGradientStyle = GradientStyle.TopBottom
  12. 'Hide vertical lines:
  13. MyChart.ChartAreas("MyChartArea").AxisX.MajorGrid.Enabled = False
  14. 'Set horizontal lines to Dash style:
  15. MyChart.ChartAreas("MyChartArea").AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash
  16. 'Set X-Axis to four Series:
  17. MyChart.ChartAreas("MyChartArea").AxisX.Maximum = 5
  18. 'Remove default Labels from X-Axis:
  19. MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.IsEndLabelVisible = False
  20. 'Display X-Axis labels with 30 degree:
  21. MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.Angle = 30
  22. 'Set X-Axis labels font:
  23. MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.Font = New Font("Arial", 10, FontStyle.Bold)
  24. 'Set X-Axis labels color to blue:
  25. MyChart.ChartAreas("MyChartArea").AxisX.LabelStyle.ForeColor = Color.Blue
  26. 'Set Y-Axis title:
  27. MyChart.ChartAreas("MyChartArea").AxisY.Title = "Number of students"
  28. 'Set Y-Axis title font:
  29. MyChart.ChartAreas("MyChartArea").AxisY.TitleFont = New Font("Arial", 10, FontStyle.Bold)
  30. 'Set Y-Axis title color:
  31. MyChart.ChartAreas("MyChartArea").AxisY.TitleForeColor = Color.Blue
  32. 'You can use ChartAreas index instead ChartAreas name:
  33. 'Example: MyChart.ChartAreas(0).BackColor = Color.Lavender
  34. 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.