Introduction
In this article, I describe how to create a chart in ASP.NET which take data from back end and show it's graph on the front end.
First of all create a table in your SQL Server using the following command:
- create table emp(empName varchar(20),workinhDate date,Workinghour varchar(20))
- go
- insert into mcnemp
- select 'd','1/12/2013','8'union all
- select 'd','1/15/2013','6'union all
- select 'd','2/18/2013','7'union all
- select 'd','2/20/2013','4'union all
- select 'd','2/26/2013','8'union all
- select 'd','3/11/2013','5'union all
- select 'd','3/16/2013','10'union all
- select 'd','4/18/2013','20'union all
- select 'd','5/20/2013','11'union all
- select 'd','6/19/2013','6'
- go
- select * from mcnemp
Output

Then create an ASP.NET website using the following procedure:
- Open Visual Studio
- Click on "File"
- Select "New" then select "website"
- Now a window appears; select "ASP.Net Empty Web Site"
- Provide an appropriate name and location
- Click "Ok"
- Right-click on the name of your site in Solution Explorer
- Now select "Add"then select "Add new Item"
- Select "webpage" and provide whatever name you wish
- Click "Ok"
Now use the following code in your .aspx page for adding the Chart Control:
- <asp:Chart ID="Chart1" runat="server" Width="488px">
- <series>
- <asp:Series Name="Series1" XValueMember="0" YValueMembers="2">
- </asp:Series>
- </series>
- <chartareas>
- <asp:ChartArea Name="ChartArea1">
- </asp:ChartArea>
- </chartareas>
- </asp:Chart>
- protected void Page_Load(object sender, EventArgs e)
- {
- DataSet ds = new DataSet();
- SqlDataAdapter da = new SqlDataAdapter("select * from mcnemp", con);
- da.Fill(ds);
- Chart1.DataSource = ds;
- }
Complete Code of the .aspx page
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Chart ID="Chart1" runat="server" Width="488px">
- <series>
- <asp:Series Name="Series1" XValueMember="0" YValueMembers="2">
- </asp:Series>
- </series>
- <chartareas>
- <asp:ChartArea Name="ChartArea1">
- </asp:ChartArea>
- </chartareas>
- </asp:Chart>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- public partial class _Default : System.Web.UI.Page
- {
- SqlConnection con = new SqlConnection("server=.;database=dd;uid=sa;pwd=wintellect@1");
- protected void Page_Load(object sender, EventArgs e)
- {
- DataSet ds = new DataSet();
- SqlDataAdapter da = new SqlDataAdapter("select * from mcnemp", con);
- da.Fill(ds);
- Chart1.DataSource = ds;
- }
- }
Now run your project by clicking F5.
Output

Summary
In this article, I described how to create a chart in ASP.NET. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.


srinivas prabhuPosted Jan 4, 2022, 11:37 PM EST
It is not showing the chart in ASP.NET 2.0
Ansari TahirPosted May 31, 2018, 2:12 AM EDT
Can we Set Every stack have different Color..??
Aleksandra NikolovaPosted Oct 9, 2016, 10:57 PM EDT
Very helpful post! You can check shieldui aspnet chart control https://demos.shieldui.com/aspnet/all-charts
Satya RathorePosted Apr 6, 2015, 10:13 PM EDT
Nice post. Thanks for post. there is some more details about chart and basic implementation also http://www.getmscode.com/2015/04/graph-control-in-aspnet.html