当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET实现数据图表1

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET实现数据图表1


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 42 ::
收藏到网摘: n/a

三.在ASP.NET中实现数据图表的完整源代码和运行界面:  在掌握了产生图片,在给图片上色、在图片上输出字符、和画线等基本操作过以后,充分的利用各种基本操作,就可以得的在ASP.NET中实现数据图表的完整程序,下图是运行界面:图05:在ASP.NET中实现数据图表的运行界面  下面是在ASP.NET中实现数据图表的完整代码(chart1.aspx),如下:<%@ Import Namespace = "System" %><%@ Import Namespace = "System.Drawing" %><%@ Import Namespace = "System.Drawing.Drawing2D" %><%@ Import Namespace = "System.Drawing.Imaging" %> <script language = "C#" runat = "server" >class LineChart{public Bitmap b ;public string Title = "在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 . DrawRectangle ( new Pen ( Color . Black , 1 ) , ChartInset , ChartInset , ChartWidth , ChartHeight ) ;//写出图片上面的图片内容文字g . DrawString ( Title , new Font ( "arial" , 14 ) , blackBrush , Width / 3 , 10 ) ;//沿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 = " 在ASP.NET中实现数据图表" ;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 ( ) ;}</script >