asimryu

canvas chart-1

Jul 13th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.22 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>HTML5 Canvas</title>
  6. </head>
  7. <body>
  8.     <canvas id="mycanvas" width="500" height="500"></canvas>
  9.     <script>
  10.         var c = document.getElementById("mycanvas");
  11.         var ctx = c.getContext("2d");
  12.         var editors = ["Dreamweaver","SublimeText","Brackets","Notepad++","Atom"];
  13.         var colors = ["#00A9DB", "#D500DB", "#0062DB", "#DBA100", "#4A4A4A"]
  14.         var rates = [20,45,30,17,15];
  15.         var total = 0;
  16.         for(var i=0; i<rates.length; i++){
  17.             total += rates[i];
  18.         }
  19.         var start = 0;
  20.         var end = 0;
  21.         for(var j=0; j<rates.length; j++){
  22.             ctx.beginPath();
  23.             ctx.moveTo(250,250);
  24.             end = start + ( (rates[j]/total)*2*Math.PI );
  25.             ctx.arc(250,250,250,start,end);
  26.             ctx.fillStyle = colors[j];
  27.             ctx.fill();
  28.             var angle = start + 2*Math.PI*((rates[j]/total)/2);
  29.             var pos = getTextPos(250,250,250/2,angle);
  30.             ctx.fillStyle = "white";
  31.             ctx.font = "15px Arial";
  32.             ctx.textAlign = "center";
  33.             ctx.fillText(editors[j],pos[0],pos[1]);
  34.             console.log(pos[0] + "/" + pos[1]);
  35.             start = end;
  36.         }
  37.  
  38.         function getTextPos(x,y,r,angle){
  39.             var tx = x + Math.cos(angle)*r;
  40.             var ty = y + Math.sin(angle)*r;
  41.             return [tx, ty];
  42.         }
  43.  
  44.  
  45.     </script>
  46. </body>
  47. </html>
Advertisement
Add Comment
Please, Sign In to add comment