js生成柱狀圖

  1. <script language=javascript>
  2. function table1(total,table_x,table_y,thickness,table_width,all_width,all_height,table_type){
  3. //參數含義(傳遞的數組,橫座標,縱座標,柱子的厚度,柱子的寬度,圖表的寬度,圖表的高度,圖表的類型)
  4. //純JAVASCRIPT代碼生成圖表函數1——柱狀圖
  5. //運行環境爲IE 6.0
  6. //注意:倒立柱狀圖dataArray顯示順序錯了
  7.   
  8. //***************************************************************************************
  9. var tmdColor1 = new Array();
  10. tmdColor1[0] = "#d1ffd1"; tmdColor1[1] = "#ffbbbb"; tmdColor1[2] = "#ffe3bb";
  11. tmdColor1[3] = "#cff4f3"; tmdColor1[4] = "#d9d9e5"; tmdColor1[5] = "#ffc7ab";
  12. tmdColor1[6] = "#ecffb7"; tmdColor1[7] = "green";
  13. var tmdColor2 = new Array();
  14. tmdColor2[0] = "#00ff00"; tmdColor2[1] = "#ff0000"; tmdColor2[2] = "#ff9900";
  15. tmdColor2[3] = "#33cccc"; tmdColor2[4] = "#666699"; tmdColor2[5] = "#993300";
  16. tmdColor2[6] = "#99cc00"; tmdColor2[7] = "yellow";
  17. var tb_color = new Array(tmdColor1,tmdColor2);
  18. var line_color = "#69f";
  19. var left_width = 70;
  20. var length = thickness/2;
  21. var total_no = total[0].length;
  22. var temp1 = 0;
  23. var temp2,temp4,temp4;
  24. for(var i = 0;i<total_no;i++) {
  25.  if(temp1<total[0][i]) {
  26.   temp1 = total[0][i];
  27.  }
  28. }
  29. temp1 = parseInt(temp1.toString());
  30. if(temp1>9){  
  31.  temp2 = temp1.toString().substr(1,1)
  32.  if(temp2>4) {
  33.   temp3 = (parseInt((temp1/(Math.pow(10,(temp1.toString().length-1)))).toString())+1)*Math.pow(10,(temp1.toString().length-1));
  34.  }
  35.  else {
  36.   temp3 = (parseInt((temp1/(Math.pow(10,(temp1.toString().length-1)))).toString())+0.5)*Math.pow(10,(temp1.toString().length-1));
  37.  } 
  38. else{
  39.  if(temp1>4)
  40.   temp3 = 10; 
  41.  else 
  42.   temp3 = 5;
  43. }
  44. temp4=temp3
  45. document.write("<!--[if gte vml 1]><v:rect id='_x0000_s1027' alt='' style='position:absolute;left:" + (table_x+left_width) + "px;top:" + table_y + "px;width:" + all_width + "px;height:" + all_height + "px;z-index:-1' fillcolor='#9cf' stroked='f'><v:fill rotate='t' angle='-45' focus='100%' type='gradient'/></v:rect><![endif]-->");
  46. document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width) + "px," + (table_y+all_height) + "px' to='" + (table_x+all_width+left_width) + "px," + (table_y+all_height) + "px'/><![endif]-->");
  47. document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width) + "px," + table_y + "px' to='" + (table_x+left_width) + "px," + (table_y+all_height) + "px'/><![endif]-->");
  48. switch (table_type){
  49.  case "A":  
  50.  var table_space = (all_width-table_width*total_no)/total_no;
  51.  document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+length) + "px,"+ table_y + "px' to='" + (table_x+left_width+length) + "px," + (table_y+all_height-length) + "px' strokecolor='" + line_color + "'/><![endif]-->");
  52.  for(var i=0;i<=all_height-1;i+= all_height/5) {
  53.   document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width)+ "px," + (table_y+all_height-length-i) + "px' to='" + (table_x+left_width+length) + "px," + (table_y+all_height-i) +"px' strokecolor='" + line_color + "'/><![endif]-->");
  54.   document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+length) + "px," + (table_y+all_height-length-i) + "px' to='" + (table_x+all_width+left_width) + "px," + (table_y+all_height-length-i) + "px' strokecolor='" + line_color + "'/><![endif]-->");
  55.   document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+(left_width-15)) + "px," + (table_y+i) + "px' to='" + (table_x+left_width) + "px," + (table_y+i) + "px'/><![endif]-->");
  56.   document.write("<!--[if gte vml 1]>");
  57.   document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + table_x + "px;top:" + (table_y+i) + "px;width:" + left_width + "px;height:18px;z-index:1'>");
  58.   document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='right'>" + temp4 + "</td></tr></table></v:textbox></v:shape><![endif]-->");
  59.   temp4 = temp4-temp3/5;
  60.  }
  61.  for(var i=0;i<total_no;i++) {
  62.   
  63.   var temp_space = table_x + left_width + table_space / 2 + table_space * i + table_width * i;  
  64.   document.write("<v:rect id='_x0000_s1025' alt='' style='position:absolute;left:");
  65.   document.write(temp_space);
  66.   document.write("px;top:");
  67.   document.write(table_y + all_height * (1 - (total[0][i] / temp3)));
  68.   document.write("px;width:" + table_width + "px;height:" + all_height * (total[0][i] / temp3) + "px;z-index:1' fillcolor='" + tb_color[1][i] + "'>");
  69.   document.write("<v:fill color2='" + tb_color[0][i] + "' rotate='t' type='gradient'/>")
  70.   document.write("<o:extrusion v:ext='view' backdepth='" + thickness + "pt' color='" + tb_color[1][i] + "' on='t'/>");
  71.   document.write("</v:rect>");
  72.   document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + temp_space + "px;top:" + (table_y+all_height*(1-(total[0][i]/temp3))-table_width) + "px;width:" + (table_space+15) + "px;height:18px;z-index:1'>");
  73.   document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='center'>" + total[0][i] + "</td></tr></table></v:textbox></v:shape>");
  74.   document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + (temp_space-table_space/2) + "px;top:" + (table_y+all_height+1) + "px;width:" + (table_space+table_width) + "px;height:18px;z-index:1'>");
  75.   document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='center'>" + total[1][i] + "</td></tr></table></v:textbox></v:shape>");
  76.  }
  77.  break;
  78. case "B":
  79.  var table_space = (all_height - table_width * total_no) / total_no;
  80.  document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+length) + "px," + (table_y+all_height-length) + "px' to='" + (table_x+left_width+all_width) + "px," + (table_y+all_height-length) + "px' strokecolor='" + line_color + "'/><![endif]-->");
  81.  for(var i=0;i<=all_width-1;i +=all_width/5) {
  82.   document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+i) + "px," + (table_y+all_height-length) + "px' to='" + (table_x+left_width+length+i) + "px," + (table_y+all_height) + "px' strokecolor='" + line_color + "'/><![endif]-->");
  83.   document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+length+i) + "px," + (table_y+all_height-length) + "px' to='" + (table_x+left_width+length+i) + "px," + table_y + "px' strokecolor='" + line_color + "'/><![endif]-->");
  84.   document.write("<!--[if gte vml 1]><v:line id='_x0000_s1027' alt='' style='position:absolute;left:0;text-align:left;top:0;flip:y;z-index:-1' from='" + (table_x+left_width+i+all_width/5) + "px," + (table_y+all_height) + "px' to='" + (table_x+left_width+i+all_width/5) + "px," + (table_y+all_height+15) + "px'/><![endif]-->");
  85.   document.write("<!--[if gte vml 1]>");
  86.   document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x+left_width+i+all_width/5-left_width) + "px;top:" + (table_y+all_height) + "px;width:" + left_width + "px;height:18px;z-index:1'>");
  87.   document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='right'>" + temp4 + "</td></tr></table></v:textbox></v:shape><![endif]-->");
  88.   temp4 = temp4 - temp3 / 5;
  89.  }
  90.  for(var i=0;i<total_no;i++) {
  91.   var temp_space = table_space/2 + table_space * i + table_width * i;
  92.   document.write("<v:rect id='_x0000_s1025' alt='' style='position:absolute;left:");
  93.   document.write(table_x + left_width);
  94.   document.write("px;top:");
  95.   document.write(table_y + temp_space);
  96.   document.write("px;width:" + all_width * (total[0][i] / temp3) + "px;height:" + table_width + "px;z-index:1' fillcolor='" + tb_color[1][i] + "'>");
  97.   document.write("<v:fill color2='" + tb_color[0][i] + "' rotate='t' angle='-90' focus='100%' type='gradient'/>");
  98.   document.write("<o:extrusion v:ext='view' backdepth='" + thickness + "pt' color='" + tb_color[1][i] + "' on='t'/>");
  99.   document.write("</v:rect>");
  100.   document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + (table_x + left_width + all_width * (total[0][i] / temp3) + thickness / 2) + "px;top:" + (table_y + temp_space) + "px;width:" + (table_space + 15) + "px;height:18px;z-index:1'>");
  101.   document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='center'>" + total[0][i] + "</td></tr></table></v:textbox></v:shape>");
  102.   document.write("<v:shape id='_x0000_s1025' type='#_x0000_t202' alt='' style='position:absolute;left:" + table_x + "px;top:" + (table_y + temp_space) + "px;width:" + left_width + "px;height:18px;z-index:1'>");
  103.   document.write("<v:textbox inset='0px,0px,0px,0px'><table cellspacing='3' cellpadding='0' width='100%' height='100%'><tr><td align='right'>" + total[1][i] + "</td></tr></table></v:textbox></v:shape>");
  104.  }
  105.   
  106.  }
  107. }
  108. </script>
  109. <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
  110. <!--[if !mso]>
  111. <style>
  112. v/:*         { behavior: url(#default#VML) }
  113. o/:*         { behavior: url(#default#VML) }
  114. .shape       { behavior: url(#default#VML) }
  115. </style>
  116. <![endif]-->
  117. <head>
  118. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  119. <title></title>
  120. <style>
  121. TD { FONT-SIZE: 9pt}
  122. </style></head>
  123. <body topmargin=5 leftmargin=0 scroll=AUTO>
  124. <script language=javascript>
  125. //=============調用方法=====================
  126. /*
  127. 1、修改dataArray和nameArray對於數據
  128. 2、修改table1函數理的tmdColor2對於的顏色代碼
  129. 3、"A""B"兩種類別圖
  130. */
  131. var dataArray = new Array()
  132. dataArray[0]=100.222
  133. dataArray[1]=2000
  134. dataArray[2]=3000
  135. dataArray[3]=4000
  136. dataArray[4]=5000
  137. dataArray[5]=6000
  138. dataArray[6]=7000
  139. dataArray[7]=8000
  140. var nameArray = new Array()
  141. nameArray[0]="經營報"
  142. nameArray[1]="招聘網"
  143. nameArray[2]="51Job"
  144. nameArray[3]="新民晚報"
  145. nameArray[4]="新聞晚報"
  146. nameArray[5]="南方週末"
  147. nameArray[6]="羊城晚報"
  148. nameArray[7]="晚報"
  149. var total= new Array(dataArray,nameArray)
  150. table1(total,200,50,20,35,500,200,"A");
  151. table1(total,200,320,20,20,400,250,"B")
  152. //倒立柱狀圖dataArray顯示順序錯了
  153. //table1(total,table_x,table_y,thickness,table_width,all_width,all_height,table_type){
  154. //參數含義(傳遞的數組,橫座標,縱座標,柱子的厚度,柱子的寬度,圖表的寬度,圖表的高度,圖表的類型)
  155. </script>
  156. </body>
  157. </html>

http://www.cnblogs.com/zhongcj/archive/2008/03/07/1094632.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章