Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. function degToRad(degree) { return degree / 180 * Math.PI; };
  2.  
  3. function drawFilledSector(ctxObject, centerX, centerY, radius, startDegree, endDegree, style) {
  4. ctxObject.fillStyle = style;
  5. ctxObject.beginPath();
  6. ctxObject.arc(centerX, centerY, radius, degToRad(startDegree), degToRad(endDegree));
  7. ctxObject.lineTo(centerX, centerY);
  8. ctxObject.fill();
  9. }
  10.  
  11. function draw() {
  12. var canvasElement = document.getElementById('main-canvas');
  13. if (canvasElement.getContext('2d')) {
  14. /* Examples of drawing pie chart - Most Popular Browser Statistics in December 2016 */
  15. var ctx = canvasElement.getContext('2d');
  16. var startRadian, endRadian;
  17. /* Chrome - 73.7% */
  18. startDegree = 270;
  19. endDegree = 0.737 * 360 - 90;
  20. drawFilledSector(ctx, 250, 250, 150, startDegree, endDegree, '#56ff22')
  21.  
  22. /* Firefox - 15.5% */
  23. startDegree = endDegree;
  24. endDegree = (0.737 + 0.155) * 360 - 90;
  25. drawFilledSector(ctx, 250, 250, 150, startDegree, endDegree, '#4de51e')
  26.  
  27. /* IE - 4.8% */
  28. startDegree = endDegree;
  29. endDegree = (0.737 + 0.155 + 0.048) * 360 - 90;
  30. drawFilledSector(ctx, 250, 250, 150, startDegree, endDegree, '#44cc1b')
  31.  
  32. /* Safari - 3.5% */
  33. startDegree = endDegree;
  34. endDegree = (0.737 + 0.155 + 0.048 + 0.035) * 360 - 90;
  35. drawFilledSector(ctx, 250, 250, 150, startDegree, endDegree, '#3cb217')
  36.  
  37. /* Opera - 1.1% */
  38. startDegree = endDegree;
  39. endDegree = (0.737 + 0.155 + 0.048 + 0.035 + 0.011) * 360 - 90;
  40. drawFilledSector(ctx, 250, 250, 150, startDegree, endDegree, '#339914')
  41.  
  42. /* Other */
  43. startDegree = endDegree;
  44. endDegree = 270;
  45. drawFilledSector(ctx, 250, 250, 150, startDegree, endDegree, '#2b7f11');
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement