Monday, July 26, 2004

How to create pie charts in web applns

How to create pie charts in web applns









Pie Chart in ASP.net



<%@ Import Namespace="System.Drawing"%>
<%@ Import Namespace="System.Drawing.Imaging"%>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script language="C#" runat="server">

void Page_Load(Object sender, EventArgs e)
{
// Since we are outputting a Jpeg, set the ContentType appropriately
Response.ContentType = "image/jpeg";

// Create a Bitmap instance that's 468x60, and a
Graphics instance

const int width = 300, height = 300;

Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);

// Create a black background for the border
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0,
width, height);

Draw3DPieChart(ref objGraphics);

// Save the image to a file
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

// clean up...
objGraphics.Dispose();
objBitmap.Dispose();
}

// Draws a 3D pie chart where ever slice is 45 degrees in value
void Draw3DPieChart(ref Graphics objGraphics)
{
int iLoop, iLoop2;

// Create location and size of ellipse.
int x = 50;
int y = 20;
int width = 200;
int height = 100;

// Create start and sweep angles.
int startAngle = 0;
int sweepAngle = 45;
SolidBrush oibjBrush = new SolidBrush(Color.Aqua);

Random rand = new Random();
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;

//Loop through 180 back around to 135 degress so it gets drawn
// correctly.

for( iLoop = 0; iLoop <= 315; iLoop += 45)
{
startAngle = (iLoop + 180) % 360;
objBrush.Color = Color.FromArgb(rand.Next(255), rand.Next(255),
rand.Next(255));

// On degrees from 0 to 180 draw 10 Hatched brush slices to show
// depth

if((startAngle < 135) || (startAngle == 180))
{
for(iLoop2 = 0; iLoop2 < 10; iLoop2++)
objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
objBrush.Color), x,
y + iLoop2, width, height, startAngle, sweepAngle);
}

// Displace this pie slice from pie.
if(startAngle == 135)
{
// Show Depth
for(iLoop2 = 0; iLoop2 < 10; iLoop2++)
objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50,
objBrush.Color), x - 30,
y + iLoop2 + 15, width, height, startAngle,
sweepAngle);

objGraphics.FillPie(objBrush, x - 30, y + 15,
width, height, startAngle, sweepAngle);
}
else // Draw normally
objGraphics.FillPie(objBrush, x, y, width,
height, startAngle, sweepAngle);
}
}
</script>






Mitesh Mehta
Microsoft Certified Professional


No comments: