Dynamic Line Charts [ASP.NET 2.0]

Demo here: http://www.kirupafx.com/Chart/Default.aspx (keep refreshing for random values)

To use this, simply copy and paste the following code into an ASP page (mine was called Default.aspx / Default.aspx.cs)

[SIZE=2][COLOR=#0000ff][COLOR=#000000]using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;[/COLOR]
[COLOR=#000000]public partial class Chart_Default : System.Web.UI.Page
{
    Graphics gfx;
    Bitmap bmp;[/COLOR]
[COLOR=#000000]    protected void Page_Load(object sender, EventArgs e)
    {
        bmp = new Bitmap(400, 300);
        gfx = Graphics.FromImage(bmp);
        gfx.Clear(Color.White);
        gfx.SmoothingMode = SmoothingMode.AntiAlias;[/COLOR]
[COLOR=#000000]        // Define Points
        //int[] p = new int[] { 10, 12, 48, 102, 290, 15, 100, 25, 300, 200, 150, 200, 60, 40, 250 };[/COLOR]
[COLOR=#000000]        int[] p = generateRandomValues();[/COLOR]
[COLOR=#000000]        int[] k = new int[p.Length];[/COLOR]
[COLOR=#000000]        Array.Copy(p, k, p.Length);
        Array.Sort(k);[/COLOR]
[COLOR=#000000]        DrawChart(p, k[k.Length - 1], k[0]);
    }
    private void DrawChart(int[] points, int maxValue, int minValue)
    {
        int bottomOffset = 50;
        int leftOffset = 40;
        int topOffset = 30;
        int rightOffset = 50;[/COLOR]
[COLOR=#000000]        // Taking care of some bookwork (declaring/initializing variables)
        int maxDataPoints = points.Length;
        int chartHeight = bmp.Height - bottomOffset;
        int chartWidth = bmp.Width - rightOffset;[/COLOR]
[COLOR=#000000]        double adjustedMax = maxValue * .10 + maxValue;
        double adjustedMin = minValue - .50 * minValue;
        double adjustVerticalRatio = (chartHeight-topOffset) / adjustedMax;
        double adjustHorizontalRatio = ((chartWidth - leftOffset) / (maxDataPoints - 1));[/COLOR]
[COLOR=#000000]        int space = (int)bmp.Width / points.Length;[/COLOR]
[COLOR=#000000]        Pen chartPen = new Pen(Color.Orange, 3);
        Pen gridLine = new Pen(Color.LightGray, 1);[/COLOR]
[COLOR=#000000]        int minYpos = chartHeight - topOffset;
        int maxYpos = 10;[/COLOR]
[COLOR=#000000]        for (int i = 0; i < maxDataPoints - 1; i++)
        {
            int xPos = Convert.ToInt32(i * adjustHorizontalRatio) + leftOffset;
            int xPos2 = Convert.ToInt32((i + 1) * adjustHorizontalRatio) + leftOffset;[/COLOR]
[COLOR=#000000]            int yPos = Convert.ToInt32(chartHeight - adjustVerticalRatio * points*);
            int yPos2 = Convert.ToInt32(chartHeight - adjustVerticalRatio * points[i + 1]);[/COLOR]
[COLOR=#000000]            if (points* == minValue)
            {
                minYpos = yPos;
            }[/COLOR]
[COLOR=#000000]            if (points* == maxValue)
            {
                maxYpos = yPos;
            }[/COLOR]
[COLOR=#000000]            gfx.DrawLine(gridLine, new Point(xPos2, topOffset), new Point(xPos2, chartHeight));
            gfx.DrawLine(chartPen, new Point(xPos, yPos), new Point(xPos2, yPos2));[/COLOR]
[COLOR=#000000]            gfx.DrawString(i.ToString(), new Font("Arial", 8), new SolidBrush(Color.Gray), new Point(xPos - 4, chartHeight + 10));
        }[/COLOR]
[COLOR=#000000]        //Draw Border Lines
        Pen borderLine = new Pen(Color.DarkGray, 2);[/COLOR]
[COLOR=#000000]        //Left Border
        gfx.DrawLine(borderLine, new Point(leftOffset, chartHeight), new Point(leftOffset, topOffset));[/COLOR]
[COLOR=#000000]        //Bottom Border
        gfx.DrawLine(borderLine, new Point(leftOffset, chartHeight), new Point(chartWidth, chartHeight));[/COLOR]
[COLOR=#000000]        //Right Border
        gfx.DrawLine(borderLine, new Point(chartWidth, chartHeight), new Point(chartWidth, topOffset));[/COLOR]
[COLOR=#000000]        //Top Border
        gfx.DrawLine(borderLine, new Point(leftOffset, topOffset), new Point(chartWidth, topOffset));[/COLOR]
[COLOR=#000000]        //Drawing Vertical Min/Max Values
        gfx.DrawString(maxValue.ToString(), new Font("Arial", 8), new SolidBrush(Color.Gray), new Point(leftOffset - 25, maxYpos));
        gfx.DrawString(minValue.ToString(), new Font("Arial", 8), new SolidBrush(Color.Gray), new Point(leftOffset - 25, minYpos));
        gfx.DrawLine(gridLine, new Point(leftOffset - 25, minYpos), new Point(chartWidth, minYpos));
        gfx.DrawLine(gridLine, new Point(leftOffset - 25, maxYpos), new Point(chartWidth, maxYpos));[/COLOR]
[COLOR=#000000]        //Finalizing and Cleaning Up
        Response.ContentType = "image/jpeg";
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        bmp.Dispose();
        gfx.Dispose();
        Response.End();
    }[/COLOR]
[COLOR=#000000]    private int[] generateRandomValues()
    {
        Random rand = new Random();
        int numValues = rand.Next(10, 20);[/COLOR]
[COLOR=#000000]        int[] newArray = new int[numValues];[/COLOR]
[COLOR=#000000]        for (int i = 0; i < numValues; i++)
        {
            newArray* = rand.Next(100, 1000);
        }
        return newArray;
    }
}[/COLOR][/COLOR][/SIZE]

I might make a tutorial for the above for .NET or even for Flash, for they are both conceptually similar. Only the syntax varies, so it all depends on how much free time I can find between now and school starting back up :slight_smile:
[SIZE=2]
:nat:
[/SIZE]