tb.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. function initTb(id,FENSHU,ftext,btext){
  2. var bs = 3;
  3. //canvas initialization
  4. var canvas = document.getElementById(id);
  5. var ctx = canvas.getContext("2d");
  6. //dimensions
  7. var s1 = 170,s2=170;
  8. canvas.width = s1*bs;
  9. canvas.height = s2*bs;
  10. var W = canvas.width;
  11. var H = canvas.height;
  12. var cpt = canvas.parentNode;
  13. cpt.style.width = s1+"px";
  14. cpt.style.height = s2+"px";
  15. canvas.style["transform"] = canvas.style["-webkit-transform"] = canvas.style["-moz-transform"] = "scale("+1/bs+") translate(-510px,-510px)";
  16. //Variables
  17. var degrees = 0;
  18. var new_degrees = 0;
  19. var difference = 0;
  20. var color = FENSHU>=50?"#00bc8c":"#00bc8c"; //green looks better to me
  21. var bgcolor = "#cce9e2";
  22. var text;
  23. var animation_loop, redraw_loop;
  24. var new_fs = FENSHU;
  25. if(new_fs==0){
  26. new_fs=0.5;
  27. }
  28. function init(){
  29. //Clear the canvas everytime a chart is drawn
  30. ctx.clearRect(0, 0, W, H);
  31. //Background 360 degree arc
  32. var r = 50;
  33. ctx.beginPath();
  34. ctx.strokeStyle = bgcolor;
  35. ctx.lineJoin = "round";
  36. ctx.lineWidth = 4*bs; //预填充环的宽度
  37. ctx.arc(W/2, H/2, r*bs, 0, Math.PI*2, false); //you can see the arc now
  38. ctx.stroke();
  39. ctx.beginPath();
  40. //gauge will be a simple arc
  41. //Angle in radians = angle in degrees * PI / 180
  42. var radians = degrees * Math.PI / 180;
  43. ctx.beginPath();
  44. ctx.strokeStyle = color;
  45. ctx.lineWidth = 8*bs; //填充环的宽度
  46. //The arc starts from the rightmost end. If we deduct 90 degrees from the angles
  47. //the arc will start from the topmost end
  48. ctx.arc(W/2, H/2, r*bs, 0 - 90*Math.PI/180, radians - 90*Math.PI/180, false);
  49. //you can see the arc now
  50. if(new_fs>0.5)
  51. ctx.stroke();
  52. //Lets add the text
  53. ctx.fillStyle = "#00bc8c";
  54. //Lets center the text
  55. //deducting half of text width from position x
  56. var fm = '"Lantinghei SC","Microsoft Yahei","Hiragino Sans GB","Helvetica Neue",Helvetica,Arial,sans-serif';
  57. ctx.font = 18*bs+"px "+fm;
  58. text_width = ctx.measureText(ftext).width;
  59. //adding manual value to position y since the height of the text cannot
  60. //be measured easily. There are hacks but we will keep it manual for now.
  61. ctx.fillText(ftext, W/2 - text_width/2, H/2 + 10*bs);
  62. text = btext;
  63. ctx.font = 16*bs+"px "+fm;
  64. //ctx.fillStyle = "#555555";
  65. text_width = ctx.measureText(text).width;
  66. ctx.fillText(text, W/2 - text_width/2, H/2 + 80*bs);
  67. }
  68. function draw(){
  69. //Cancel any movement animation if a new chart is requested
  70. if(typeof animation_loop != undefined) clearInterval(animation_loop);
  71. new_degrees=new_degrees+1;
  72. difference = new_degrees - degrees;
  73. animate_to();
  74. }
  75. //function to make the chart move to new degrees
  76. function animate_to(){
  77. //clear animation loop if degrees reaches to new_degrees
  78. var fs = parseInt((new_fs/100)*360);
  79. if(degrees == fs)
  80. clearInterval(animation_loop);
  81. if(degrees==fs)//如果加载到了360度,就停止
  82. return;
  83. if(degrees < new_degrees)
  84. degrees++;
  85. else
  86. degrees--;
  87. degrees = fs;
  88. init();
  89. }
  90. //调用各个函数,实现动态效果
  91. draw();
  92. //redraw_loop = setInterval(draw, 10); //Draw a new chart every 2 seconds
  93. }
  94. function initTbOrange(id,FENSHU,ftext,btext){
  95. var bs = 3;
  96. //canvas initialization
  97. var canvas = document.getElementById(id);
  98. var ctx = canvas.getContext("2d");
  99. //dimensions
  100. var s1 = 170,s2=170;
  101. canvas.width = s1*bs;
  102. canvas.height = s2*bs;
  103. var W = canvas.width;
  104. var H = canvas.height;
  105. var cpt = canvas.parentNode;
  106. cpt.style.width = s1+"px";
  107. cpt.style.height = s2+"px";
  108. canvas.style["transform"] = canvas.style["-webkit-transform"] = canvas.style["-moz-transform"] = "scale("+1/bs+") translate(-510px,-510px)";
  109. //Variables
  110. var degrees = 0;
  111. var new_degrees = 0;
  112. var difference = 0;
  113. var color = FENSHU>=50?"#00bc8c":"#ff6938"; //green looks better to me
  114. var bgcolor = "#cce9e2";
  115. var text;
  116. var animation_loop, redraw_loop;
  117. var new_fs = FENSHU;
  118. function init(){
  119. //Clear the canvas everytime a chart is drawn
  120. ctx.clearRect(0, 0, W, H);
  121. //Background 360 degree arc
  122. var r = 50;
  123. ctx.beginPath();
  124. ctx.strokeStyle = bgcolor;
  125. ctx.lineJoin = ctx.lineCap = "round";
  126. ctx.lineWidth = 4*bs; //预填充环的宽度
  127. ctx.arc(W/2, H/2, r*bs, 0, Math.PI*2, false); //you can see the arc now
  128. ctx.stroke();
  129. ctx.beginPath();
  130. //gauge will be a simple arc
  131. //Angle in radians = angle in degrees * PI / 180
  132. var radians = degrees * Math.PI / 180;
  133. ctx.beginPath();
  134. ctx.strokeStyle = color;
  135. ctx.lineWidth = 8*bs; //填充环的宽度
  136. //The arc starts from the rightmost end. If we deduct 90 degrees from the angles
  137. //the arc will start from the topmost end
  138. ctx.arc(W/2, H/2, r*bs, 0 - 90*Math.PI/180, radians - 90*Math.PI/180, false);
  139. //you can see the arc now
  140. ctx.stroke();
  141. //Lets add the text
  142. ctx.fillStyle = "#00bc8c";
  143. //Lets center the text
  144. //deducting half of text width from position x
  145. var fm = '"Lantinghei SC","Microsoft Yahei","Hiragino Sans GB","Helvetica Neue",Helvetica,Arial,sans-serif';
  146. ctx.font = 18*bs+"px "+fm;
  147. text_width = ctx.measureText(ftext).width;
  148. //adding manual value to position y since the height of the text cannot
  149. //be measured easily. There are hacks but we will keep it manual for now.
  150. ctx.fillText(ftext, W/2 - text_width/2, H/2 + 10*bs);
  151. text = btext;
  152. ctx.font = 16*bs+"px "+fm;
  153. ctx.fillStyle = "#b8542e";
  154. text_width = ctx.measureText(text).width;
  155. ctx.fillText(text, W/2 - text_width/2, H/2 + 80*bs);
  156. }
  157. function draw(){
  158. //Cancel any movement animation if a new chart is requested
  159. if(typeof animation_loop != undefined) clearInterval(animation_loop);
  160. new_degrees=new_degrees+1;
  161. difference = new_degrees - degrees;
  162. animate_to();
  163. }
  164. //function to make the chart move to new degrees
  165. function animate_to(){
  166. //clear animation loop if degrees reaches to new_degrees
  167. var fs = parseInt((new_fs/100)*360);
  168. if(degrees == fs)
  169. clearInterval(animation_loop);
  170. if(degrees==fs)//如果加载到了360度,就停止
  171. return;
  172. if(degrees < new_degrees)
  173. degrees++;
  174. else
  175. degrees--;
  176. degrees = fs;
  177. init();
  178. }
  179. //调用各个函数,实现动态效果
  180. draw();
  181. //redraw_loop = setInterval(draw, 10); //Draw a new chart every 2 seconds
  182. }