javascript 在客户端绘制图表系列二——柱图

可适当参考  系列1 饼图 http://www.blogjava.net/JAVA-HE/archive/2007/06/29/126957.html

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*************更多技术文章请访问:http://www.blogjava.net/JAVA-HE****************
  2InBlock.gif *
  3InBlock.gif *    文件名:bar.js V 1.01
  4InBlock.gif *    
  5InBlock.gif *    作  者:何昌敏
  6InBlock.gif *
  7InBlock.gif *    时  间:2007-6
  8InBlock.gif *
  9InBlock.gif *    描  述:绘制柱图
 10InBlock.gif *
 11InBlock.gif *    备  注:
 12InBlock.gif *                1.修改数据==0,出现的图形走样bug。(设置了2像素作为0的显示)
 13InBlock.gif *                2.startx 实现水平移动。
 14InBlock.gif *                3.实现自动比例。
 15InBlock.gif *                4.实现实现柱的宽度自适应,分布自适应。
 16InBlock.gif *                5.实现比较严格的参数判断。
 17InBlock.gif *                6.实现了图像清除。
 18InBlock.gif *                7.是否画上箭头。 在画箭头的时候可设定箭头大小。
 19InBlock.gif *
 20InBlock.gif *    说  明:
 21InBlock.gif *                对于其位置的调整并没有做更多扩展,能自适应大小。
 22InBlock.gif *                至于位置,我想直接控制div的位置比较方便。
 23InBlock.gif *                当然还有背景的绘制,也认为修改DIV的背景,比在这里画要方便点。
 24InBlock.gif 
 25InBlock.gif *    感  谢:Walter Zorn提供了API ——wz_jsgraphics.js v. 3.01。
 26InBlock.gif *                
 27ExpandedBlockEnd.gif*************更多技术文章请访问:http://www.blogjava.net/JAVA-HE****************/

 28 None.gif
 29 None.gif function  Bar(_div)
 30 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 31InBlock.gif    var barjg = new jsGraphics(_div);
 32InBlock.gif    var colors = new Array();
 33InBlock.gif    colors[0= "#0066FF";
 34InBlock.gif    colors[1= "#FF6633";
 35InBlock.gif    colors[2= "#9900FF";
 36InBlock.gif    colors[3= "#FF0066";
 37InBlock.gif    colors[4= "#066600";
 38InBlock.gif    colors[5= "#006633";
 39InBlock.gif    colors[6= "#33FFFF";
 40InBlock.gif    colors[7= "#000000";
 41InBlock.gif    colors[8= "#FFFF00";
 42InBlock.gif    colors[9= "#000033";
 43InBlock.gif    colors[10= "#CCFFFF";
 44InBlock.gif    colors[11= "#666666";
 45InBlock.gif    
 46InBlock.gif    this.start_x = 40;
 47InBlock.gif    this.start_y = 0;
 48InBlock.gif    this.width=200;
 49InBlock.gif    this.height=120;
 50InBlock.gif    this.line_num=6;
 51InBlock.gif    this.y_start_x = 0;
 52InBlock.gif    this.scale=12;    
 53InBlock.gif    this.IsDrawArrow = true;
 54InBlock.gif    this.ArrowLength =6;
 55InBlock.gif    
 56InBlock.gif    this.drawBar = function (_y,_x)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 58InBlock.gif        var y_len = _y.length;
 59InBlock.gif        var x_len = _x.length;
 60InBlock.gif        if(y_len != x_len)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            alert("X and Y length of inconsistencies, errors parameters.");
 63InBlock.gif            return;
 64ExpandedSubBlockEnd.gif        }

 65InBlock.gif        barjg.setColor("#000000");
 66InBlock.gif        barjg.drawLine(this.start_x,this.height+this.start_y,this.width+this.start_x,this.height+this.start_y);//x line
 67InBlock.gif        barjg.drawLine(this.start_x,this.start_y,this.start_x,this.height+this.start_y);// y line
 68InBlock.gif      if(this.IsDrawArrow)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
 70InBlock.gif          barjg.drawLine(this.width+this.start_x,this.height+this.start_y+this.ArrowLength,this.width+this.start_x+this.ArrowLength,this.height+this.start_y);
 71InBlock.gif          barjg.drawLine(this.width+this.start_x,this.height+this.start_y-this.ArrowLength,this.width+this.start_x+this.ArrowLength,this.height+this.start_y);
 72InBlock.gif            barjg.drawLine(this.width+this.start_x,this.height+this.start_y,this.width+this.start_x+this.ArrowLength,this.height+this.start_y);
 73InBlock.gif            barjg.drawLine(this.start_x,this.start_y-this.ArrowLength,this.start_x-this.ArrowLength,this.start_y+this.ArrowLength);
 74InBlock.gif            barjg.drawLine(this.start_x,this.start_y-this.ArrowLength,this.start_x+this.ArrowLength,this.start_y+this.ArrowLength);
 75InBlock.gif            barjg.drawLine(this.start_x,this.start_y,this.start_x,this.start_y-this.ArrowLength);
 76ExpandedSubBlockEnd.gif      }

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

 85InBlock.gif            if(max_H<_y[i])
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif              max_H=_y[i];
 88ExpandedSubBlockEnd.gif            }

 89ExpandedSubBlockEnd.gif    }

 90InBlock.gif        this.scale=Math.round(max_H/this.height);
 91InBlock.gif        //每像素代表数值10
 92InBlock.gif        if(    this.scale<10)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            scale=10;
 95ExpandedSubBlockEnd.gif        }

 96InBlock.gif        for(i=0;i<this.line_num;i++)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            var y=this.height*i/this.line_num;        
 99InBlock.gif            barjg.setStroke(Stroke.DOTTED);    
100InBlock.gif            barjg.drawLine(this.start_x,y+this.start_y,this.width+this.start_x,y+this.start_y);    
101InBlock.gif            barjg.drawString(""+Math.round((this.height-y)*this.scale),this.y_start_x,y+this.start_y);
102ExpandedSubBlockEnd.gif        }

103InBlock.gif        barjg.setStroke(2);
104InBlock.gif        for(i=0;i<x_len;i++)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
106InBlock.gif            var barwidth=(this.width-this.start_x)/(x_len*2);
107InBlock.gif            var startX=this.start_x+(i+1)*this.width/x_len-barwidth;
108InBlock.gif            barjg.setColor(colors[i]);
109InBlock.gif            //从左上到右下
110InBlock.gif            barjg.fillRect( startX,this.height-Math.max(_y[i]/this.scale,2)+this.start_y,barwidth,Math.max(_y[i]/this.scale,2));
111InBlock.gif            barjg.drawString(_x[i],startX,this.height+this.start_y);
112ExpandedSubBlockEnd.gif        }

113InBlock.gif        barjg.paint();
114ExpandedSubBlockEnd.gif   }
;
115InBlock.gif        this.clearBar =function()
116ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
117InBlock.gif        barjg.clear();
118ExpandedSubBlockEnd.gif    }
;
119ExpandedBlockEnd.gif    }

上面js文件保存为:bar.js,使用下面DEMO 文件的的时候,一样需要引入wz_jsgraphics.js。可以在 http://www.blogjava.net/Files/JAVA-HE/pieDemo.rar 下载。

 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 ="bar.js" ></ script >
 8 None.gif </ head >
 9 None.gif < body >
10 None.gif < p > 1.柱图 </ p >
11 None.gif < div  id ="BarDiv"  style ="position:relative;height:200px;width:300px;" ></ div >
12 None.gif
13 ExpandedBlockStart.gifContractedBlock.gif < script  language ="javascript" > dot.gif
14InBlock.gif    var y= new Array ();
15InBlock.gif     y[0= 11112;
16InBlock.gif     y[1= 16000;
17InBlock.gif     y[2= 20000;
18InBlock.gif
19InBlock.gif    var x = new Array ();
20InBlock.gif    x[0= "a";
21InBlock.gif    x[1= "b";
22InBlock.gif    x[2= "c";    
23InBlock.gif    var mybar = new
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值