473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pie Chart - Labelling

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
}
}
Nov 19 '05 #1
1 1871
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
}
}

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brian Shade | last post by:
Hi all. I am having a bit of a problem with my Perl script and the creation of a chart. I can create the chart and create a new SeriesCollection. I then specify the XValues and the Values parameter...
8
by: Prabhakar | last post by:
Friends, I have been migrating ASP applications from IIS4.0/NT4.0 to IIS5.0/W2K. All the applications working perfectly except one. This particular application will make use of asp chart object....
5
by: gj | last post by:
I'm working on a build environment for .NET application where the developers are using visual studio and VSS. In order to pull only the changed modules (and any required dependencies) I want to...
1
by: Jim | last post by:
I have this chart on a form. I'm trying to get this chart to render only if the user chooses to do so. This because of that the chart is quite complex and takes some time to render. I know that I...
22
by: PeteCresswell | last post by:
I've been to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mschrt/html/vbobjtitleobject.asp, but still don't have a clue. For example, I've got a chart object namde...
3
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am...
1
by: Scott H. | last post by:
Hello: I am trying to use Crystal Reports from VS.NET 2003 to produce a simple line chart, where the x axis represents the number of measurements taken and the y axis represents the range of...
1
by: Saintor | last post by:
This is for A97 in runtime or MDE mode. My intent was to define a temporary query using the recordsetclone property / filter / orderby. The controlsource of my chart would be based on a grouped...
1
by: cubekid | last post by:
I am using OWC 11 in my new web application for displaying charts. Is it possible in OWC 11 to combine and display a column and a line graph (Looking like they overlap each other)? I tried it using...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.