老师现在要我做的网页的后台程序中要读数据库中采集的时间和电压两列数据,做出线形图显示在网页上,可我现在一点头绪也没有,我查资料得到下面的程序,有坐标点可以显示图表,但不知道怎样读数据库,请高手帮帮我,老师催的很紧。谢谢
namespace tree
{
/// <summary>
/// WebForm5 的摘要说明。
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
class LineChart
{
public Bitmap b ;
public string Title = "在ASP.NET中实现数据图表" ;
public string Title2 = "在ASP.NET中实现数据图表" ;
public string Title3 = "在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 ) ;
Brush blackBrush = new SolidBrush ( Color . Black ) ;
Font axesFont = new Font ( "arial", 10 ) ; //首先要创建图片的大小
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.DrawLine(new Pen ( Color . Black , 1 ),10,ChartHeight,ChartWidth-20,ChartHeight);
//g.DrawLine(new Pen ( Color . Black , 1 ),10,5,10,ChartHeight-20);
g . DrawRectangle ( new Pen ( Color . Black , 1 ) , ChartInset , ChartInset , ChartWidth , ChartHeight ) ;
//写出图片上面的图片内容文字
g . DrawString ( Title , new Font ( "arial", 16 ) , blackBrush , Width / 3 , 10 ) ;
g . DrawString ( Title2 , new Font ( "arial", 9 ) , blackBrush , Width / 23 , 18 ) ;
g . DrawString ( Title3 , new Font ( "arial", 9 ) , blackBrush , Width /2, 450) ;
//沿X坐标写入X标签
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 ) ;
}
//沿Y坐标写入Y标签
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 ) ; //画出图表中的数据
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 ;
} //最后以图片形式来浏览
b . Save ( p . Response . OutputStream , ImageFormat . Jpeg ) ;
} ~LineChart ( )
{
g . Dispose ( ) ;
b . Dispose ( ) ;
}
}
void Page_Load ( Object sender , EventArgs e )
{
LineChart c = new LineChart ( 640 , 480 , Page ) ;
c . Title = "油井电流图显示 ";
c . Title2 = "电流值 ";
c . Title3 = "时间 ";
c . Xorigin = 0 ; c . ScaleX = 500 ; c . Xdivs = 5 ;
c . Yorigin = 0 ; c . ScaleY = 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 ( ) ;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
namespace tree
{
/// <summary>
/// WebForm5 的摘要说明。
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
class LineChart
{
public Bitmap b ;
public string Title = "在ASP.NET中实现数据图表" ;
public string Title2 = "在ASP.NET中实现数据图表" ;
public string Title3 = "在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 ) ;
Brush blackBrush = new SolidBrush ( Color . Black ) ;
Font axesFont = new Font ( "arial", 10 ) ; //首先要创建图片的大小
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.DrawLine(new Pen ( Color . Black , 1 ),10,ChartHeight,ChartWidth-20,ChartHeight);
//g.DrawLine(new Pen ( Color . Black , 1 ),10,5,10,ChartHeight-20);
g . DrawRectangle ( new Pen ( Color . Black , 1 ) , ChartInset , ChartInset , ChartWidth , ChartHeight ) ;
//写出图片上面的图片内容文字
g . DrawString ( Title , new Font ( "arial", 16 ) , blackBrush , Width / 3 , 10 ) ;
g . DrawString ( Title2 , new Font ( "arial", 9 ) , blackBrush , Width / 23 , 18 ) ;
g . DrawString ( Title3 , new Font ( "arial", 9 ) , blackBrush , Width /2, 450) ;
//沿X坐标写入X标签
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 ) ;
}
//沿Y坐标写入Y标签
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 ) ; //画出图表中的数据
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 ;
} //最后以图片形式来浏览
b . Save ( p . Response . OutputStream , ImageFormat . Jpeg ) ;
} ~LineChart ( )
{
g . Dispose ( ) ;
b . Dispose ( ) ;
}
}
void Page_Load ( Object sender , EventArgs e )
{
LineChart c = new LineChart ( 640 , 480 , Page ) ;
c . Title = "油井电流图显示 ";
c . Title2 = "电流值 ";
c . Title3 = "时间 ";
c . Xorigin = 0 ; c . ScaleX = 500 ; c . Xdivs = 5 ;
c . Yorigin = 0 ; c . ScaleY = 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 ( ) ;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
解决方案 »
- 关于DataTable合并行的问题
- c#怎么让两个独立窗体在屏幕上并排平铺?
- 急?SQL统计数据怎么样提高效率 sqlserver
- 菜鸟学习C#,高手进来看下。。
- 请问viewstate可以跨页面传输吗?
- 在dropdownlist控件里的字符串怎样换行??
- 在同一解决方案中,根据不同的启动项,执行不同的代码。
- 『StreamReader 问题..在线等,立刻给分!!!』
- 如何把一些文件放到C#生成的exe文件里
- 如何在两个窗体之间传递值,高手帮忙!
- 急!!请问 windows有没有API接口可以在程序中验证域用户的密码,以便在我公司内部网站提供VPN虚拟拨号的对帐功能
- 如何在winform中将DataGrid中的每一行末尾插入复选框??
再填充系列.
http://edobnet.cnblogs.com/archive/2004/06/07/13999.html