From 7f6bf1b07ffed6c8098af97fe831717999b182c6 Mon Sep 17 00:00:00 2001 From: Yingtong Li Date: Mon, 1 May 2023 18:28:42 +1000 Subject: [PATCH] Do not attempt to render budget graph when no data available --- sstreasury/static/sstreasury/budget.js | 88 ++++++++++++++------------ 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/sstreasury/static/sstreasury/budget.js b/sstreasury/static/sstreasury/budget.js index 7a569e9..6bcec5b 100644 --- a/sstreasury/static/sstreasury/budget.js +++ b/sstreasury/static/sstreasury/budget.js @@ -156,52 +156,56 @@ function makeGrid() { function makeCharts() { // Display expense, revenue charts on budget view page - new Chart(document.getElementById('chartExpenses'), { - type: 'pie', - data: { - labels: expense_data.map(e => e['Description']), - datasets: [{ - label: 'Expenses', - data: expense_data.map(e => e['Unit price'] * e['Units']) - }] - }, - options: { - plugins: { - tooltip: { - callbacks: { - label: i => '$' + i.parsed.toFixed(2) + if (document.getElementById('chartExpenses')) { + new Chart(document.getElementById('chartExpenses'), { + type: 'pie', + data: { + labels: expense_data.map(e => e['Description']), + datasets: [{ + label: 'Expenses', + data: expense_data.map(e => e['Unit price'] * e['Units']) + }] + }, + options: { + plugins: { + tooltip: { + callbacks: { + label: i => '$' + i.parsed.toFixed(2) + } } } - } - }, - }); - - new Chart(document.getElementById('chartRevExp'), { - type: 'bar', - data: { - labels: ['Revenue', 'Expenses'], - datasets: [{ - label: 'Budget', - data: [revTotal - revTotalIWT, (expTotal * (1 + emergency_fund_mult))], - backgroundColor: ['#36a2eb', '#ff6384'] - }] - }, - options: { - scales: { - y: { - beginAtZero: true - } }, - plugins: { - legend: { - display: false + }); + } + + if (document.getElementById('chartRevExp')) { + new Chart(document.getElementById('chartRevExp'), { + type: 'bar', + data: { + labels: ['Revenue', 'Expenses'], + datasets: [{ + label: 'Budget', + data: [revTotal - revTotalIWT, (expTotal * (1 + emergency_fund_mult))], + backgroundColor: ['#36a2eb', '#ff6384'] + }] + }, + options: { + scales: { + y: { + beginAtZero: true + } }, - tooltip: { - callbacks: { - label: i => '$' + i.parsed.y.toFixed(2) + plugins: { + legend: { + display: false + }, + tooltip: { + callbacks: { + label: i => '$' + i.parsed.y.toFixed(2) + } } } - } - }, - }); + }, + }); + } }