header image
Home
NinethSense
My Favorites
Blog
Reach Me
 
My Microsoft
My OpenSource
My Electronics
My Misc Tech
My Project Management
 


Home arrow My Electronics arrow Microsoft arrow Reflected Bar Chart GDI+
Reflected Bar Chart GDI+ PDF Print
Written by Praveen V Nair   
Apr 30, 2007 at 05:04 AM
Author: PraVeeN - Microsoft MVP

Reflected Bar Chart with GDI+ for ASP.NET 2.0

 

Reflected Bar Chart

I dont know the name of this Graph. I searched a lot on the net to find the code for generating such a graph, but found only ordinary bar charts and histograms. Later I made a small program for generating such a graph and named 'reflected bar graph'. ;) I will rename it when I get the correct name.

 

private void DrawGraph(double[] yvalues)

{

Response.Clear();

double ybig = yvalues[0];

for (int i = 1; i < yvalues.Length; i++)

{

if (ybig < yvalues[i]) ybig = yvalues[i];

}

 

string str = string.Empty;

str = str.PadLeft(ybig.ToString().Length - 1, '0');

int devisor = int.Parse("1" + str);

ybig = ybig + (devisor - (ybig % devisor));

 

 

int x = 400, y = 400;

int margin = 40;

int xdistance = 30, ydistance = 15;

int ymiddle = y / 2;

// start desktop

Bitmap b = new Bitmap(x, y);

Graphics g = Graphics.FromImage(b);

Brush br = new SolidBrush(Color.White);

g.FillRectangle(br, 0, 0, x, y);

Pen p = new Pen(Color.LightBlue);

g.DrawRectangle(p, 0, 0, x - 1, y - 1);

ImageAttributes attr = new ImageAttributes();

 

Pen blackPen = new Pen(Color.Black, 3);

// end desktop

// start plot graph

Brush blackbrush = new SolidBrush(Color.Black);

Random r = new Random();

//Brush randombrush = new SolidBrush(Color.FromArgb(255, 228,228));

for (int i = 0, j = 0; i < yvalues.Length; i++, j += xdistance)

{

float yval = (float)((yvalues[i] * 10) / ybig) * ydistance;

 

Brush randombrush = new SolidBrush(Color.FromArgb(r.Next(155, 255), r.Next(155, 255), r.Next(155, 255)));

//g.FillRectangle(randombrush, margin + j, ymiddle - int.Parse(yvalues[i].ToString()) * ydistance, xdistance, int.Parse(yvalues[i].ToString()) *ydistance );

g.FillRectangle(randombrush, margin + j, ymiddle - yval, xdistance, yval);

// g.DrawRectangle(new Pen(Color.Gray), margin + j, ymiddle - float.Parse(yvalues[i].ToString()) * ydistance, xdistance, float.Parse(yvalues[i].ToString()) * ydistance);

}

g.CopyFromScreen(10, 10, 20, 20, Size.Empty);

//flip

Rectangle rect = new Rectangle(0, 0, x, ymiddle);

Bitmap b2 = (System.Drawing.Bitmap)b.Clone(rect, PixelFormat.Undefined);

b2.RotateFlip(RotateFlipType.RotateNoneFlipY);

g.DrawImage((System.Drawing.Image)b2, 0, ymiddle);

// end plot graph

 

 

// start guides

g.DrawLine(blackPen, margin, ymiddle, x - margin, ymiddle);

g.DrawLine(blackPen, margin, margin, margin, y - margin);

Font fnt = new Font("arial", 8);

for (int i = margin, j = 0; i < x - margin; i += xdistance, j += 10)

{

g.DrawLine(blackPen, i, ymiddle, i, (ymiddle) + 5);

g.DrawString(j.ToString() + "%", fnt, blackbrush, i - 7, ymiddle + 3);

}

for (int i = ymiddle, j = 0; i > margin; i -= ydistance, j += (int.Parse(ybig.ToString()) / 10))

{

g.DrawLine(blackPen, margin - 5, i, margin, i);

g.DrawString(j.ToString(), fnt, blackbrush, margin - 30, i - 7);

}

// end guides

Response.ContentType = "image/jpeg";

b.Save(Response.OutputStream, ImageFormat.Jpeg);

b.Dispose();

b2.Dispose();

g.Dispose();

 

}

 

 

<Previous   Next>
Microsoft MVP
Trivandrum Microsoft Users Group Forum
MSDN
ASP.net forums
  PHP Kerala Forums
PIT Solutions 



The CodeProject Microsoft Developer Network Official ASP.NET Forums Microsoft .NET Framework Community Microsoft Most Valuable Professional Kidoos.net forums Microsoft Visual Studio Developer Home Professional Information Technology Solutions Microsoft Research Home Trivandrum Microsoft Users Group Community Website