PROWAREtech

articles » archived » asp-net » pie-chart

ASP.NET: Pie Chart

An example pie chart using ASPX Web Forms (.NET Framework).

This is an ASPX page that is outputting a pie chart JPEG image. Note that for production, it would be best that this code is in a code behind file so that it is precompiled.

<%@ Page Language="VB" ContentType="image/jpeg" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%
	Response.Clear()
	Dim height As Integer = IIf(IsNumeric(Request.QueryString("height")), _
		Request.QueryString("height"), 300)
	Dim width As Integer = IIf(IsNumeric(Request.QueryString("width")), _
		Request.QueryString("width"), 300)
	Dim r As New Random
	Dim bmp As New Bitmap(width, height, PixelFormat.Format24bppRgb)
	Dim g As Graphics = Graphics.FromImage(bmp)
	g.SmoothingMode = SmoothingMode.AntiAlias
	g.Clear(Color.White)
	Dim Values(5) As Integer
	Dim SumOfValues As Integer = 0
	Dim i As Integer
	For i = 0 To 5
		Values(i) = r.Next(10, 1001)
		SumOfValues += Values(i)
	Next
	Dim rect As New Rectangle(0, 0, width - 50, height - 50)
	Dim sweep As Single
	Dim angle As Single
	Dim key As Integer = 10
	Dim hPen As New Pen(Color.Black, 1)
	For Each i In Values
		angle += sweep
		sweep = Convert.ToSingle(i) / SumOfValues * 360
		Dim hBrush As New SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)))
		g.FillRectangle(hBrush, width - 45, key, 10, 13)
		g.DrawRectangle(hPen, width - 45, key, 10, 13)
		g.DrawString(i.ToString(), New Font("Arial", 8, FontStyle.Regular), _
			SystemBrushes.WindowText, width - 35, key)
		g.FillPie(hBrush, rect, angle, sweep)
		key += 13
	Next
	g.DrawEllipse(hPen, rect)
	bmp.Save(Response.OutputStream, ImageFormat.Jpeg)
	g.Dispose()
	bmp.Dispose()
%>
<%@ Page Language="C#" ContentType="image/jpeg" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%
	Response.Clear();
	int height, width;
	if (!int.TryParse(Request.QueryString["height"], out height))
		height = 300;
	if (!int.TryParse(Request.QueryString["width"], out width))
		width = 300;
	Random r = new Random();
	Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
	Graphics g = Graphics.FromImage(bmp);
	g.SmoothingMode = SmoothingMode.AntiAlias;
	g.Clear(Color.White);
	int[] Values = { 0, 0, 0, 0, 0, 0 };
	int SumOfValues = 0;
	for(int i = 0; i <= 5; i++)
	{
		Values[i] = r.Next(10, 1001);
		SumOfValues += Values[i];
	}
	Rectangle rect = new Rectangle(0, 0, width - 50, height - 50);
	float sweep = 0;
	float angle = 0;
	int key = 10;
	Pen hPen = new Pen(Color.Black, 1);
	foreach(int i in Values)
	{
		angle += sweep;
		sweep = (float)i / SumOfValues * 360;
		SolidBrush hBrush = new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)));
		g.FillRectangle(hBrush, width - 45, key, 10, 13);
		g.DrawRectangle(hPen, width - 45, key, 10, 13);
		g.DrawString(i.ToString(), new Font("Arial", 8, FontStyle.Regular), 
			SystemBrushes.WindowText, width - 35, key);
		g.FillPie(hBrush, rect, angle, sweep);
		key += 13;
	}
	g.DrawEllipse(hPen, rect);
	bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
	g.Dispose();
	bmp.Dispose();
%>

PieChart2: <img style="border:1px solid gray;" src="/_piechart2.aspx?values=Roland:0.100;Aaron:0.50;John:0.75;David:0.382;William:2.133;Felix:1.05;" />

<%@ Page Language="C#" ContentType="image/jpeg" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%
	// PIECHART2.ASPX
	Response.Clear();
	int height = 300;
	int width = 300 + 100;
	Random r = new Random();
	using(Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
	{
		using(Graphics g = Graphics.FromImage(bmp))
		{
			g.SmoothingMode = SmoothingMode.AntiAlias;
			g.Clear(Color.White);
			string values = Request.QueryString["values"];
			if(values != null)
			{
				string[] sections = values.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries);
				double SumOfValues = 0;
				for(int i = 0; i < sections.Length; i++)
				{
					string[] stmp = sections[i].Split(':');
					SumOfValues += float.Parse(stmp[1]);
				}
				Rectangle rect = new Rectangle(0, 20, width - 100 - 50, height - 50);
				float sweep = 0;
				float angle = 0;
				int key = 10;
				Pen hPen = new Pen(Color.Black, 1);
				Font font = new Font("Arial", 8, FontStyle.Regular);
				for(int i = 0; i < sections.Length; i++)
				{
					angle += sweep;
					string[] stmp = sections[i].Split(':');
					sweep = (float)(float.Parse(stmp[1]) / SumOfValues * 360.0F);
					Brush hBrush = new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)));
					g.FillRectangle(hBrush, width - 100 - 45, key, 10, font.Height);
					g.DrawRectangle(hPen, width - 100 - 45, key, 10, font.Height);
					g.DrawString(stmp[0] + ": " + stmp[1], font, Brushes.Black, width - 100 - 35, key);
					g.FillPie(hBrush, rect, angle, sweep);
					key += font.Height;
				}
				g.DrawEllipse(hPen, rect);
			}
			bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
		}
	}
%>

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT