progress.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 DashboardProgress = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Rounded progress charts
  15. var _ProgressRoundedChart = function(element, radius, border, color, end, iconClass, textTitle, textAverage) {
  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. startPercent = 0,
  27. iconSize = 32,
  28. endPercent = end,
  29. twoPi = Math.PI * 2,
  30. formatPercent = d3.format('.0%'),
  31. boxSize = radius * 2;
  32. // Values count
  33. var count = Math.abs((endPercent - startPercent) / 0.01);
  34. // Values step
  35. var step = endPercent < startPercent ? -0.01 : 0.01;
  36. // Create chart
  37. // ------------------------------
  38. // Add SVG element
  39. var container = d3Container.append('svg');
  40. // Add SVG group
  41. var svg = container
  42. .attr('width', boxSize)
  43. .attr('height', boxSize)
  44. .append('g')
  45. .attr('transform', 'translate(' + (boxSize / 2) + ',' + (boxSize / 2) + ')');
  46. // Construct chart layout
  47. // ------------------------------
  48. // Arc
  49. var arc = d3.svg.arc()
  50. .startAngle(0)
  51. .innerRadius(radius)
  52. .outerRadius(radius - border);
  53. //
  54. // Append chart elements
  55. //
  56. // Paths
  57. // ------------------------------
  58. // Background path
  59. svg.append('path')
  60. .attr('class', 'd3-progress-background')
  61. .attr('d', arc.endAngle(twoPi))
  62. .style('fill', color)
  63. .style('opacity', 0.2);
  64. // Foreground path
  65. var foreground = svg.append('path')
  66. .attr('class', 'd3-progress-foreground')
  67. .attr('filter', 'url(#blur)')
  68. .style('fill', color)
  69. .style('stroke', color);
  70. // Front path
  71. var front = svg.append('path')
  72. .attr('class', 'd3-progress-front')
  73. .style('fill', color)
  74. .style('fill-opacity', 1);
  75. // Text
  76. // ------------------------------
  77. // Percentage text value
  78. var numberText = d3.select(element)
  79. .append('h2')
  80. .attr('class', 'pt-1 mt-2 mb-1')
  81. // Icon
  82. d3.select(element)
  83. .append('i')
  84. .attr('class', iconClass + ' counter-icon')
  85. .attr('style', 'top: ' + ((boxSize - iconSize) / 2) + 'px');
  86. // Title
  87. d3.select(element)
  88. .append('div')
  89. .text(textTitle);
  90. // Subtitle
  91. d3.select(element)
  92. .append('div')
  93. .attr('class', 'font-size-sm text-muted mb-3')
  94. .text(textAverage);
  95. // Animation
  96. // ------------------------------
  97. // Animate path
  98. function updateProgress(progress) {
  99. foreground.attr('d', arc.endAngle(twoPi * progress));
  100. front.attr('d', arc.endAngle(twoPi * progress));
  101. numberText.text(formatPercent(progress));
  102. }
  103. // Animate text
  104. var progress = startPercent;
  105. (function loops() {
  106. updateProgress(progress);
  107. if (count > 0) {
  108. count--;
  109. progress += step;
  110. setTimeout(loops, 10);
  111. }
  112. })();
  113. }
  114. };
  115. //
  116. // Return objects assigned to module
  117. //
  118. return {
  119. init: function() {
  120. _ProgressRoundedChart('#hours-available-progress', 38, 2, '#F06292', 0.68, 'icon-watch text-pink-400', 'Hours available', '64% average');
  121. _ProgressRoundedChart('#goal-progress', 38, 2, '#5C6BC0', 0.82, 'icon-trophy3 text-indigo-400', 'Productivity goal', '87% average');
  122. }
  123. }
  124. }();
  125. // Initialize module
  126. // ------------------------------
  127. document.addEventListener('DOMContentLoaded', function() {
  128. DashboardProgress.init();
  129. });