Graphical Report in Windows Application Using Chart Control

In Visual Studio 2010, Microsoft added a Chart Control to show data in graphical format. Using a Chart Control you can very simply show complex statistical data.

In this example I will implement a Chart Control in a Windows application.

Step 1 : Create an Employee table and add some data for the report.



Step 2 : Now, create a new Windows Forms Application.

Step 3 : Drag and drop the Chart Control to the form as shown in the screen below.



Step 4 : Add the following code to the form.

Namespace

using System.Data.SqlClient; 

using System.Web.UI.DataVisualization.Charting;

Code On Form Load Event

SqlConnection conn = new SqlConnection("Server=LENOVO-PC;Database=Abhinav;User Id=sa;Password=****");

            string qry = "select Name, Salary from employee";

            SqlDataAdapter da = new SqlDataAdapter(qry, conn);

            DataSet ds = new DataSet();

            da.Fill(ds,"Salary");

            chart1.DataSource = ds.Tables["Salary"];

 

            chart1.Series["Series1"].XValueMember = "NAME";

            chart1.Series["Series1"].YValueMembers = "SALARY";

      

            this.chart1.Titles.Add("EMPLOYEE SALARY");   // Set the chart title

            chart1.Series["Series1"].ChartType = SeriesChartType.Pie;  // Set chart type like Bar chart, Pie chart

            chart1.Series["Series1"].IsValueShownAsLabel = true;  // To show chart value

On Button Click to Save Chart

this.chart1.SaveImage("D:\\chart1.Jpeg"ChartImageFormat.Jpeg);

            MessageBox.Show("Chart Saved Successfully"Application.ProductName, 
            MessageBoxButtons
.OK, MessageBoxIcon.Information);

Step 5 : Now run the application, you will see a graphical report like this.



You can download the complete attached source code.

I hope you liked it.


Similar Articles