diff --git a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php index 4ea5a119cc..07eb4e0f31 100644 --- a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php +++ b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php @@ -180,11 +180,15 @@ class ChartJsCategoryChartGenerator implements CategoryChartGenerator // then, loop all entries and create datasets: foreach ($entries as $entry) { - $name = $entry['name']; - $spent = $entry['spent']; - $earned = $entry['earned']; - $data['datasets'][] = ['label' => 'Spent in category ' . $name, 'data' => array_values($spent)]; - $data['datasets'][] = ['label' => 'Earned in category ' . $name, 'data' => array_values($earned)]; + $name = $entry['name']; + $spent = $entry['spent']; + $earned = $entry['earned']; + if (array_sum(array_values($spent)) != 0) { + $data['datasets'][] = ['label' => 'Spent in category ' . $name, 'data' => array_values($spent)]; + } + if (array_sum(array_values($earned)) != 0) { + $data['datasets'][] = ['label' => 'Earned in category ' . $name, 'data' => array_values($earned)]; + } } $data['count'] = count($data['datasets']); diff --git a/public/js/reports/default/multi-year.js b/public/js/reports/default/multi-year.js index c7fed264f2..d43475f6c1 100644 --- a/public/js/reports/default/multi-year.js +++ b/public/js/reports/default/multi-year.js @@ -23,13 +23,39 @@ function drawChart() { // draw budget chart based on selected budgets: $('.budget-checkbox').on('change', updateBudgetChart); + selectBudgetsByCookie(); updateBudgetChart(); // draw category chart based on selected budgets: $('.category-checkbox').on('change', updateCategoryChart); + selectCategoriesByCookie(); updateCategoryChart(); } +function selectBudgetsByCookie() { + "use strict"; + var cookie = readCookie('multi-year-budgets'); + if (cookie !== null) { + var cookieArray = cookie.split(','); + for (var x in cookieArray) { + var budgetId = cookieArray[x]; + $('.budget-checkbox[value="' + budgetId + '"').prop('checked', true); + } + } +} + +function selectCategoriesByCookie() { + "use strict"; + var cookie = readCookie('multi-year-categories'); + if (cookie !== null) { + var cookieArray = cookie.split(','); + for (var x in cookieArray) { + var categoryId = cookieArray[x]; + $('.category-checkbox[value="' + categoryId + '"').prop('checked', true); + } + } +} + function updateBudgetChart() { "use strict"; console.log('will update budget chart.'); @@ -42,7 +68,7 @@ function updateBudgetChart() { } }); - if(budgets.length > 0) { + if (budgets.length > 0) { var budgetIds = budgets.join(','); @@ -54,6 +80,7 @@ function updateBudgetChart() { // draw chart. Redraw when exists? Not sure if we support that. columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart'); + createCookie('multi-year-budgets', budgets, 365); } else { // hide canvas, show message: $('#budgets-chart-message').show(); @@ -75,7 +102,7 @@ function updateCategoryChart() { } }); - if(categories.length > 0) { + if (categories.length > 0) { var categoryIds = categories.join(','); @@ -87,10 +114,42 @@ function updateCategoryChart() { // draw chart. Redraw when exists? Not sure if we support that. columnChart('chart/category/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + categoryIds, 'categories-chart'); + createCookie('multi-year-categories', categories, 365); } else { // hide canvas, show message: $('#categories-chart-message').show(); $('#categories-chart').hide(); } +} + + +function createCookie(name, value, days) { + "use strict"; + var expires; + + if (days) { + var date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = "; expires=" + date.toGMTString(); + } else { + expires = ""; + } + document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/"; +} + +function readCookie(name) { + "use strict"; + var nameEQ = encodeURIComponent(name) + "="; + var ca = document.cookie.split(';'); + for (var i = 0; i < ca.length; i++) { + var c = ca[i]; + while (c.charAt(0) === ' ') c = c.substring(1, c.length); + if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length)); + } + return null; +} + +function eraseCookie(name) { + createCookie(name, "", -1); } \ No newline at end of file diff --git a/public/js/reports/index.js b/public/js/reports/index.js index 6a961bd77a..c3c3a2b150 100644 --- a/public/js/reports/index.js +++ b/public/js/reports/index.js @@ -118,4 +118,5 @@ function readCookie(name) { if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length)); } return null; -} \ No newline at end of file +} +