Resources » Code Snippets » ASP.NET WebForms

Draw Pie Chart In Asp.Net


Posted Date: 01-Apr-2010  Last Updated:   Category: ASP.NET WebForms    
Author: Member Level: Gold    Points: 8


Draw Pie Chart In Asp.Net



How To Draw Simple Pie Chart In Asp.Net



1.Find Attached WebChart.dll
Default2.aspx Page

[%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %]

[%@ Register Assembly="WebChart" Namespace="WebChart" TagPrefix="Web" %]
[!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]Untitled Page[/title]

[/head]
[body]
[form id="form1" runat="server"]
[div]
[Web:ChartControl ID="ChartControl1" runat="server" BorderStyle="Outset" BorderWidth="5px"
Height="400" Width="350" GridLines="none" Legend-Position="Bottom"]
[/Web:ChartControl]
[/div]
[/form]
[/body]
[/html]



Default2.aspx.cs Page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebChart;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateChart();
}
void CreateChart()
{
PieChart chart = new PieChart();
chart.DataSource = GetDataSet().Tables[0].DefaultView;
chart.DataXValueField = "Title";
chart.DataYValueField = "Price";
chart.DataLabels.Visible = true;
chart.DataLabels.ForeColor = System.Drawing.Color.Blue;
chart.Shadow.Visible = true;
chart.DataBind();
chart.Explosion = 10;
ChartControl1.Charts.Add(chart);
ChartControl1.RedrawChart();
}

DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable table = ds.Tables.Add("My Table");
table.Columns.Add(new DataColumn("Title"));
table.Columns.Add(new DataColumn("Price", typeof(int)));

Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
DataRow row = table.NewRow();
row["Title"] = "Title:" + i.ToString();
row["Price"] = rnd.Next(1, 100);
table.Rows.Add(row);
}
return ds;
}
}



Did you like this resource? Share it with your friends and show your love!

Responses to "Draw Pie Chart In Asp.Net"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Gopi A
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India