JavaScript and CSS Pie chart

This started off as an experiment with a combination JQuery and WordPress custom fields. The idea was to total up the distances of all the walks on my other site, a walking blog and make something a bit more interesting than a simple table.

An obvious choice would be to use the new HTML5 canvas element however there's a great plugin which allows elements to be rotated rather than using the Mozilla/Web-kit CSS rules.it uses a combination of images layered on top of each other to mask, and display the animation.

The Image

An image sprite to create a pie chart

An image sprite to create a pie chart

The HTML

  1.  
  2. <div id="walkDistanceFrame">
  3. <div id="walkDistanceMask"></div>
  4. <div id="walkArrowLine" style="display: block; -moz-transform: rotate(6.98421deg);"></div>
  5. <div id="walkArrowLineMask"></div>
  6. <div style="display: none;" id="walkArrowLine2"></div>
  7. <div id="walkArrow"></div>
  8. <h3 title="click me..." id="walkDistance">131.73
  9. <span>miles</span></h3>
  10.  
  11. <a href="/about/#distance">Explain?</a>
  12. </div>
  13.  

The Javascript

  1.  
  2. $(document).ready(function() {
  3. $('#walkDistance').click(function(){animateHowFar();});
  4. animateHowFar();
  5. });
  6. var randValue = -1;
  7.  
  8. function animateHowFar(){
  9. var animTime = 2000;
  10. var calcDistance = 0;
  11. var myLocations = ['far around the moon',
  12. 'far along the M25',
  13. 'far along the thames',
  14. 'many marathons',
  15. 'far round an olympic swimming pool',
  16. 'far from Lands End to John o\'Groats' ];
  17.  
  18. var myDistances = [6790,176,215,26.2, 0.093,874];
  19. var myImages = ['a','b','c','d','e','f'];
  20. randValue = randValue + 1;
  21. if(randValue >= myLocations.length){
  22. randValue = 0;
  23. }
  24. var target = myDistances[randValue];
  25. var remainder = 0;
  26. var circuits = 0;
  27.  
  28. if(!runningAnim) {
  29. runningAnim = true;
  30. $('#walkAroundLoc').html(myLocations[randValue]);
  31. $('#walkArrowLine').hide();
  32. $('#walkArrowLine2').hide();
  33. $('#walkCircuits').hide();
  34.  
  35. if(target < walkDistance){
  36. remainder = (walkDistance % target);
  37. circuits = ((walkDistance) / target).toFixed(2);
  38. calcDistance = remainder;
  39. $('#walkCircuits').html('<strong>Circuits:</strong> '+circuits).show();
  40. } else {
  41. calcDistance = walkDistance;
  42. }
  43.  
  44. var deg = ((calcDistance/target)*360);
  45. var walkPercentage = (calcDistance/target);
  46.  
  47. var time1 = (animTime/deg) * 180;
  48. var time2 = (animTime/deg) * (deg - 180);
  49.  
  50. $('#walkArrow').transform({rotate: 0});
  51. $('#walkArrowLine').transform({rotate: 0});
  52. $('#walkArrowLine2').transform({rotate: 0});
  53.  
  54. $('#walkArrow').animate({rotate: deg+'deg'},animTime, 'easeOutBounce',function(){
  55. if(deg <= 180){
  56. $('#walkArrowLine').transform({rotate: deg+"deg"}).show();
  57. runningAnim = false;
  58. } else {
  59. $('#walkArrowLine').transform({rotate: "180deg"}).show();
  60. $('#walkArrowLine2').transform({rotate: (deg-180)+"deg"}).show();
  61. runningAnim = false;
  62. }
  63. });
  64. }
  65. }
  66.  

A working demo can be seen on the homepage of MudWetandBeers.com.

This entry was posted in Development, jQuery. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>