Thursday, September 5, 2013

Asp.net Dynamic chart using Google Visualization chart API


Here is code for create dynamic chart in asp.net using Google Visualization chart API Default.aspx Page :
 
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!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></title>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            PieChart Example</h3>
        <asp:Literal ID="lt" runat="server"></asp:Literal>
        <div id="piechart" style="width: 900px; height: 500px;">
        </div>
        <h3>
            BAr chart Example</h3>
        <asp:Literal ID="lt1" runat="server"></asp:Literal>
        <div id="piechart14" style="width: 900px; height: 500px;">
        </div>
    </div>
    </form>
</body>
</html>


WebForm2.aspx.cs  page :

using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Text;

public partial class _Default :
System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        getPIEChart(); getLineChart();

    }
    public void getLineChart()
    {
        DataTable dt = GetDataTable();

        StringBuilder str = new StringBuilder();

        try
        {

            List dataList = new List();

            string cat = "";

            int val = 0;

            int val1 = 0;

            int val2 = 0;

            foreach (DataRow dr in dt.Rows)
            {
                cat = dr[0].ToString();

                val = Convert.ToInt32(dr[1]);

                val1 = Convert.ToInt32(dr[2]);

                val2 = Convert.ToInt32(dr[3]);

                dataList.Add(new Data(cat, val, val1, val2));

            } str.Append(@"");

            lt1.Text = str.ToString().TrimEnd(',');

            // Where "lt" is the Literal control .

        }
        catch (Exception e) { }

    }
    public void getPIEChart()
    {
        DataTable dt = GetDataTable();

        StringBuilder str = new StringBuilder();



        try
        {
            List dataList = new List();

            string cat = ""; int val = 0; int val1 = 0;

            int val2 = 0; foreach (DataRow dr in dt.Rows)
            {
                cat = dr[0].ToString();

                val = Convert.ToInt32(dr[1]);

                val1 = Convert.ToInt32(dr[2]);

                val2 = Convert.ToInt32(dr[3]);

                dataList.Add(new Data(cat, val, val1, val2));

            } str.Append(@"");

            lt.Text = str.ToString().TrimEnd(',');

            // Where "lt" is the Literal control .

        }
        catch (Exception e)

        { }
    }

 private static DataTable GetDataTable()

 {        DataTable dt = new DataTable();

 dt.Columns.Add("result");        dt.Columns.Add("count");

 dt.Columns.Add("count1");        dt.Columns.Add("count2");

 for (int i = 0; i = 10; i++)

 {            DataRow dr = dt.NewRow();

 dr["result"] = "test" + i;

 dr["count"] = i * 10;

 dr["count1"] = i * 20;

dr["count2"] = i * 10;            dt.Rows.Add(dr);        }        return dt;    }



    public class Data
    {

        public string ColumnName = "";

        public int Value = 0;

        public int Value1 = 0;

        public int Value2 = 0;

        public Data(string columnName, int value, int value1, int value2)
        {

            ColumnName = columnName;

            Value = value;

            Value1 = value1;

            Value2 = value2;

        }
    }
}


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment