三.在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+也有所了解。