Draw Bar chart in asp.net


Code for draw chart in asp.net

Draw chart in asp.net



here is the complete example for draw bar chart


public partial class drawBarchart : System.Web.UI.Page
{
public Bitmap myImage;
public Graphics g;
public double[] p;
public string[] GLabel;
public Brush[] myBrushes;
public int Gwidth;
protected void Page_Load(object sender, EventArgs e)
{
setvalues();//Set the default values for chart
g.Clear(Color.WhiteSmoke);
drawBarcharts(g);
myImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
public void setvalues()
{
int size = 6;

DefineArrays(size);

p[0] = 20.12;
p[1] = 10.12;
p[2] = 16.40;
p[3] = 15.29;
p[4] = 12.12;
p[5] = 17.21;

GLabel[0] = "A";
GLabel[1] = "B";
GLabel[2] = "C";
GLabel[3] = "D";
GLabel[4] = "E";
GLabel[5]= "F";


initialiseGraphics(size);

}

public void DefineArrays(int size)
{
p = new double[size];
GLabel = new string[size];
myBrushes = new Brush[20];

}

private void initialiseGraphics(int size)
{
try
{
// Create an in-memory bitmap where you will draw the image.
// The Bitmap is 300 pixels wide and 200 pixels high.

Gwidth = size * 55;
myImage = new Bitmap(Gwidth, 300, PixelFormat.Format32bppRgb);
//myImage=new Bitmap(


// Get the graphics context for the bitmap.
g = Graphics.FromImage(myImage);

// Create the brushes for drawing
createBrushes();
}
catch (Exception ex)
{
throw ex;
}

}
public void createBrushes()
{
try
{
//Method to create brushes of specific colours

myBrushes[0] = new SolidBrush(Color.Red);
myBrushes[1] = new SolidBrush(Color.Blue);
myBrushes[2] = new SolidBrush(Color.Yellow);
myBrushes[3] = new SolidBrush(Color.Green);
myBrushes[4] = new SolidBrush(Color.Tan);
myBrushes[5] = new SolidBrush(Color.SkyBlue);
myBrushes[6] = new SolidBrush(Color.Orange);
myBrushes[7] = new SolidBrush(Color.PaleGreen);
myBrushes[8] = new SolidBrush(Color.Silver);
myBrushes[9] = new SolidBrush(Color.SeaGreen);
myBrushes[10] = new SolidBrush(Color.White);
}
catch (Exception ex)
{
throw ex;
}
}
public void drawBarcharts(Graphics g)
{
try
{
int i;
int xInterval = 40;
int width = 30;
int height = 0;
double total = 0.0;
double percentage;
SolidBrush blackBrush = new SolidBrush(Color.Black);

for (i = 0; i < p.Length; i++)
{
total = total + p[i];
}
for (i = 0; i < p.Length; i++)
{
percentage = double.Parse(p[i].ToString()) / double.Parse(total.ToString()) * 100;
height = Convert.ToInt32(percentage*7);

g.FillRectangle(myBrushes[i], xInterval * i + 50, 280 - height, width, height);

//label the barcharts
g.DrawString(GLabel[i], new Font("Verdana", 8, FontStyle.Bold), Brushes.Black, xInterval * i + 50 + (width / 3), 280 - height - 25);
// Draw the scale
g.DrawString(p[i].ToString(), new Font("Verdana", 5, FontStyle.Bold), Brushes.Black, 0, 280 - height);

//Draw the axes
g.DrawLine(Pens.Brown, 40, 10, 40, 290); //y-axis
g.DrawLine(Pens.Brown, 20, 280, Gwidth, 280); //x-axis

}
}
catch (Exception ex)
{
throw ex;
}
}


}


Related Articles

More articles: Bar chart ASP.Net Bar chart Draw chart

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: