I created a pie chart in an aspx. However, how can I display the data in the
piechart itself in the right location? The code is as follows:
(Please see the definition of float x and float y)
private void CreatePieChart_a(string pStr_SQL, string
dataColumnName,string labelColumnName,string title, int width)
{
string connString = Cls_DManager.GetOleDbConnection().ToString();
OleDbConnection myConnection = new OleDbConnection(connString);
myConnection.Open();
//string pStr_SQL = "SELECT DISTINCT [" + dataColumnName + "], [" +
labelColumnName + "] FROM " + tableName;
OleDbCommand myCommand = new OleDbCommand(pStr_SQL, myConnection);
// 3. Create/Populate the DataSet
DataSet ds = new DataSet();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand);
myAdapter.Fill(ds);
// close the connection
myConnection.Close();
// find the total of the numeric data
float total = 0.0F, tmp;
int iLoop;
for (iLoop=0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
{
tmp = Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]);
total += tmp;
}
// we need to create fonts for our legend and title
Font fontLegend = new Font("Arial", 10),
fontTitle = new Font("Arial", 15, FontStyle.Bold);
// We need to create a legend and title, how big do these need to be?
// Also, we need to resize the height for the pie chart, respective to the
// height of the legend and title
const int bufferSpace = 15;
int legendHeight = fontLegend.Height * (ds.Tables[0].Rows.Count+1) +
bufferSpace;
int titleHeight = fontTitle.Height + bufferSpace;
int height = width + legendHeight + titleHeight + bufferSpace;
int pieHeight = width; // maintain a one-to-one ratio
// Create a rectange for drawing our pie
Rectangle pieRect = new Rectangle(0, titleHeight, width, pieHeight);
// Create our pie chart, start by creating an ArrayList of colors
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255),
rnd.Next(255))));
float currentDegree = 0.0F;
// Create a Bitmap instance
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
SolidBrush blackBrush = new SolidBrush(Color.Black);
int xxx=0;
// Put a white backround in
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width,
height);
for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
{
objGraphics.FillPie((SolidBrush) colors[iLoop], pieRect, currentDegree,
Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total *
360);
// increment the currentDegree
currentDegree +=
Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total * 360;
string drawString = ds.Tables[0].Rows[iLoop][labelColumnName].ToString();
// Create font and brush.
Font drawFont = new Font("Arial", 9);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
//////////////////////////////////////////////////////////////////////
float x = currentDegree;
float y =Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) /
total * 360;
StringFormat drawFormat = new StringFormat();
objGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
//////////////////////////////////////////////////////////////////////
}
// Create the title, centered
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
objGraphics.DrawString(title, fontTitle, blackBrush,
new Rectangle(0, 0, width, titleHeight), stringFormat);
// Create the legend
objGraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height -
legendHeight, width, legendHeight);
for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++)
{
objGraphics.FillRectangle((SolidBrush) colors[iLoop], 5,
height - legendHeight + fontLegend.Height * iLoop + 5, 10, 10);
objGraphics.DrawString(((string)
ds.Tables[0].Rows[iLoop][labelColumnName].ToString()) + " - " +
Convert.ToString(ds.Tables[0].Rows[iLoop][dataColumnName]), fontLegend,
blackBrush,
20, height - legendHeight + fontLegend.Height * iLoop + 1);
}
// display the total
objGraphics.DrawString("Total Inspections: " + Convert.ToString(total),
fontLegend, blackBrush,
5, height - fontLegend.Height - 5);
// Since we are outputting a Jpeg, set the ContentType appropriately
Response.ContentType = "image/jpeg";
// Save the image to a file
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
// clean up...
objGraphics.Dispose();
objBitmap.Dispose();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
} 1 1808
I worked with someone who used geometry and trig calculations to get the pie
charts with the proper labels (and clickable to boot). I would google and see
if anyone has done this type of work in an open source project. The asp.net
site has a report writer application for download that might have this type
of algorithm.
---
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
"rkbnair" wrote: I created a pie chart in an aspx. However, how can I display the data in the piechart itself in the right location? The code is as follows: (Please see the definition of float x and float y)
private void CreatePieChart_a(string pStr_SQL, string dataColumnName,string labelColumnName,string title, int width) { string connString = Cls_DManager.GetOleDbConnection().ToString(); OleDbConnection myConnection = new OleDbConnection(connString); myConnection.Open();
//string pStr_SQL = "SELECT DISTINCT [" + dataColumnName + "], [" + labelColumnName + "] FROM " + tableName; OleDbCommand myCommand = new OleDbCommand(pStr_SQL, myConnection);
// 3. Create/Populate the DataSet DataSet ds = new DataSet(); OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand); myAdapter.Fill(ds);
// close the connection myConnection.Close();
// find the total of the numeric data float total = 0.0F, tmp; int iLoop; for (iLoop=0; iLoop < ds.Tables[0].Rows.Count; iLoop++) { tmp = Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]); total += tmp; }
// we need to create fonts for our legend and title Font fontLegend = new Font("Arial", 10), fontTitle = new Font("Arial", 15, FontStyle.Bold);
// We need to create a legend and title, how big do these need to be? // Also, we need to resize the height for the pie chart, respective to the // height of the legend and title const int bufferSpace = 15; int legendHeight = fontLegend.Height * (ds.Tables[0].Rows.Count+1) + bufferSpace; int titleHeight = fontTitle.Height + bufferSpace; int height = width + legendHeight + titleHeight + bufferSpace; int pieHeight = width; // maintain a one-to-one ratio
// Create a rectange for drawing our pie Rectangle pieRect = new Rectangle(0, titleHeight, width, pieHeight);
// Create our pie chart, start by creating an ArrayList of colors ArrayList colors = new ArrayList(); Random rnd = new Random(); for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++) colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
float currentDegree = 0.0F;
// Create a Bitmap instance Bitmap objBitmap = new Bitmap(width, height); Graphics objGraphics = Graphics.FromImage(objBitmap);
SolidBrush blackBrush = new SolidBrush(Color.Black); int xxx=0; // Put a white backround in objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height); for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++) { objGraphics.FillPie((SolidBrush) colors[iLoop], pieRect, currentDegree, Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total * 360); // increment the currentDegree currentDegree += Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total * 360;
string drawString = ds.Tables[0].Rows[iLoop][labelColumnName].ToString(); // Create font and brush. Font drawFont = new Font("Arial", 9); SolidBrush drawBrush = new SolidBrush(Color.Black); // Create point for upper-left corner of drawing.
////////////////////////////////////////////////////////////////////// float x = currentDegree; float y =Convert.ToSingle(ds.Tables[0].Rows[iLoop][dataColumnName]) / total * 360;
StringFormat drawFormat = new StringFormat(); objGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat); //////////////////////////////////////////////////////////////////////
}
// Create the title, centered StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center;
objGraphics.DrawString(title, fontTitle, blackBrush, new Rectangle(0, 0, width, titleHeight), stringFormat);
// Create the legend objGraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height - legendHeight, width, legendHeight); for (iLoop = 0; iLoop < ds.Tables[0].Rows.Count; iLoop++) { objGraphics.FillRectangle((SolidBrush) colors[iLoop], 5, height - legendHeight + fontLegend.Height * iLoop + 5, 10, 10);
objGraphics.DrawString(((string) ds.Tables[0].Rows[iLoop][labelColumnName].ToString()) + " - " + Convert.ToString(ds.Tables[0].Rows[iLoop][dataColumnName]), fontLegend, blackBrush, 20, height - legendHeight + fontLegend.Height * iLoop + 1); }
// display the total objGraphics.DrawString("Total Inspections: " + Convert.ToString(total), fontLegend, blackBrush, 5, height - fontLegend.Height - 5);
// Since we are outputting a Jpeg, set the ContentType appropriately Response.ContentType = "image/jpeg";
// Save the image to a file objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
// clean up... objGraphics.Dispose(); objBitmap.Dispose(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by Brian Shade |
last post: by
|
8 posts
views
Thread by Prabhakar |
last post: by
|
5 posts
views
Thread by gj |
last post: by
|
1 post
views
Thread by Jim |
last post: by
|
22 posts
views
Thread by PeteCresswell |
last post: by
|
3 posts
views
Thread by StBond |
last post: by
|
1 post
views
Thread by Scott H. |
last post: by
|
1 post
views
Thread by Saintor |
last post: by
| | | | | | | | | | | |