The following code can be called in the Page Load Event. the Code given here will be displayed from the (x,y) position (0,0). If the pie chart to be drawn from the top of the page, then the following code is OK. If it is need to be created in the center of the page or on the fly means, create a new aspx page and copy the code in the page load event and pass the aspx file as source for an image tag.
Add the following Namespaces
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
ArrayList aMonths;
ArrayList aProfits;
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
DrawBarChrt("Bar Chart", aMonths, aProfits);
}
//Creating Array for Months and Profits - to populate values for the Report
private void LoadData()
{
aMonths = new ArrayList();
aProfits = new ArrayList();
aMonths.Add("January");
aMonths.Add("February");
aMonths.Add("March");
aMonths.Add("April");
aMonths.Add("May");
aMonths.Add("June");
aMonths.Add("July");
aMonths.Add("August");
aMonths.Add("September");
aMonths.Add("October");
aMonths.Add("November");
aMonths.Add("December");
aProfits.Add(240500);
aProfits.Add(220950);
aProfits.Add(283500);
aProfits.Add(340000);
aProfits.Add(325750);
aProfits.Add(123456);
aProfits.Add(235621);
aProfits.Add(345235);
aProfits.Add(290451);
aProfits.Add(152345);
aProfits.Add(653456);
aProfits.Add(785620);
}
private void DrawBarChrt(String sTitle, ArrayList aMonth, ArrayList aProfit)
{
const int iBarWidth = 30;
const int iBarGap = 20;
const int iMaxHeight = 400;
const int iHeightSpace = 25;
const int iLegendSpace = 30;
const int iTitleSpace = 50;
int iMaxWidth = (iBarWidth + iBarGap) * aMonth.Count + iBarGap;
int iMaxColHeight = 0;
int iTotalHeight = iMaxHeight + iLegendSpace + iTitleSpace;
Bitmap objBitmap = new Bitmap(iMaxWidth, iTotalHeight);
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, iMaxWidth, iTotalHeight);
objGraphics.FillRectangle(new SolidBrush(Color.Lavender), 0, 0, iMaxWidth, iMaxHeight);
// find the maximum value
foreach (int iValue in aProfit)
{
if (iValue > iMaxColHeight)
iMaxColHeight = iValue;
}
int iBarX = iBarGap;
Double iCurrentHeight = 0;
SolidBrush objBrush = new SolidBrush(Color.FromArgb(70, 20, 20));
Font fontLegend = new Font("Arial", 11);
Font fontValues = new Font("Arial", 8);
Font fontTitle = new Font("Arial", 24);
// loop through and draw each bar
for (int iLoop = 0; iLoop <= aMonth.Count - 1; iLoop++)
{
iCurrentHeight = ((Convert.ToSingle(aProfit[iLoop]) / Convert.ToSingle(iMaxColHeight)) * Convert.ToSingle(iMaxHeight - iHeightSpace));
objGraphics.FillRectangle(objBrush, Convert.ToSingle(iBarX), Convert.ToSingle(iMaxHeight - iCurrentHeight), Convert.ToSingle(iBarWidth), Convert.ToSingle(iCurrentHeight));
objGraphics.DrawString(aMonth[iLoop].ToString().Substring(0, 3) , fontLegend, objBrush, Convert.ToSingle(iBarX), Convert.ToSingle(iMaxHeight));
objGraphics.DrawString(String.Format(aProfit[iLoop].ToString(), "#,###"), fontValues, objBrush, Convert.ToSingle(iBarX), Convert.ToSingle(iMaxHeight - iCurrentHeight - 15));
iBarX += (iBarGap + iBarWidth);
}
objGraphics.DrawString(sTitle, fontTitle, objBrush, Convert.ToSingle((iMaxWidth / 2) - sTitle.Length * 6), Convert.ToSingle(iMaxHeight + iLegendSpace));
//objBitmap.Save("C:\inetpub\wwwroot\graph.gif", ImageFormat.GIF)
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
objGraphics.Dispose();
objBitmap.Dispose();
}
The output is like this :