当前位置:   article > 正文

原生js实现饼图_百分比 饼状 原生js

百分比 饼状 原生js

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>饼图</title>
  6. <style>
  7. #canvas{
  8. border:1px solid #ccc;
  9. }
  10. .coverDiv {
  11. position: absolute;
  12. display: none;
  13. z-index: 999999999;
  14. left: 0;
  15. top: 0;
  16. background-color: #fff;
  17. border-style: solid;
  18. white-space: nowrap;
  19. z-index: 9999999;
  20. background-color: rgba(50, 50, 50, 0.7);
  21. border-width: 0px;
  22. border-color: rgb(51, 51, 51);
  23. border-radius: 4px;
  24. color: rgb(255, 255, 255);
  25. font: 14px / 21px "Microsoft YaHei";
  26. padding: 5px;
  27. }
  28. .title {
  29. position: absolute;
  30. left: 300px;
  31. top: 35px;
  32. font-size: 26px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <canvas id="canvas"></canvas>
  38. <div class="coverDiv"></div>
  39. <div class="title">周一到周五的人数</div>
  40. </body>
  41. <script>
  42. var inputData =[21,12,51,35,63]
  43. var inputText =['星期一','星期二','星期三','星期四','星期五']
  44. var colorList=['#000','skyblue','#666','pink','red']
  45. var title='周一到周五的人数'
  46. //小矩形距横向间距
  47. var spaceX=10;
  48. //小矩形距纵向间距
  49. var spaceY=20;
  50. //小矩形的宽度
  51. var smallW=30;
  52. //小矩形的宽度
  53. this.smallH=15;
  54. // 鼠标移动是坐标
  55. var mousePosition= {}
  56. var mouseTimer
  57. // div覆盖显示
  58. var isSelect=false
  59. var coverDiv=document.querySelector('.coverDiv')
  60. var isMouseIn=false
  61. // 画布大小
  62. var canvasWidth='800';
  63. var canvasHeight='650';
  64. var canvas = document.getElementById('canvas');
  65. var context = canvas.getContext('2d');
  66. canvas.width=canvasWidth;
  67. canvas.height=canvasHeight;
  68. draw()
  69. function drawCircle(startAngle,endAngle,data,i) {
  70. context.beginPath();
  71. context.moveTo(400,350);
  72. context.arc(400, 350, 200, startAngle, endAngle, false);
  73. var isSelect=context.isPointInPath(mousePosition.x, mousePosition.y)
  74. if (isSelect) {
  75. context.closePath();
  76. context.beginPath();
  77. context.moveTo(400,350);
  78. context.arc(400, 350, 210, startAngle, endAngle, false);
  79. coverDiv.style.left=mousePosition.x+10+'px'
  80. coverDiv.style.top=mousePosition.y+10+'px'
  81. coverDiv.style.display='block'
  82. let rate=data.scale.toFixed(2)*100
  83. rate=rate.toFixed(0)
  84. coverDiv.innerHTML=`${title}<br>${inputText[i]}: ${inputData[i]} ${rate}%`
  85. }
  86. context.closePath();
  87. context.fillStyle = data.color;
  88. context.fill()
  89. context.strokeStyle = '#fff';
  90. // 留空白
  91. context.lineWidth = 2;
  92. context.stroke();
  93. }
  94. function handleData(inputData) {
  95. // 把inputData变成比例
  96. let dataNew = [];
  97. let total = 0;
  98. for (var i = 0; i < inputData.length; i++) {
  99. total += inputData[i];
  100. }
  101. var radianStart=0
  102. for (var i = 0; i < inputData.length; i++) {
  103. radianStart =radianStart+ inputData[i] / total * Math.PI * 2;
  104. let radian=radianStart-inputData[i] / total * Math.PI * 2/2
  105. dataNew[i] = {
  106. scale: inputData[i] / total,
  107. color: colorList[i],
  108. radian: radian
  109. }
  110. }
  111. return dataNew
  112. }
  113. function draw(){
  114. context.clearRect(0, 0, 800, 800);
  115. var data=handleData(inputData)
  116. var startAngle=0;
  117. var endAngle=0;
  118. for(let i=0;i<data.length;i++)
  119. {
  120. // 画线
  121. var edgeX = 250 * Math.cos(data[i].radian);
  122. var edgeY = 250 * Math.sin(data[i].radian);
  123. context.beginPath();
  124. context.moveTo(400, 350);
  125. context.lineTo(400 + edgeX, 350 + edgeY);
  126. context.font = '30px Palatino'
  127. context.strokeStyle=data[i].color
  128. context.fillStyle=data[i].color
  129. if(edgeX<0) {
  130. context.lineTo(400 + edgeX-50, 350 + edgeY);
  131. context.textAlign='end'
  132. context.textBaseline='middle'
  133. context.fillText(inputText[i],400 +edgeX-50, 350 + edgeY)
  134. }else {
  135. context.lineTo(400 + edgeX+50, 350 + edgeY);
  136. context.textAlign='start'
  137. context.textBaseline='middle'
  138. context.fillText(inputText[i],400 +edgeX+50, 350 + edgeY)
  139. }
  140. context.stroke();
  141. context.closePath();
  142. // 画圆
  143. endAngle=startAngle+data[i].scale*Math.PI*2;
  144. drawCircle(startAngle,endAngle,data[i],i);
  145. startAngle=endAngle;
  146. drawInfo(i,inputText[i],colorList[i])
  147. }
  148. }
  149. canvas.addEventListener('mousemove', (e) => {
  150. e = e || window.event;
  151. if ( e.ofSfsetX || e.offsetX==0 ) {
  152. mousePosition.x = e.offsetX;
  153. mousePosition.y = e.offsetY;
  154. } else if ( e.layerX || e.layerX==0 ){
  155. mousePosition.x = e.layerX;
  156. mousePosition.y = e.layerY;
  157. }
  158. draw();
  159. // 计算鼠标位置与圆心距离
  160. const distance = Math.sqrt((e.offsetX - 400) ** 2 + (e.offsetY - 350) ** 2);
  161. // 判断是否在饼图内部
  162. isMouseIn = distance < 200;
  163. if(!isMouseIn) {
  164. coverDiv.style.display='none'
  165. }
  166. // clearTimeout(mouseTimer);
  167. // mouseTimer = setTimeout(() => {
  168. // draw();
  169. // }, 12)
  170. });
  171. //添加说明
  172. function drawInfo (index,text,color) {
  173. context.beginPath();
  174. //画圆角小矩形
  175. // context.lineJoin="round";
  176. context.fillRect( spaceX, spaceY*index+ smallH, smallW, smallH);
  177. //画文字
  178. context.font = "12px 微软雅黑";
  179. context.fillStyle = color;
  180. context.textAlign="left";
  181. context.textBaseline="bottom";
  182. context.fillText(text, spaceX*2+ smallW, spaceY*index+ smallH*2);
  183. }
  184. </script>
  185. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/132628?site
推荐阅读
相关标签
  

闽ICP备14008679号