Draw Pie chart in asp.net


Code for Draw Pie chart in asp.net

Draw Pie chart in asp.net



Here is the Code for Draw Pie chart in Asp.net

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 System.Drawing;
using System.Drawing.Imaging;

public partial class Chart : System.Web.UI.Page
{
public Bitmap myImage;
public Graphics g;
public double[] p;
public string[] GLabel;
public Brush[] myBrushes;
protected void Page_Load(object sender, EventArgs e)
{
setvalues();
initialiseGraphics();
g.Clear(Color.WhiteSmoke);
drawPieChart(g);
myImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
public void setvalues()
{
int size = 6;

DefineArrays(size);

p[0] = 20.12;
p[1] = 10.12;
p[2] = 16.40;
p[3] = 15.29;
p[4] = 12.12;
p[5] = 17.21;

GLabel[0] = "A";
GLabel[1] = "B";
GLabel[2] = "C";
GLabel[3] = "D";
GLabel[4] = "E";
GLabel[5] = "F";

}

public void DefineArrays(int size)
{
p = new double[size];
GLabel = new string[size];
myBrushes = new Brush[20];

}

private void initialiseGraphics()
{
try
{
myImage = new Bitmap(500, 300, PixelFormat.Format32bppRgb);
g = Graphics.FromImage(myImage);
createBrushes();
}
catch (Exception ex)
{
throw ex;
}
}
public void createBrushes()
{
try
{
//Method to create brushes of specific colours

myBrushes[0] = new SolidBrush(Color.Red);
myBrushes[1] = new SolidBrush(Color.Blue);
myBrushes[2] = new SolidBrush(Color.Yellow);
myBrushes[3] = new SolidBrush(Color.Green);
myBrushes[4] = new SolidBrush(Color.Tan);
myBrushes[5] = new SolidBrush(Color.SkyBlue);
myBrushes[6] = new SolidBrush(Color.Orange);
myBrushes[7] = new SolidBrush(Color.PaleGreen);
myBrushes[8] = new SolidBrush(Color.Silver);
myBrushes[9] = new SolidBrush(Color.SeaGreen);
myBrushes[10] = new SolidBrush(Color.White);
}
catch (Exception ex)
{
throw ex;
}
}
public void drawPieChart(Graphics g)
{
try
{
int i;
double total = 0;
double percentage;
double angleSoFar = 0.0;

for (i = 0; i < p.Length; i++)
{
total = total + p[i];
}
for (i = 0; i < p.Length; i++)
{
percentage = double.Parse(p[i].ToString()) / double.Parse(total.ToString()) * 360;

g.FillPie(myBrushes[i], 25, 25, 250, 250, float.Parse(angleSoFar.ToString()), float.Parse(percentage.ToString()));

angleSoFar = angleSoFar + percentage;

g.FillRectangle(myBrushes[i], 350, 25 + (i * 30), 15, 15);

g.DrawString("Town " + GLabel[i], new Font("Verdana", 8, FontStyle.Bold), Brushes.Brown, 390, 15 + (i * 30) + 10);
}


}
catch (Exception ex)
{
throw ex;
}
}
}


Related Articles

More articles: Pie chart ASP.Net Pie chart Generate pie chart Draw chart

Comments

Author: shubhang sahai mathur05 May 2010 Member Level: Silver   Points : 0

thanks dear
but can this code is attached with direct excel sheet.

Author: Devendra Singh Rathore06 May 2010 Member Level: Silver   Points : 1

Hi Shubhang,

In this example the chart is generated in the webpage using Bitmap Image.

If you want to generate this chart in your excel sheet then you have to
convert this code according to the Excel Code format by which you can export
this chart image into Excel sheet.

Guest Author: Venkateswaran14 Oct 2013

Hi, I am creating pie chart based on you, now I want to create pie chart with percentage.. Can you give any solution for my requirements?



  • 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:
    Email: