I have created a GraphChart project using C# (2010), my project has the following six forms:
- frmChart: this is the main form to choose the type of the chart.
- frmGraph1: Binding the Chart to the Array from the DataGrid with the DataBindXY method.
- frmGraph2: Using the DataBindXY method as the previous form but binding the Chart to data from a MDB file and show the following chart:
- frmGraph3: Using the DataBindTable method to bind the Chart to the data from a MDB file.
- frmGraph4: Using the AddXY method to bind the Chart to the data from a MDB file.
- 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:
- private void CreateChartArea()
- {
-
- MyChart.ChartAreas.Clear();
-
- MyChartArea = new ChartArea();
-
- MyChart.ChartAreas.Add("MyChartArea");
-
-
- MyChart.BackColor = Color.LightGray;
- MyChart.ChartAreas["MyChartArea"].BackSecondaryColor = Color.Orange;
- MyChart.ChartAreas["MyChartArea"].BackHatchStyle = ChartHatchStyle.Wave;
-
-
- MyChart.ChartAreas["MyChartArea"].AxisX.MajorGrid.Enabled = false;
-
- MyChart.ChartAreas["MyChartArea"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
-
- MyChart.ChartAreas["MyChartArea"].AxisX.Maximum = 5;
-
- MyChart.ChartAreas["MyChartArea"].AxisX.LabelStyle.Font = new Font("Arial", 10, FontStyle.Bold);
-
- MyChart.ChartAreas["MyChartArea"].AxisX.LabelStyle.ForeColor = Color.Blue;
-
- MyChart.ChartAreas["MyChartArea"].AxisY.Title = "Number of students";
-
- MyChart.ChartAreas["MyChartArea"].AxisY.TitleFont = new Font("Arial", 10, FontStyle.Bold);
-
- MyChart.ChartAreas["MyChartArea"].AxisY.TitleForeColor = Color.Blue;
-
-
-
- }
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":
- private void DrawGraph()
- {
-
- string Family = "Single";
- strSql = "SELECT TeacherFamily, TeachingStuff, COUNT(*) AS Total FROM TeacherData " +
- "WHERE TeacherFamily = " + "'" + Family + "'" +
- "GROUP BY TeacherFamily, TeachingStuff " +
- "Order by TeacherFamily";
-
- OleDbConnection DataCon = new OleDbConnection(strConnection);
- if (DataCon.State == ConnectionState.Open)
- DataCon.Close();
- DataCon.Open();
-
- OleDbCommand myCommand = new OleDbCommand(strSql, DataCon);
-
- MyChart.DataSource = myCommand.ExecuteReader();
-
- MyChart.Series["TeacherSeries"].XValueMember = "TeachingStuff";
- MyChart.Series["TeacherSeries"].YValueMembers = "Total";
-
- MyChart.DataBind();
- myCommand = null;
-
- DataCon.Close();
- }
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.