Linechart
Public class linechart
{
Public bitmap B;
Public String title = "Implementing Data charts in ASP. NET ";
Public arraylist chartvalues = new arraylist ();
Public float xorigin = 0, yorigin = 0;
Public float scalex, scaley;
Public float xdivs = 2, ydivs = 2;
Private int width, height;
Private graphics g;
Private page P;
Struct datapoint
{
Public float X;
Public float y;
Public bool valid;
}
Public linechart (INT mywidth, int myheight, page mypage)
{
Width = mywidth;
Height = myheight;
Scalex = mywidth;
Scaley = myheight;
B = new Bitmap (mywidth, myheight );
G = graphics. fromimage (B );
P = mypage;
}
Public void addvalue (int x, int y)
{
Datapoint mypoint;
Mypoint. x = X;
Mypoint. Y = y;
Mypoint. Valid = true;
Chartvalues. Add (mypoint );
}
Public void draw ()
{
Int I;
Float X, Y, x0, y0;
String mylabel;
Pen blackpen = new pen (color. Blue, 2); // defines the paint brush.
Brush blackbrush = new solidbrush (color. Black); // defines the paint brush
Font axesfont = new font ("Arial", 10); // define the font
// Create the image size first
P. response. contenttype = "image/JPEG ";
G. fillrectangle (New solidbrush (color. lightgreen), 0, 0, width, height );
Int chartinset = 50;
Int chartwidth = width-(2 * chartinset );
Int chartheight = height-(2 * chartinset );
G. drawrectangle (new pen (color. Black, 1), chartinset, chartinset, chartwidth, chartheight );
// Write the text on the Image
G. drawstring (title, new font ("Arial", 14), blackbrush, width/3, 10 );
// Write the X tag along the X coordinate
For (I = 0; I <= xdivs; I ++)
{
X = chartinset + (I * chartwidth)/xdivs;
Y = chartheight + chartinset;
Mylabel = (xorigin + (scalex * I/xdivs). tostring ();
G. drawstring (mylabel, axesfont, blackbrush, X-4, Y + 10 );
G. drawline (blackpen, X, Y + 2, X, Y-2 );
}
// Write the y tag along the Y coordinate
For (I = 0; I <= ydivs; I ++)
{
X = chartinset;
Y = chartheight + chartinset-(I * chartheight/ydivs );
Mylabel = (yorigin + (scaley * I/ydivs). tostring ();
G. drawstring (mylabel, axesfont, blackbrush, 5, Y-6 );
G. drawline (blackpen, x + 2, Y, X-2, y );
}
G. rotatetransform (180 );
G. translatetransform (0,-height );
G. translatetransform (-chartinset, chartinset );
G. scaletransform (-1, 1 );
// Plot the data in the chart
Datapoint prevpoint = new datapoint ();
Prevpoint. Valid = false;
Foreach (datapoint mypoint in chartvalues)
{
If (prevpoint. Valid = true)
{
X0 = chartwidth * (prevpoint. X-xorigin)/scalex;
Y0 = chartheight * (prevpoint. Y-yorigin)/scaley;
X = chartwidth * (mypoint. X-xorigin)/scalex;
Y = chartheight * (mypoint. Y-yorigin)/scaley;
G. drawline (blackpen, x0, y0, x, y );
G. fillellipse (blackbrush, x0-2, y0-2, 4, 4 );
G. fillellipse (blackbrush, X-2, Y-2, 4, 4 );
}
Prevpoint = mypoint;
}
// Finally, view the image
B. Save (P. response. outputstream, imageformat. JPEG );
}
Public void dispose ()
{
G. Dispose ();
B. Dispose ();
}
}
Aspx. CS page call:
private void page_load (Object sender, system. eventargs E)
{< br> // place the user Code here to initialize the page
linechart c = new linechart (640,480, this. page);
C. title = "in ASP.
C. xorigin = 0; C. scalex = 500; C. xdivs = 5;
C. yorigin = 0; C. scale= 1000; C. ydivs = 5;
C. addvalue (0,150);
C. addvalue (50, 50);
C. addvalue (100,700);
C. addvalue (200,150);
C. addvalue (300,450);
C. addvalue (400, 75);
C. addvalue (450,450);
C. addvalue (500,250);
C. draw ();
C. dispose ();
}