Saturday, 3 January 2015

how to create chart control from database in asp.net using c#.

embed  or bind data to chart




Description:-

In this example we explain that how to create chart control from database in asp.net using c#. or displaying data in chart control in asp.net.

Here we explain how to create column 3D chart, pie chart, Bar chart, Line chart, area chart, pie chart, bubble chart, column chart etc… and displaying data in chart retrieve data from the database in 
asp.net using C#.

Sorting Row data in gridview Gridview Sorting 

How to handle Concurrency in Linq to Sql Concurrency Example 

Before implementing code of chart first you have to add two line of code web.config file to access chart control or display data in chart control in asp.net.the code are :


<system.web><httpHandlers>
  <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    validate="false" />
</httpHandlers></system.web>



Chart.aspx:-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="progrebar.aspx.cs" Inherits="progrebar" %>

<%@ 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 id="Head1" runat="server">
    <title>Column 3D Chart in Asp.Net Using C#.Net and VB.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       

        <asp:Chart ID="Chart1" runat="server" Height="235px" Width="366px">
            <Series>
                <asp:Series Name="Series1" XValueMember="Country" YValueMembers="Growth">
                </asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1">
                    <Area3DStyle Enable3D="True" />
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>
    </div>
    </form>
</body>
</html>

Chart.aspx.cs:-

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 progrebar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable objcountry = new DataTable();
        objcountry = GetDataForChart();
        BindDataToChartcontrol(objcountry);

    }
    ///
    /// This function will bind data to chart control
    ///
    public void BindDataToChartcontrol(DataTable objcountry)
    {
        Chart1.DataSource = objcountry;
        Chart1.DataBind();
    }
    ///
    /// This method will provide data
    /// In this methos you can fatch data from DB and pass it to chart control
    ///
    public DataTable GetDataForChart()
    {
        DataTable objcountry = new DataTable();

        objcountry.Columns.Add("Country", typeof(string));
        objcountry.Columns.Add("Growth", typeof(long));

        objcountry.Columns.Add("LabelValue");
        var datarow = objcountry.NewRow();
        datarow["Country"] = "India";
        datarow["Growth"] = 20;

        objcountry.Rows.Add(datarow);
        datarow = objcountry.NewRow();
        datarow["Country"] = "Pakistan";
        datarow["Growth"] = 25;
        objcountry.Rows.Add(datarow);

        datarow = objcountry.NewRow();
        datarow["Country"] = "Sri Lanka";
        datarow["Growth"] = 4;
        objcountry.Rows.Add(datarow);

        datarow = objcountry.NewRow();
        datarow["Country"] = "Africa";
        datarow["Growth"] = 3;
        objcountry.Rows.Add(datarow);

        return objcountry;
    }
}



0 comments:

Post a Comment