News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Nov 30 2014 8:23AM     Anand Krish
  5 Comments    12421 Views  
Here Anand Krish provided a short tutorial with example, how to create Chart Control in Windows Forms applicaton. In Windows Forms application, there is already predefined control is availabe ( Chart ).
Using the Chart Control we can draw Bar chart, pie Chart, line chart and Etc.
Database
For creating a chart, First have to create a table in Microsoft SQL Server Database as shown below.
Creating a table
When creating a table make Identity Specification as true for column 'ID'.

Set the table name as shown below.
Create a new windows forms application, do the steps given below to create a chart
Form Design
Adding a Chart to the Windows Form
Firstly you need to Drag and drop the Chart Control to the Windows Form from the Visual Studio ToolBox as shown below.

Namespaces
You will need to import the following namespace.
C#

using System.Data.SqlClient;
using System.Windows.Forms.DataVisualization.Charting;

VB

Imports System.Data.SqlClient
Imports System.Windows.Forms.DataVisualization.Charting

To create a chart in Windows Application
Below is the sample code for creating a Chart from Database using DataSet in C#.
C#

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindChart();
        }

        private void BindChart()
        {
            string conString = @"server=****; database=****; uid=***; password=****; ";
            SqlConnection conn = new SqlConnection(conString);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Employee", conn);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds, "EmployeeExperience");
                chart1.DataSource = ds.Tables["EmployeeExperience"];
                chart1.Series["Series1"].XValueMember = "EmployeeName";
                chart1.Series["Series1"].YValueMembers = "EmployeeExperience";
                // Set the chart title
                this.chart1.Titles.Add("EMPLOYEE EXPERIENCE");   
                // Set chart type like Bar chart, Pie chart 
                chart1.Series["Series1"].ChartType = SeriesChartType.Pie;     
                // To show chart value           
                chart1.Series["Series1"].IsValueShownAsLabel = true;   
            }
            catch (Exception ex)
            {
                //Exception Message
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
 }

VB

Public Partial Class Form1

Inherits Form

Public Sub New()
       InitializeComponent()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs)
       BindChart()
End Sub

Private Sub BindChart()

     Dim conString As String = "server=****; database=****; uid=***; password=****;
     Dim conn As New SqlConnection(conString)
     Dim ds As New DataSet()
     Try
    conn.Open()
    Dim cmd As New SqlCommand("SELECT * FROM Employee", conn)
    Dim da As New SqlDataAdapter()
    da.SelectCommand = cmd
    da.Fill(ds, "EmployeeExperience")
    chart1.DataSource = ds.Tables("EmployeeExperience")
    chart1.Series("Series1").XValueMember = "EmployeeName"
    chart1.Series("Series1").YValueMembers = "EmployeeExperience"
    ' Set the chart title
    Me.chart1.Titles.Add("EMPLOYEE EXPERIENCE")
    ' Set chart type like Bar chart, Pie chart 
    chart1.Series("Series1").ChartType = SeriesChartType.Pie
    ' To show chart value           
    chart1.Series("Series1").IsValueShownAsLabel = True
'Exception Message
     Catch ex As Exception
     Finally
    conn.Close()
    conn.Dispose()
     End Try

 End Sub

End 

To create BarChart
By changing the SeriesChartType properties we can create different charts.
C#

 chart1.Series["Series1"].ChartType = SeriesChartType.Column;

VB

chart1.Series("Series1").ChartType = SeriesChartType.Column

To create Line Chart
C#

chart1.Series["Series1"].ChartType = SeriesChartType.Line;

VB

chart1.Series("Series1").ChartType = SeriesChartType.Line

Result
Pie Chart
Bar Chart
Line Chart
Column Chart
Pyramid Chart
I hope this page will helps to create chart in windows Forms Application in C#/VB. Thanks.
BackToTop
Comments


AnonymousUser
Sep 11 2015 5:26AM
i want a line chart using database value in windows application c
Vishwanath
Sep 12 2015 10:47PM
Hi, You can draw the line chart by changing the chart type in the above code.

Eg: [ For Line Chart ]
chart1.Series["Series1"].ChartType = SeriesChartType.Line;

Already article says to draw chart from database value. I hope this helps.
John Prathip
Jan 12 2016 6:05AM
hi i want a point chart with Trendline power options in windows application
John Prathip
Jan 12 2016 6:05AM
hi i want a point chart with Trendline power options in windows application
manimani
Jul 20 2016 7:57AM
hi I want a bar chart and area chart in a single chart in windows application

 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: