123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- /* ------------------------------------------------------------------------------
- *
- * # D3.js - horizontal bar chart
- *
- * Demo d3.js horizontal bar chart setup with .csv data source
- *
- * ---------------------------------------------------------------------------- */
- // Setup module
- // ------------------------------
- var DashboardDonuts = function() {
- //
- // Setup module components
- //
- // Marketing campaigns donut chart
- var _MarketingCampaignsDonutChart = function(element, size) {
- 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
- // ------------------------------
- // Add data set
- var data = [
- {
- "browser": "Google Adwords",
- "icon": "<i class='icon-google mr-2'></i>",
- "value": 1047
- }, {
- "browser": "Social media",
- "icon": "<i class='icon-share4 mr-2'></i>",
- "value": 2948
- }, {
- "browser": "Youtube video",
- "icon": "<i class='icon-youtube mr-2'></i>",
- "value": 3909
- }
- ];
- // Main variables
- var d3Container = d3.select(element),
- distance = 2, // reserve 2px space for mouseover arc moving
- radius = (size/2) - distance,
- sum = d3.sum(data, function(d) { return d.value; });
- // Colors
- var colorrange = ['#66BB6A','#9575CD','#FF7043'];
- // Tooltip
- // ------------------------------
- var tip = d3.tip()
- .attr('class', 'd3-tip')
- .offset([-10, 0])
- .direction('e')
- .html(function (d) {
- return '<ul class="list-unstyled mb-1">' +
- '<li>' + '<div class="font-size-base mb-1 mt-1">' + d.data.icon + d.data.browser + '</div>' + '</li>' +
- '<li>' + 'Visits: ' + '<span class="font-weight-semibold float-right">' + d.value + '</span>' + '</li>' +
- '<li>' + 'Share: ' + '<span class="font-weight-semibold float-right">' + (100 / (sum / d.value)).toFixed(2) + '%' + '</span>' + '</li>' +
- '</ul>';
- });
- // Create chart
- // ------------------------------
- // Add svg element
- var container = d3Container.append('svg').call(tip);
-
- // Add SVG group
- var svg = container
- .attr('width', size)
- .attr('height', size)
- .append('g')
- .attr('transform', 'translate(' + (size / 2) + ',' + (size / 2) + ')');
- // Construct chart layout
- // ------------------------------
- // Pie
- var pie = d3.layout.pie()
- .sort(null)
- .startAngle(Math.PI)
- .endAngle(3 * Math.PI)
- .value(function (d) {
- return d.value;
- });
- // Arc
- var arc = d3.svg.arc()
- .outerRadius(radius)
- .innerRadius(radius / 2);
- // Colors
- var colors = d3.scale.ordinal().range(colorrange);
- //
- // Append chart elements
- //
- // Group chart elements
- var arcGroup = svg.selectAll('.d3-arc')
- .data(pie(data))
- .enter()
- .append('g')
- .attr('class', 'd3-arc d3-slice-border')
- .style('cursor', 'pointer');
-
- // Append path
- var arcPath = arcGroup
- .append('path')
- .style('fill', function (d) { return colors(d.data.value); });
- // Add tooltip
- arcPath
- .on('mouseover', function (d, i) {
- // Transition on mouseover
- d3.select(this)
- .transition()
- .duration(500)
- .ease('elastic')
- .attr('transform', function (d) {
- d.midAngle = ((d.endAngle - d.startAngle) / 2) + d.startAngle;
- var x = Math.sin(d.midAngle) * distance;
- var y = -Math.cos(d.midAngle) * distance;
- return 'translate(' + x + ',' + y + ')';
- });
- })
- .on('mousemove', function (d) {
-
- // Show tooltip on mousemove
- tip.show(d)
- .style('top', (d3.event.pageY - 40) + 'px')
- .style('left', (d3.event.pageX + 30) + 'px');
- })
- .on('mouseout', function (d, i) {
- // Mouseout transition
- d3.select(this)
- .transition()
- .duration(500)
- .ease('bounce')
- .attr('transform', 'translate(0,0)');
- // Hide tooltip
- tip.hide(d);
- });
- // Animate chart on load
- arcPath
- .transition()
- .delay(function(d, i) { return i * 500; })
- .duration(500)
- .attrTween('d', function(d) {
- var interpolate = d3.interpolate(d.startAngle,d.endAngle);
- return function(t) {
- d.endAngle = interpolate(t);
- return arc(d);
- };
- });
- }
- };
- // Campaign status donut chart
- var _CampaignStatusDonutChart = function(element, size) {
- 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
- // ------------------------------
- // Add data set
- var data = [
- {
- "status": "Active campaigns",
- "icon": "<i class='icon-checkmark3 text-success mr-2'></i>",
- "value": 439
- }, {
- "status": "Closed campaigns",
- "icon": "<i class='icon-cross2 text-danger mr-2'></i>",
- "value": 290
- }, {
- "status": "Pending campaigns",
- "icon": "<i class='icon-history text-blue mr-2'></i>",
- "value": 190
- }, {
- "status": "Campaigns on hold",
- "icon": "<i class='icon-infinite text-grey mr-2'></i>",
- "value": 148
- }
- ];
- // Main variables
- var d3Container = d3.select(element),
- distance = 2, // reserve 2px space for mouseover arc moving
- radius = (size/2) - distance,
- sum = d3.sum(data, function(d) { return d.value; });
- // Colors
- var colorrange = ['#29B6F6', '#EF5350', '#81C784', '#999'];
- // Tooltip
- // ------------------------------
- var tip = d3.tip()
- .attr('class', 'd3-tip')
- .offset([-10, 0])
- .direction('e')
- .html(function (d) {
- return '<ul class="list-unstyled mb-1">' +
- '<li>' + '<div class="font-size-base mb-1 mt-1">' + d.data.icon + d.data.status + '</div>' + '</li>' +
- '<li>' + 'Total: ' + '<span class="font-weight-semibold float-right">' + d.value + '</span>' + '</li>' +
- '<li>' + 'Share: ' + '<span class="font-weight-semibold float-right">' + (100 / (sum / d.value)).toFixed(2) + '%' + '</span>' + '</li>' +
- '</ul>';
- });
- // Create chart
- // ------------------------------
- // Add svg element
- var container = d3Container.append('svg').call(tip);
-
- // Add SVG group
- var svg = container
- .attr('width', size)
- .attr('height', size)
- .append('g')
- .attr('transform', 'translate(' + (size / 2) + ',' + (size / 2) + ')');
- // Construct chart layout
- // ------------------------------
- // Pie
- var pie = d3.layout.pie()
- .sort(null)
- .startAngle(Math.PI)
- .endAngle(3 * Math.PI)
- .value(function (d) {
- return d.value;
- });
- // Arc
- var arc = d3.svg.arc()
- .outerRadius(radius)
- .innerRadius(radius / 2);
- // Colors
- var colors = d3.scale.ordinal().range(colorrange);
- //
- // Append chart elements
- //
- // Group chart elements
- var arcGroup = svg.selectAll('.d3-arc')
- .data(pie(data))
- .enter()
- .append('g')
- .attr('class', 'd3-arc d3-slice-border')
- .style('cursor', 'pointer');
-
- // Append path
- var arcPath = arcGroup
- .append('path')
- .style('fill', function (d) { return colors(d.data.value); });
- // Add tooltip
- arcPath
- .on('mouseover', function (d, i) {
- // Transition on mouseover
- d3.select(this)
- .transition()
- .duration(500)
- .ease('elastic')
- .attr('transform', function (d) {
- d.midAngle = ((d.endAngle - d.startAngle) / 2) + d.startAngle;
- var x = Math.sin(d.midAngle) * distance;
- var y = -Math.cos(d.midAngle) * distance;
- return 'translate(' + x + ',' + y + ')';
- });
- })
- .on('mousemove', function (d) {
-
- // Show tooltip on mousemove
- tip.show(d)
- .style('top', (d3.event.pageY - 40) + 'px')
- .style('left', (d3.event.pageX + 30) + 'px');
- })
- .on('mouseout', function (d, i) {
- // Mouseout transition
- d3.select(this)
- .transition()
- .duration(500)
- .ease('bounce')
- .attr('transform', 'translate(0,0)');
- // Hide tooltip
- tip.hide(d);
- });
- // Animate chart on load
- arcPath
- .transition()
- .delay(function(d, i) { return i * 500; })
- .duration(500)
- .attrTween('d', function(d) {
- var interpolate = d3.interpolate(d.startAngle,d.endAngle);
- return function(t) {
- d.endAngle = interpolate(t);
- return arc(d);
- };
- });
- }
- };
- // Tickets status donut chart
- var _TicketStatusDonutChart = function(element, size) {
- 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
- // ------------------------------
- // Add data set
- var data = [
- {
- "status": "Pending tickets",
- "icon": "<i class='icon-history text-blue mr-2'></i>",
- "value": 295
- }, {
- "status": "Resolved tickets",
- "icon": "<i class='icon-checkmark3 text-success mr-2'></i>",
- "value": 189
- }, {
- "status": "Closed tickets",
- "icon": "<i class='icon-cross2 text-danger mr-2'></i>",
- "value": 277
- }
- ];
- // Main variables
- var d3Container = d3.select(element),
- distance = 2, // reserve 2px space for mouseover arc moving
- radius = (size/2) - distance,
- sum = d3.sum(data, function(d) { return d.value; });
- // Colors
- var colorrange = ['#29B6F6','#66BB6A','#EF5350'];
- // Tooltip
- // ------------------------------
- var tip = d3.tip()
- .attr('class', 'd3-tip')
- .offset([-10, 0])
- .direction('e')
- .html(function (d) {
- return '<ul class="list-unstyled mb-1">' +
- '<li>' + '<div class="font-size-base mb-1 mt-1">' + d.data.icon + d.data.status + '</div>' + '</li>' +
- '<li>' + 'Total: ' + '<span class="font-weight-semibold float-right">' + d.value + '</span>' + '</li>' +
- '<li>' + 'Share: ' + '<span class="font-weight-semibold float-right">' + (100 / (sum / d.value)).toFixed(2) + '%' + '</span>' + '</li>' +
- '</ul>';
- });
- // Create chart
- // ------------------------------
- // Add svg element
- var container = d3Container.append('svg').call(tip);
-
- // Add SVG group
- var svg = container
- .attr('width', size)
- .attr('height', size)
- .append('g')
- .attr('transform', 'translate(' + (size / 2) + ',' + (size / 2) + ')');
- // Construct chart layout
- // ------------------------------
- // Pie
- var pie = d3.layout.pie()
- .sort(null)
- .startAngle(Math.PI)
- .endAngle(3 * Math.PI)
- .value(function (d) {
- return d.value;
- });
- // Arc
- var arc = d3.svg.arc()
- .outerRadius(radius)
- .innerRadius(radius / 2);
- // Colors
- var colors = d3.scale.ordinal().range(colorrange);
- //
- // Append chart elements
- //
- // Group chart elements
- var arcGroup = svg.selectAll('.d3-arc')
- .data(pie(data))
- .enter()
- .append('g')
- .attr('class', 'd3-arc d3-slice-border')
- .style('cursor', 'pointer');
-
- // Append path
- var arcPath = arcGroup
- .append('path')
- .style('fill', function (d) { return colors(d.data.value); });
- // Add tooltip
- arcPath
- .on('mouseover', function (d, i) {
- // Transition on mouseover
- d3.select(this)
- .transition()
- .duration(500)
- .ease('elastic')
- .attr('transform', function (d) {
- d.midAngle = ((d.endAngle - d.startAngle) / 2) + d.startAngle;
- var x = Math.sin(d.midAngle) * distance;
- var y = -Math.cos(d.midAngle) * distance;
- return 'translate(' + x + ',' + y + ')';
- });
- })
- .on('mousemove', function (d) {
-
- // Show tooltip on mousemove
- tip.show(d)
- .style('top', (d3.event.pageY - 40) + 'px')
- .style('left', (d3.event.pageX + 30) + 'px');
- })
- .on('mouseout', function (d, i) {
- // Mouseout transition
- d3.select(this)
- .transition()
- .duration(500)
- .ease('bounce')
- .attr('transform', 'translate(0,0)');
- // Hide tooltip
- tip.hide(d);
- });
- // Animate chart on load
- arcPath
- .transition()
- .delay(function(d, i) { return i * 500; })
- .duration(500)
- .attrTween('d', function(d) {
- var interpolate = d3.interpolate(d.startAngle,d.endAngle);
- return function(t) {
- d.endAngle = interpolate(t);
- return arc(d);
- };
- });
- }
- };
- //
- // Return objects assigned to module
- //
- return {
- init: function() {
- _MarketingCampaignsDonutChart('#campaigns-donut', 42);
- _CampaignStatusDonutChart('#campaign-status-pie', 42);
- _TicketStatusDonutChart('#tickets-status', 42);
- }
- }
- }();
- // Initialize module
- // ------------------------------
- document.addEventListener('DOMContentLoaded', function() {
- DashboardDonuts.init();
- });
|