Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Pie Chart - Labelling

Reply
Thread Tools

Pie Chart - Labelling

 
 
=?Utf-8?B?cmtibmFpcg==?=
Guest
Posts: n/a
 
      01-21-2005
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
}
}


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBNVlA=?=
Guest
Posts: n/a
 
      01-21-2005
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
> }
> }
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OWC pie chart LB ASP .Net 9 10-10-2006 11:03 AM
PIE/BAR CHART QUESTIONS?(creating them dynamically) =?Utf-8?B?UGF0cmljay5PLklnZQ==?= ASP .Net 10 11-27-2005 08:40 PM
ASP can generate a chart (pie chart) ? Tarek ASP General 6 11-19-2004 06:33 PM
Pie Chart control Cliff Harris ASP .Net 1 06-12-2004 12:18 AM
Re: GD::Graph Pie Chart Display Problem IPaul Perl 0 04-02-2004 12:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57