.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Pie Chart in asp.net

Posted By:Nikhil Kumar       Posted Date: February 27, 2010    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  

This code shows you how to create pie chart in asp.net. This code is very simple to use
 

private Bitmap bmpImg;
private Graphics g;
private int[] p = new int[5] { 10, 6, 25, 8, 35 };

private string[] towns = { "A", "B", "C", "D","E" };
private Brush[] myBrushes = new Brush[5];

protected void Page_Load(object sender, EventArgs e)
{
try
{
bmpImg = new Bitmap(400, 300, PixelFormat.Format32bppRgb);

Graphics g = Graphics.FromImage(bmpImg);
g.Clear(Color.White);
Pen redPen = new Pen(Color.Red);
createBrushes();
DrawPieChart(g);
}
catch (Exception ex)
{
ex.Message.ToString();
}
}

private void createBrushes()
{
try
{
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.Indigo);
myBrushes[5] = new SolidBrush(Color.Brown);
myBrushes[6] = new SolidBrush(Color.Chocolate);
}
catch (Exception ex)
{
ex.Message.ToString();
}
}

private void DrawPieChart(Graphics g)
{
int i ;
int iTotal = 0;
double dPercentage;
double dAngleSoFar =0.0;
for (i = 0; i < p.Length ; i++)
iTotal += p[i];
for (i = 0; i < p.Length ; i++)
{
dPercentage = (double)p[i] / iTotal * 360;
// g.FillPie(myBrushes[i],25,25,250,250,Convert.ToInt32(dAngleSoFar),Convert.ToInt32(dPercentage));
g.FillPie(myBrushes[i], 15, 15, 180, 180, Convert.ToInt32(dAngleSoFar), Convert.ToInt32(dPercentage));
dAngleSoFar += dPercentage;
g.FillRectangle(myBrushes[i], 250, 25 + (i * 30), 15, 15);
g.DrawString("Town "+towns[i].ToString(),new Font("Verdana", 8, FontStyle.Bold), Brushes.Brown, 250, 25 + (i * 30) + 15);
}


}

}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend