| 1 |
/** |
| 2 |
* Dashboard monthly revenue chart (Chart.js). |
| 3 |
*/ |
| 4 |
(function () { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
function init() { |
| 8 |
if (typeof Chart === 'undefined' || typeof easyInvoiceDashboard === 'undefined') { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
var cfg = easyInvoiceDashboard; |
| 13 |
if (!cfg.hasData || !cfg.monthlyRevenue) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
var el = document.getElementById('revenue-chart'); |
| 18 |
if (!el) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
var monthly = cfg.monthlyRevenue; |
| 23 |
var months = Object.keys(monthly); |
| 24 |
if (!months.length) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
var currencySet = {}; |
| 29 |
months.forEach(function (m) { |
| 30 |
var row = monthly[m]; |
| 31 |
if (!row || typeof row !== 'object') { |
| 32 |
return; |
| 33 |
} |
| 34 |
Object.keys(row).forEach(function (code) { |
| 35 |
currencySet[code] = true; |
| 36 |
}); |
| 37 |
}); |
| 38 |
|
| 39 |
var currencies = Object.keys(currencySet); |
| 40 |
if (!currencies.length) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
var palette = [ |
| 45 |
'rgba(79, 70, 229, 0.85)', |
| 46 |
'rgba(34, 197, 94, 0.85)', |
| 47 |
'rgba(239, 68, 68, 0.85)', |
| 48 |
'rgba(245, 158, 11, 0.85)', |
| 49 |
'rgba(139, 92, 246, 0.85)', |
| 50 |
]; |
| 51 |
|
| 52 |
var datasets = currencies.map(function (currency, idx) { |
| 53 |
return { |
| 54 |
label: currency, |
| 55 |
data: months.map(function (m) { |
| 56 |
var cell = monthly[m] && monthly[m][currency]; |
| 57 |
if (!cell || cell.amount === undefined || cell.amount === null) { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
var n = parseFloat(cell.amount); |
| 61 |
return isNaN(n) ? 0 : n; |
| 62 |
}), |
| 63 |
backgroundColor: palette[idx % palette.length], |
| 64 |
borderColor: palette[idx % palette.length].replace('0.85', '1'), |
| 65 |
borderWidth: 1, |
| 66 |
}; |
| 67 |
}); |
| 68 |
|
| 69 |
var ctx = el.getContext('2d'); |
| 70 |
if (!ctx) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if (window.easyInvoiceDashboardChart) { |
| 75 |
window.easyInvoiceDashboardChart.destroy(); |
| 76 |
} |
| 77 |
|
| 78 |
window.easyInvoiceDashboardChart = new Chart(ctx, { |
| 79 |
type: 'bar', |
| 80 |
data: { |
| 81 |
labels: months, |
| 82 |
datasets: datasets, |
| 83 |
}, |
| 84 |
options: { |
| 85 |
responsive: true, |
| 86 |
maintainAspectRatio: false, |
| 87 |
scales: { |
| 88 |
y: { |
| 89 |
beginAtZero: true, |
| 90 |
ticks: { |
| 91 |
precision: 0, |
| 92 |
}, |
| 93 |
}, |
| 94 |
x: { |
| 95 |
grid: { |
| 96 |
display: false, |
| 97 |
}, |
| 98 |
}, |
| 99 |
}, |
| 100 |
plugins: { |
| 101 |
legend: { |
| 102 |
display: currencies.length > 1, |
| 103 |
position: 'top', |
| 104 |
}, |
| 105 |
tooltip: { |
| 106 |
mode: 'index', |
| 107 |
intersect: false, |
| 108 |
}, |
| 109 |
}, |
| 110 |
}, |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
if (document.readyState === 'loading') { |
| 115 |
document.addEventListener('DOMContentLoaded', init); |
| 116 |
} else { |
| 117 |
init(); |
| 118 |
} |
| 119 |
})(); |
| 120 |
|