123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /* ------------------------------------------------------------------------------
- *
- * # D3.js - horizontal bar chart
- *
- * Demo d3.js horizontal bar chart setup with .csv data source
- *
- * ---------------------------------------------------------------------------- */
- // Setup module
- // ------------------------------
- var DashboardProgress = function() {
- //
- // Setup module components
- //
- // Rounded progress charts
- var _ProgressRoundedChart = function(element, radius, border, color, end, iconClass, textTitle, textAverage) {
- if (typeof d3 == 'undefined') {
- console.warn('Warning - d3.min.js is not loaded.');
- return;
- }
- // Initialize chart only if element exsists in the DOM
- if($(element).length > 0) {
- // Basic setup
- // ------------------------------
- // Main variables
- var d3Container = d3.select(element),
- startPercent = 0,
- iconSize = 32,
- endPercent = end,
- twoPi = Math.PI * 2,
- formatPercent = d3.format('.0%'),
- boxSize = radius * 2;
- // Values count
- var count = Math.abs((endPercent - startPercent) / 0.01);
- // Values step
- var step = endPercent < startPercent ? -0.01 : 0.01;
- // Create chart
- // ------------------------------
- // Add SVG element
- var container = d3Container.append('svg');
- // Add SVG group
- var svg = container
- .attr('width', boxSize)
- .attr('height', boxSize)
- .append('g')
- .attr('transform', 'translate(' + (boxSize / 2) + ',' + (boxSize / 2) + ')');
- // Construct chart layout
- // ------------------------------
- // Arc
- var arc = d3.svg.arc()
- .startAngle(0)
- .innerRadius(radius)
- .outerRadius(radius - border);
- //
- // Append chart elements
- //
- // Paths
- // ------------------------------
- // Background path
- svg.append('path')
- .attr('class', 'd3-progress-background')
- .attr('d', arc.endAngle(twoPi))
- .style('fill', color)
- .style('opacity', 0.2);
- // Foreground path
- var foreground = svg.append('path')
- .attr('class', 'd3-progress-foreground')
- .attr('filter', 'url(#blur)')
- .style('fill', color)
- .style('stroke', color);
- // Front path
- var front = svg.append('path')
- .attr('class', 'd3-progress-front')
- .style('fill', color)
- .style('fill-opacity', 1);
- // Text
- // ------------------------------
- // Percentage text value
- var numberText = d3.select(element)
- .append('h2')
- .attr('class', 'pt-1 mt-2 mb-1')
- // Icon
- d3.select(element)
- .append('i')
- .attr('class', iconClass + ' counter-icon')
- .attr('style', 'top: ' + ((boxSize - iconSize) / 2) + 'px');
- // Title
- d3.select(element)
- .append('div')
- .text(textTitle);
- // Subtitle
- d3.select(element)
- .append('div')
- .attr('class', 'font-size-sm text-muted mb-3')
- .text(textAverage);
- // Animation
- // ------------------------------
- // Animate path
- function updateProgress(progress) {
- foreground.attr('d', arc.endAngle(twoPi * progress));
- front.attr('d', arc.endAngle(twoPi * progress));
- numberText.text(formatPercent(progress));
- }
- // Animate text
- var progress = startPercent;
- (function loops() {
- updateProgress(progress);
- if (count > 0) {
- count--;
- progress += step;
- setTimeout(loops, 10);
- }
- })();
- }
- };
- //
- // Return objects assigned to module
- //
- return {
- init: function() {
- _ProgressRoundedChart('#hours-available-progress', 38, 2, '#F06292', 0.68, 'icon-watch text-pink-400', 'Hours available', '64% average');
- _ProgressRoundedChart('#goal-progress', 38, 2, '#5C6BC0', 0.82, 'icon-trophy3 text-indigo-400', 'Productivity goal', '87% average');
- }
- }
- }();
- // Initialize module
- // ------------------------------
- document.addEventListener('DOMContentLoaded', function() {
- DashboardProgress.init();
- });
|