Skip to main content

Drawing Line Charts In asp.net

LineChart.cs

import the following namespaces

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;


public class LineChart
{
public Bitmap Bitmapb;
public string strTitle = "Default Title";
public ArrayList ArrayListchartValues = new ArrayList();
public float ftXorigin = 0, ftYorigin = 0;
public float ftScaleX, ftScaleY;
public float ftXdivs = 2, ftYdivs = 2;
private int intWidth, intHeight;
private Graphics Graphicsg;
private Page Pagep;

struct datapoint
{
public float ftx;
public float fty;
public bool bolvalid;
}
//initialize

public LineChart(int intmyWidth, int intmyHeight, Page myPage)
{
intWidth = intmyWidth; intHeight = intmyHeight;
ftScaleX = intmyWidth; ftScaleY = intmyHeight;
Bitmapb = new Bitmap(intmyWidth, intmyHeight);
Graphicsg = Graphics.FromImage(Bitmapb);
Pagep = myPage;
}
public void AddValue(int intx, int inty)
{
datapoint myPoint;
myPoint.ftx = intx;
myPoint.fty = inty;
myPoint.bolvalid = true;
ArrayListchartValues.Add(myPoint);

}
public void Draw()

{

int inti;
float ftx, fty, ftx0, fty0;
string strmyLabel;
Pen penblackPen = new Pen(Color.Black,1);
Brush BrushblackBrush = new SolidBrush(Color.Black);
Font FontaxesFont = new Font("arial",10);

//first establish working area

Pagep.Response.ContentType="image/jpeg";
Graphicsg.FillRectangle(new
SolidBrush(Color.LightYellow),0,0,intWidth,intHeight);
int intChartInset = 50;
int intChartWidth = intWidth-(2*intChartInset);
int intChartHeight = intHeight-(2*intChartInset);
Graphicsg.DrawRectangle(new Pen(Color.Black,1),intChartInset,intChartInset,intChartWidth,intChartHeight);
//must draw all text items before doing the rotate below
Graphicsg.DrawString(strTitle, new Font("arial",14), BrushblackBrush, intWidth/3, 10);
//draw X axis labels
for(inti=0; inti<=ftXdivs; inti++)

{

ftx=intChartInset+(inti*intChartWidth)/ftXdivs;
fty=intChartHeight+intChartInset;
strmyLabel = (ftXorigin + (ftScaleX*inti/ftXdivs)).ToString();
Graphicsg.DrawString(strmyLabel, FontaxesFont, BrushblackBrush, ftx-4, fty+10);
Graphicsg.DrawLine(penblackPen, ftx, fty+2, ftx, fty-2);
}

//draw Y axis labels
for(inti=0; inti<=ftYdivs; inti++)

{

ftx=intChartInset;
fty=intChartHeight+intChartInset-(inti*intChartHeight/ftYdivs);
strmyLabel = (ftYorigin + (ftScaleY*inti/ftYdivs)).ToString();
Graphicsg.DrawString(strmyLabel, FontaxesFont, BrushblackBrush, 5, fty-6);
Graphicsg.DrawLine(penblackPen, ftx+2, fty, ftx-2, fty);

}



//transform drawing coords to lower-left (0,0)

Graphicsg.RotateTransform(180);
Graphicsg.TranslateTransform(0,-intHeight);
Graphicsg.TranslateTransform(-intChartInset,intChartInset);
Graphicsg.ScaleTransform(-1, 1);



//draw chart data

datapoint datapointprevPoint = new datapoint();
datapointprevPoint.bolvalid=false;

foreach(datapoint myPoint in ArrayListchartValues)

{

if(datapointprevPoint.bolvalid==true)

{

ftx0=intChartWidth*(datapointprevPoint.ftx-ftXorigin)/ftScaleX;
fty0=intChartHeight*(datapointprevPoint.fty-ftYorigin)/ftScaleY;
ftx=intChartWidth*(myPoint.ftx-ftXorigin)/ftScaleX;
fty=intChartHeight*(myPoint.fty-ftYorigin)/ftScaleY;
Graphicsg.DrawLine(penblackPen,ftx0,fty0,ftx,fty);
Graphicsg.FillEllipse(BrushblackBrush,ftx0-2,fty0-2,4,4);
Graphicsg.FillEllipse(BrushblackBrush,ftx-2,fty-2,4,4);

}

datapointprevPoint = myPoint;

}

//finally send graphics to browser
Bitmapb.Save(Pagep.Response.OutputStream, ImageFormat.Jpeg);

}
LineChart()

{

Graphicsg.Dispose();

Bitmapb.Dispose();

}

}

-----------------------------------------------------------

add a default.aspx and add the following code in page load


default.aspx.cs
-----------------
protected void Page_Load(object sender, EventArgs e)
{
LineChart c = new LineChart(640, 480, Page);
c.strTitle = "My Line Chart";
c.ftXorigin = 0;
c.ftScaleX = 500;
c.ftXdivs = 5;
c.ftYorigin = 0;
c.ftScaleY = 1000;
c.ftYdivs = 5;
c.AddValue(50, 50);
c.AddValue(100, 100);
c.AddValue(200, 150);
c.AddValue(450, 450);
c.Draw();


}

Comments

Popular posts from this blog

AJAX

ASP.NET AJAX From Wikipedia, the free encyclopedia Jump to: navigation, search ASP.NET AJAX is a set of extensions to ASP.NET developed by Microsoft for implementing Ajax functionality. It is released under the Microsoft Public License. Contents [hide] • 1 Road Map • 2 Browser support • 3 ASP.NET AJAX Suite • 4 Microsoft Ajax Library • 5 The UpdatePanel Control • 6 Script Controls and Extenders • 7 Web-services and JSON • 8 See also • 9 References • 10 External links [edit] Road Map In its early development stages, ASP.NET AJAX was given the code name Atlas. Initial releases were made public to the development community in the form of CTPs (Community Technical Previews). The production version, ASP.NET AJAX 1.0 RTM; was released on January 23, 2007[1][2] as an extension to ASP.NET 2.0. Important release milestones for ASP.NET AJAX are as follows: • CTPs - October, December 2005, January, March, April, June, July 2006 • Beta - October, November 2006 • Release Candidate (RC) - December 2