javascript 在客户端绘制图表系列三——xy坐标曲线图

系列1、2中介绍过的内容在这里不会赘述。
原理同面1、2基本一样。

 

 1 None.gif <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 2 None.gif < html  xmlns ="http://www.w3.org/1999/xhtml" >
 3 None.gif < head >
 4 None.gif < meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />
 5 None.gif < title > TEST </ title >
 6 None.gif < script  type ="text/javascript"  src ="wz_jsgraphics.js" ></ script >
 7 None.gif < script  type ="text/javascript"  src ="line.js" ></ script >
 8 None.gif </ head >
 9 None.gif < body >
10 None.gif < p > 1.线图 </ p >
11 None.gif < div  id ="LineDiv"  style ="position:relative;height:200px;width:300px;" ></ div >
12 ExpandedBlockStart.gifContractedBlock.gif < script  language ="javascript" > dot.gif
13 InBlock.gif     var  y =   new  Array ();
14 InBlock.gif     y[ 0 =   16000 ;
15 InBlock.gif     y[ 1 =   1000 ;
16 InBlock.gif     y[ 2 =   20000 ;
17 InBlock.gif         y[ 3 =   100 ;
18 InBlock.gif     y[ 4 =   - 500 ;
19 InBlock.gif     y[ 5 =   9000 ;
20 InBlock.gif     var  x  =   new  Array ();
21 InBlock.gif    x[ 0 =   " a " ;
22 InBlock.gif    x[ 1 =   " b " ;
23 InBlock.gif    x[ 2 =   " c " ;
24 InBlock.gif     x[ 3 =   " aa " ;
25 InBlock.gif     x[ 4 =   " bb " ;
26 InBlock.gif     x[ 5 =   " dd " ;
27 InBlock.gif     var  myline  =   new  Line( " LineDiv " );
28 InBlock.gif    myline.drawXYLine(y,x);
29 ExpandedBlockEnd.gif     // myline.clearLine();
30 None.gif
</ script >         
31 None.gif </ body >
32 None.gif </ html >

效果图:
line.PNG

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* ************更多技术文章请访问:http://www.blogjava.net/JAVA-HE****************
  2 InBlock.gif *
  3 InBlock.gif *    文件名:line.js V 1.01
  4 InBlock.gif *    
  5 InBlock.gif *    作  者:何昌敏
  6 InBlock.gif *
  7 InBlock.gif *    时  间:2007-7
  8 InBlock.gif *
  9 InBlock.gif *    描  述:绘制坐标曲线图
 10 InBlock.gif *
 11 InBlock.gif *    备  注:
 12 InBlock.gif *                1.实现了根据所提供数据,自动标刻y轴坐标。
 13 InBlock.gif *                2.实现了图像清除。
 14 InBlock.gif *                3.调整startx starty能实现整体位置调整。
 15 InBlock.gif *
 16 InBlock.gif *    感  谢:Walter Zorn提供了API ——wz_jsgraphics.js v. 3.01。
 17 InBlock.gif *                
 18 ExpandedBlockEnd.gif*************更多技术文章请访问:http://www.blogjava.net/JAVA-HE*************** */

 19 None.gif
 20 None.gif function  Line(obj)
 21 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 22 InBlock.gif     this .jg  =   new  jsGraphics(obj); 
 23 InBlock.gif     var  colors  =   new  Array();
 24 InBlock.gif    colors[ 0 =   " #0066FF " ;
 25 InBlock.gif    colors[ 1 =   " #FF6633 " ;
 26 InBlock.gif    colors[ 2 =   " #9900FF " ;
 27 InBlock.gif    colors[ 3 =   " #FF0066 " ;
 28 InBlock.gif    colors[ 4 =   " #066600 " ;
 29 InBlock.gif    colors[ 5 =   " #006633 " ;
 30 InBlock.gif    colors[ 6 =   " #33FFFF " ;
 31 InBlock.gif    colors[ 7 =   " #000000 " ;
 32 InBlock.gif    colors[ 8 =   " #FFFF00 " ;
 33 InBlock.gif    colors[ 9 =   " #000033 " ;
 34 InBlock.gif    colors[ 10 =   " #CCFFFF " ;
 35 InBlock.gif    colors[ 11 =   " #666666 " ;
 36 InBlock.gif     this .start_x  =   40 ;         // 应大于等于y_str_width
 37 InBlock.gif      this .y_str_width = 40 ;     // 坐标系的左边距
 38 InBlock.gif      this .x_str_tom  = 10 ;         // x轴文字 距离坐标系底部距离。
 39 InBlock.gif      this .start_y  =   20 ;         // >=this.ArrowLength*2 箭头的高度
 40 InBlock.gif      this .width = 200 ;
 41 InBlock.gif     this .height = 120 ;
 42 InBlock.gif     this .y_line_num  =   5 ;
 43 InBlock.gif    
 44 InBlock.gif     this .IsDrawArrow  =   true ;
 45 InBlock.gif     this .ArrowLength  = 6 ;
 46 InBlock.gif    
 47 InBlock.gif     this .drawXYLine  =   function  (_y,_x)
 48 ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif {
 49 InBlock.gif         var  y_length  =  _y.length;
 50 InBlock.gif         var  x_length  =  _x.length;
 51 InBlock.gif         if (y_length != x_length)
 52 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 53 InBlock.gif            alert( " X and Y length of inconsistencies, errors parameters. " );
 54 InBlock.gif             return ;
 55 ExpandedSubBlockEnd.gif        }

 56 InBlock.gif         var  y_line_distance  =  Math.round( this .height / this .y_line_num);
 57 InBlock.gif         var  x_line_distance  =  Math.round( this .width / x_length);
 58 InBlock.gif        
 59 InBlock.gif         this .jg.drawLine( this .start_x, this .start_y + this .height, this .start_x + this .width, this .start_y + this .height); // x
 60 InBlock.gif          this .jg.drawLine( this .start_x, this .start_y + this .height, this .start_x, this .start_y); // y
 61 InBlock.gif          this .jg.setStroke(Stroke.DOTTED);    
 62 InBlock.gif         var  _y_copy  =  _y.concat();
 63 InBlock.gif         var  temp  =  _y;
 64 InBlock.gif        temp.sort( function  AscSort(x, y) 
 65 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 66 InBlock.gif           return  x  ==  y  ?   0  : (x  >  y  ?   1  :  - 1 );
 67 ExpandedSubBlockEnd.gif        }

 68 InBlock.gif        );
 69 InBlock.gif         var  y_max2y_min  =  temp[x_length - 1 ] - temp[ 0 ];
 70 InBlock.gif         var  y_min  = temp[ 0 ];
 71 InBlock.gif         var  y_value_distance  =  y_max2y_min / this .y_line_num;
 72 InBlock.gif         for ( var  i = 0 ;i < this .y_line_num;i ++ )
 73 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 74 InBlock.gif             var  y_item  =   this .start_y + this .height - (i + 1 ) * y_line_distance;
 75 InBlock.gif             this .jg.drawLine( this .start_x,y_item, this .start_x + this .width,y_item);
 76 InBlock.gif             var  y_v  =  Math.round(y_value_distance * (i + 1 ) + y_min);
 77 InBlock.gif             this .jg.drawString(y_v, this .start_x - this .y_str_width,y_item);
 78 ExpandedSubBlockEnd.gif        }

 79 InBlock.gif         for (i = 0 ;i < x_length;i ++ )
 80 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 81 InBlock.gif             this .jg.setStroke( - 1 );
 82 InBlock.gif             this .jg.setColor( " #000000 " );
 83 InBlock.gif             var  x_item_end  =   this .start_x + x_line_distance * (i + 1 );
 84 InBlock.gif             this .jg.drawLine(x_item_end, this .start_y + this .height,x_item_end, this .start_y);
 85 InBlock.gif             this .jg.drawString(_x[i],x_item_end, this .start_y + this .height + 10 );
 86 ExpandedSubBlockEnd.gif        }

 87 InBlock.gif         for (i = y_length;i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值