javascript 在客户端绘制图表系列一——饼图

      希望转载,或引用我blog资源保留作者信息。——算了teeth_smile.gif,显然那也是不大可能的。我前不就在其他blog还发现了和我写的一摸一样的文章,虽然我写的自认为还是比较臭,但是没有人喜欢看到自己花了精力的总结,被人毫不分析,毫不处理,毫不掩饰的改上他的大名。在blogjava也发现过,啥都没有改。就作者名字改了,和写的时间晚点,有些估计是我的有缘人hitwall.gif,就晚几个小时——也许还真是和我想的一摸一样了。我也常用别人资源(不过性质没有这么恶劣罢了),所以不废话了,必定分享知识和分享苹果是不一样的。呵呵,发现自己废话还真不少哦。emwink.gif

上手JFreeChart
http://www.blogjava.net/JAVA-HE/archive/2007/04/18/111439.aspx

报表使用经验、技巧大总结(包括JFreechart、JS chart以及自己的使用经验)

http://www.blogjava.net/JAVA-HE/archive/2007/05/08/115813.html

这是我前面写的两篇关于客户端报表的总结,有需要的朋友可以参考参考。下面总结的是自己扩展封装的一个绘制饼图javascript class。

 

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* ************更多技术文章请访问:http://www.blogjava.net/JAVA-HE****************
  2 InBlock.gif *
  3 InBlock.gif *    文件名:pie.js V 1.01
  4 InBlock.gif *    
  5 InBlock.gif *    作  者:何昌敏
  6 InBlock.gif *
  7 InBlock.gif *    时  间:2007-6
  8 InBlock.gif *
  9 InBlock.gif *    描  述:绘制饼图
 10 InBlock.gif *
 11 InBlock.gif *    备  注:
 12 InBlock.gif *                1.修改数据转化为像素<1 像素时候,出现的图形走样bug。
 13 InBlock.gif *                2.实现换行可设置。
 14 InBlock.gif *                3.实现是否将说明图标居右。
 15 InBlock.gif *                4.实现阴影绘制可选。
 16 InBlock.gif *                5.实现比较严格的参数判断。
 17 InBlock.gif *                6.可选择显示百分比的。
 18 InBlock.gif *                7.实现了图像清除。
 19 InBlock.gif *                8.调整startx starty能实现整体位置调整。
 20 InBlock.gif *
 21 InBlock.gif *    感  谢:Walter Zorn提供了API ——wz_jsgraphics.js v. 3.01。
 22 InBlock.gif *                
 23 ExpandedBlockEnd.gif*************更多技术文章请访问:http://www.blogjava.net/JAVA-HE*************** */

 24 None.gif function  Pie(_div)
 25 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 26 InBlock.gif     var  piejg  =   new  jsGraphics(_div);
 27 InBlock.gif     var  colors  =   new  Array(); 
 28 InBlock.gif    colors[ 9 =   " #0066FF " ;
 29 InBlock.gif    colors[ 5 =   " #996633 " ;
 30 InBlock.gif    colors[ 2 =   " #80bb80 " ;
 31 InBlock.gif    colors[ 3 =   " #FF0066 " ;
 32 InBlock.gif    colors[ 4 =   " #9900FF " ;
 33 InBlock.gif    colors[ 6 =   " #006633 " ;
 34 InBlock.gif    colors[ 1 =   " #8080FF " ;
 35 InBlock.gif    colors[ 7 =   " #000000 " ;
 36 InBlock.gif    colors[ 8 = " #CCFFFF " ;
 37 InBlock.gif    colors[ 0 =   " #FF8080 " ;
 38 InBlock.gif    colors[ 10 =   " #066600 " ;
 39 InBlock.gif    colors[ 11 = " #666666 " ;
 40 InBlock.gif    
 41 InBlock.gif     this .start_x  =   0 ;
 42 InBlock.gif     this .start_y  =   0 ;
 43 InBlock.gif     this .width =   100 ;
 44 InBlock.gif     this .height =   100 ;
 45 InBlock.gif     this .desc_distance  =   80 ;
 46 InBlock.gif     this .desc_width  =   10 ;
 47 InBlock.gif     this .desc_height =   10 ;
 48 InBlock.gif     this .IsShowPercentage  = true ;
 49 InBlock.gif     this .IsShowShadow  = true ;
 50 InBlock.gif     this .IsDescRight = true ;
 51 InBlock.gif     this .nextRow  =   2 ;
 52 InBlock.gif    
 53 InBlock.gif     this .drawPie  = function  (y_value,x_value)
 54 ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif {
 55 InBlock.gif         if ( this .IsShowShadow)
 56 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 57 InBlock.gif            piejg.setColor( " #666666 " );
 58 InBlock.gif            piejg.fillEllipse( this .start_x + 5 this .start_y + 5 this .width,     this .height);
 59 InBlock.gif      piejg.setColor( " #CCFFFF " );
 60 InBlock.gif            piejg.fillEllipse( this .start_x,  this .start_y,  this .width,     this .height);
 61 ExpandedSubBlockEnd.gif        }

 62 InBlock.gif         var  Percentage  =   new  Array();
 63 InBlock.gif         var  y_len  =  y_value.length;
 64 InBlock.gif         var  x_len  =  x_value.length;
 65 InBlock.gif         var  sum  =   0 ;
 66 InBlock.gif         var  perspective   =   new  Array();
 67 InBlock.gif         var  begin_perspective  =   0 ;
 68 InBlock.gif         var  end_perspective  =   0 ;
 69 InBlock.gif        
 70 InBlock.gif         if (y_len  !=  x_len)
 71 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 72 InBlock.gif            alert( " X and Y length of inconsistencies, errors parameters. " );
 73 InBlock.gif             return ;
 74 ExpandedSubBlockEnd.gif        }

 75 InBlock.gif         for ( var  i  =   0 ; i < y_len;i ++ )
 76 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 77 InBlock.gif            sum += y_value[i];
 78 ExpandedSubBlockEnd.gif        }

 79 InBlock.gif         for  ( var  i  =   0 ; i < y_len;i ++ )
 80 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
 81 InBlock.gif             if (isNaN(y_value[i]))
 82 ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif {
 83 InBlock.gif                alert( " y is not a number! " );
 84 InBlock.gif                 return ;
 85 ExpandedSubBlockEnd.gif            }

 86 InBlock.gif            perspective[i]  =  Math.max(Math.round( 360 * y_value[i] / sum), 1 );
 87 InBlock.gif            Percentage[i]  = Math.round( 100 * y_value[i] / sum);
 88 InBlock.gif            end_perspective  += perspective[i];
 89 InBlock.gif             if (i == 0 )
 90 ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif {
 91 InBlock.gif                piejg.setColor(colors[i]); 
 92 InBlock.gif                piejg.fillArc( this .start_x, this .start_y, this .width, this .height,  0 , end_perspective); 
 93 ExpandedSubBlockEnd.gif            }

 94 InBlock.gif             else
 95 ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif {    
 96 InBlock.gif                begin_perspective  +=  perspective[i - 1 ];
 97 InBlock.gif                piejg.setColor(colors[i]); 
 98 InBlock.gif                piejg.fillArc( this .start_x, this .start_y, this .width, this .height, begin_perspective, end_perspective); 
 99 ExpandedSubBlockEnd.gif            }

100 InBlock.gif            
101 ExpandedSubBlockEnd.gif        }

102 InBlock.gif         var  temp_x  =   0 ;
103 InBlock.gif         var  temp_y  =   0 ;
104 InBlock.gif         if ( this .IsDescRight)
105 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
106 InBlock.gif             for ( var  i  =   0  ;i < x_len;i ++ )
107 ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif {
108 InBlock.gif                temp_x  =   this .width + 10 + this .start_y;
109 InBlock.gif                temp_y  =   this .start_y + (i - x_len / 2 + 1 / 2 ) * ( this .height / x_len) + this .height / 2 ;
110 InBlock.gif                 // temp_y = this.start_y+(i+1)*(this.height/x_len);
111 InBlock.gif                 piejg.setColor(colors[i]);
112 InBlock.gif                piejg.fillRect(temp_x,temp_y, this .desc_width, this .desc_height);  
113 InBlock.gif                 if ( this .IsShowPercentage)
114 ExpandedSubBlockStart.gifContractedSubBlock.gif                 dot.gif {
115 InBlock.gif                    piejg.drawString(x_value[i] + " [ " + Percentage[i] + " %] " ,temp_x + this .desc_width,temp_y); 
116 ExpandedSubBlockEnd.gif                }
else
117 ExpandedSubBlockStart.gifContractedSubBlock.gif                 dot.gif {
118 InBlock.gif                    piejg.drawString(x_value[i],temp_x + this .desc_width,temp_y); 
119 ExpandedSubBlockEnd.gif                }
        
120 ExpandedSubBlockEnd.gif            }

121 ExpandedSubBlockEnd.gif        }

122 InBlock.gif         else
123 ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif {
124 InBlock.gif             for ( var  i  =   0  ;i < x_len;i ++ )
125 ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif {
126 InBlock.gif                temp_x  =  i * this .desc_distance + this .start_x;
127 InBlock.gif                temp_y  =   this
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值