Wednesday 8 August 2012

Asp.net Chart using C#


How to Create Asp.net Chart using C# ?

Find below code for the chart



















using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;

public partial class Chart : 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 = 2;

        DefineArrays(size);

        p[0] = 100;
        p[1] = 200;
        //p[2] = 16.40;
        //p[3] = 15.29;
        //p[4] = 12.12;
        //p[5] = 17.21;

        GLabel[0] = "Estimate Hour";
        GLabel[1] = "Worked Hour";
        //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 * 155;
            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);



            //mine
            System.Drawing.Bitmap objBitMap = new System.Drawing.Bitmap(400, 250);
            System.Drawing.Graphics objGraphics;
            objGraphics = System.Drawing.Graphics.FromImage(objBitMap);
            objGraphics.Clear(System.Drawing.Color.Silver);
            System.Drawing.PointF symbolLeg = new System.Drawing.PointF(250, 25);
            //System.Drawing.PointF descLeg = new System.Drawing.PointF(310, 21);
            System.Drawing.PointF descLeg = new System.Drawing.PointF(130, 21);
         
            //
            for (i = 0; i < p.Length; i++)
            {
                total = total + p[i];
            }
            for (i = 0; i < p.Length; i++)
            {
               
                //mine

                //string color = myBrushes[i];
                //System.Drawing.ColorConverter colConvert = new System.Drawing.ColorConverter();
                //System.Drawing.Color objColor;
                //objColor = (System.Drawing.Color)colConvert.ConvertFromString(color);
                g.FillRectangle(myBrushes[i], symbolLeg.X, symbolLeg.Y, 20, 10);
                g.DrawRectangle(System.Drawing.Pens.Black, symbolLeg.X, symbolLeg.Y, 20, 10);
               // g.DrawString(GLabel[i], new System.Drawing.Font("Tahoma", 10), System.Drawing.Brushes.Black, descLeg);
                g.DrawString(GLabel[i], new Font("Verdana", 8, FontStyle.Bold), Brushes.Black, descLeg);
                //




                percentage = double.Parse(p[i].ToString()) / double.Parse(total.ToString()) * 100;
                height = Convert.ToInt32(percentage * 2);

                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
                //mine
                symbolLeg.Y += 20;
                descLeg.Y += 20;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}

No comments:

Post a Comment