pies.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # D3.js - horizontal bar chart
  4. *
  5. * Demo d3.js horizontal bar chart setup with .csv data source
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var DashboardPies = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Small progress pie chart
  15. var _ProgressPieChart = function(element, width, height, color) {
  16. if (typeof d3 == 'undefined') {
  17. console.warn('Warning - d3.min.js is not loaded.');
  18. return;
  19. }
  20. // Initialize chart only if element exsists in the DOM
  21. if($(element).length > 0) {
  22. // Basic setup
  23. // ------------------------------
  24. // Main variables
  25. var d3Container = d3.select(element),
  26. border = 2,
  27. radius = Math.min(width / 2, height / 2) - border,
  28. twoPi = 2 * Math.PI,
  29. progress = $(element).data('progress'),
  30. total = 100;
  31. // Construct chart layout
  32. // ------------------------------
  33. // Arc
  34. var arc = d3.svg.arc()
  35. .startAngle(0)
  36. .innerRadius(0)
  37. .outerRadius(radius)
  38. .endAngle(function(d) {
  39. return (d.value / d.size) * 2 * Math.PI;
  40. })
  41. // Create chart
  42. // ------------------------------
  43. // Add svg element
  44. var container = d3Container.append('svg');
  45. // Add SVG group
  46. var svg = container
  47. .attr('width', width)
  48. .attr('height', height)
  49. .append('g')
  50. .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
  51. //
  52. // Append chart elements
  53. //
  54. // Progress group
  55. var meter = svg.append('g')
  56. .attr('class', 'progress-meter');
  57. // Background
  58. meter.append('path')
  59. .attr('d', arc.endAngle(twoPi))
  60. .style('fill', 'none')
  61. .style('stroke', color)
  62. .style('stroke-width', 1.5);
  63. // Foreground
  64. var foreground = meter.append('path')
  65. .style('fill', color);
  66. // Animate foreground path
  67. foreground
  68. .transition()
  69. .ease('cubic-out')
  70. .duration(2500)
  71. .attrTween('d', arcTween);
  72. // Tween arcs
  73. function arcTween() {
  74. var i = d3.interpolate(0, progress);
  75. return function(t) {
  76. var currentProgress = progress / (100/t);
  77. var endAngle = arc.endAngle(twoPi * (currentProgress));
  78. return arc(i(endAngle));
  79. };
  80. }
  81. }
  82. };
  83. //
  84. // Return objects assigned to module
  85. //
  86. return {
  87. init: function() {
  88. _ProgressPieChart('#today-progress', 20, 20, '#7986CB');
  89. _ProgressPieChart('#yesterday-progress', 20, 20, '#7986CB');
  90. }
  91. }
  92. }();
  93. // Initialize module
  94. // ------------------------------
  95. document.addEventListener('DOMContentLoaded', function() {
  96. DashboardPies.init();
  97. });