How to create Chart in ASP.NET

Lets see how to create Chart in ASP.NET.

Step 1 : Your .aspx page like

<%
@ 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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
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="500px">
           
<Series>
               
<asp:Series Name="Education" XValueMember="State" YValueMembers="Education"
                   
IsVisibleInLegend="true" ChartType="Pie">
               
</asp:Series>
           
</Series>
           
<ChartAreas>
               
<asp:ChartArea Name="ChartArea1" Area3DStyle-Enable3D="true">
                   
<AxisX LineColor="DarkGray">
                       
<MajorGrid LineColor="LightGray" />
                   
</AxisX>
                   
<AxisY LineColor="DarkGray">
                       
<MajorGrid LineColor="LightGray" />
                    </AxisY> 
<
Area3DStyle Enable3D="True"></Area3DStyle>
               
</asp:ChartArea>
           
</ChartAreas>
           
<Legends>
               
<asp:Legend>
               
</asp:Legend>
           
</Legends>
       
</asp:Chart>
   
</div>
   
</form>
</
body>
</
html>

Step 2 : Your .cs page like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateChart();
        }
    }
 
    private void CreateChart()
    {
        var table = new DataTable();
        table.Columns.Add("State"typeof(string));
        table.Columns.Add("Education"typeof(long));
        table.Columns.Add("Lbl");
        var row = table.NewRow();
        row["State"] = "Gujarat";
        row["Education"] = 791;
        table.Rows.Add(row);
 
        row = table.NewRow();
        row["State"] = "Delhi";
        row["Education"] = 978;
        table.Rows.Add(row);
 
        row = table.NewRow();
        row["State"] = "Rajasthan";
        row["Education"] = 1262;
        table.Rows.Add(row);
 
        row = table.NewRow();
        row["State"] = "Panjab";
        row["Education"] = 1650;
       
        table.Rows.Add(row);
        row = table.NewRow();
        row["State"] = "Maharastra";
        row["Education"] = 2519;
       
        table.Rows.Add(row);
        row = table.NewRow();
        row["State"] = "Madyapradesh";
        row["Education"] = 6071;
        table.Rows.Add(row);
        Chart1.DataSource = table;
        Chart1.DataBind();
    }
}
Output like

chart.gif