How to use Chart in asp.net or Barchat example in asp.net

Introduction:


In this example Iam Going to Explain  how to use Chart in asp.net(Frame work 4.0 onwards Chat availble in Data Section).




Description:When you want to display your data in graphical form, you can use Chart The Chart  can render an image that displays data in a variety of chart types. TheChart can render more than 30 types of charts, including all the types of charts that you might be familiar with from Microsoft Excel or other tools — area charts, bar charts, column charts, line charts, and pie charts, along with more specialized charts like stock charts.

Here iam giving Database table Design.


Year Jan Feb March April May
2014 10000 20000 1500 18000 20000
2015 3000 5000 7000 15000 10000

Here is Aspx Coding

<body>
    <form id="form1" runat="server">
        <div>
       


       <asp:Chart ID="Chart1" runat="server" BorderlineWidth="0" Width="550px">
        <Series>
        <asp:Series Name="Series1" XValueMember="Year" YValueMembers="Jan" LegendText="Jan"   IsValueShownAsLabel="false" ChartArea="ChartArea1" MarkerBorderColor="#DBDBDB"></asp:Series>

        <asp:Series Name="Series2" XValueMember="Year" YValueMembers="Feb" LegendText="Feb" IsValueShownAsLabel="false" ChartArea="ChartArea1" MarkerBorderColor="#DBDBDB"></asp:Series>

         <asp:Series Name="Series3" XValueMember="Year" YValueMembers="March" LegendText="March" IsValueShownAsLabel="false" ChartArea="ChartArea1" MarkerBorderColor="#DBDBDB"></asp:Series>

         <asp:Series Name="Series4" XValueMember="Year" YValueMembers="April" LegendText="April" IsValueShownAsLabel="false" ChartArea="ChartArea1" MarkerBorderColor="#DBDBDB"></asp:Series>

          <asp:Series Name="Series5" XValueMember="Year" YValueMembers="May" LegendText="May" IsValueShownAsLabel="false" ChartArea="ChartArea1" MarkerBorderColor="#DBDBDB"></asp:Series>
                
           </Series>
            <Legends>
            <asp:Legend Title="Month"></asp:Legend>
                </Legends>
                <Titles>
                    <asp:Title Docking="Bottom" Text="Percentage"></asp:Title>
                </Titles>
                <ChartAreas>
                    <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
                </ChartAreas>
            </asp:Chart>
        </div>
    </form>
</body>


Here Aspx.Cs coding

    SqlConnection con = new SqlConnection("Your Connection String");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
          
            FetchReportData();
        }
    }
    public void FetchReportData()
    {
        SqlCommand cmd = new SqlCommand("select * from Percentage",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
       
        Chart1.DataSource = ds;
        Chart1.DataBind();

    }

No comments: