!Dan Vallejo's WebLog!

Visual Studio .NET Developer

Creating Charts in ASP.NET

I finished my first pass at the Advanced ASP.NET course.

Here's a snippet of my section on graphics...

  1. Add a new web form named Chart.aspx.

  2. Modify Page_Load as follows:

    private void Page_Load(object sender, System.EventArgs e)
    {
        Random r = new Random();
    
        // A. Create a bitmap object
        Bitmap bmp = new Bitmap(250,150);
    
        // B. Create a graphics object to draw on
        // based on the bitmap.
        Graphics g = Graphics.FromImage(bmp);
    
        // C. Draw on the graphics object
        g.FillRectangle(new SolidBrush(Color.Beige), 0,0, bmp.Width, bmp.Height);
    
        SolidBrush b = new SolidBrush(Color.Navy);
        Font f = new Font("Arial", 14);
        Font legend = new Font("Arial", 6);
    
        for (int x = 0; x < 10; x++)
        {
            int h = r.Next(1,100);
    
            // rectangles fill upside down
            g.FillRectangle(b, x*20, bmp.Height - h - 10, 15, h);
    
            g.DrawString((x+1).ToString(), legend, b, x*20, bmp.Height - 10);
        }
    
        g.DrawString("Hello World", f, new SolidBrush(Color.Red), 10, 10);
    
        // D. Save the bitmap to the response outputstream
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
    
        // E. Dispose of the objects
        g.Dispose();
        bmp.Dispose();
    }
  3. Press F5 and navigate to the Chart.aspx page

Now how do you include the chart in another page?

  1. Open Default.aspx and drag an Image control on the bottom of the page.

  2. Modify the ImageUrl property to Chart.aspx.

  3. Press F5.

Pretty cool!

Published Monday, August 04, 2003 3:51 PM by DanVallejo
Filed under:

Comments

 

Nick Parker said:

Similar to an article I wrote back in Feb. of 2002. Really neat stuff.

"Web Graphics On The Fly in ASP.NET"
http://www.codeproject.com/aspnet/aspnet_web_graphics.asp
May 21, 2004 2:54 PM
 

Mahavir said:

Please help me with the questions below :
May 25, 2004 5:02 AM
 

Dan Vallejo said:

What question?
May 25, 2004 8:47 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker