三.在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 >
四. 总结:
实现图表始终是互联网编程的一个难点,本文介绍了在asp.net页面中如何实现数据图表,在没有什么好的组件可以利用的前提下,利用.net framework sdk gdi+中提供的各种用以操作图形的方法,这样的过程虽然有点烦杂,但对实现复杂的图表是非常有用的。希望本文不仅能够帮助读者解决在互联网上的图表问题,也能够对读者的针对gdi+也有所了解。