Charts "columnChart" and "stackedColumnChart" now support beforeDraw() which will check the data to see if the chart should be drawn. This cleans up the front page for new users and empty months.

This commit is contained in:
James Cole
2016-01-29 13:35:08 +01:00
parent 3d82afd4e6
commit 9ccb67db8a
2 changed files with 55 additions and 12 deletions

View File

@@ -204,8 +204,19 @@ function areaChart(URL, container, options) {
function columnChart(URL, container, options) { function columnChart(URL, container, options) {
"use strict"; "use strict";
options = options || {};
$.getJSON(URL).success(function (data) { $.getJSON(URL).success(function (data) {
var result = true;
if (options.beforeDraw) {
result = options.beforeDraw(data, {url: URL, container: container});
}
if (result === false) {
return;
}
console.log('Will draw columnChart(' + URL + ')');
var ctx = document.getElementById(container).getContext("2d"); var ctx = document.getElementById(container).getContext("2d");
var newData = {}; var newData = {};
newData.datasets = []; newData.datasets = [];
@@ -240,6 +251,15 @@ function stackedColumnChart(URL, container, options) {
$.getJSON(URL).success(function (data) { $.getJSON(URL).success(function (data) {
var result = true;
if (options.beforeDraw) {
result = options.beforeDraw(data, {url: URL, container: container});
}
if (result === false) {
return;
}
var ctx = document.getElementById(container).getContext("2d"); var ctx = document.getElementById(container).getContext("2d");
var newData = {}; var newData = {};
newData.datasets = []; newData.datasets = [];

View File

@@ -34,14 +34,37 @@ function drawChart() {
"use strict"; "use strict";
areaChart('chart/account/frontpage', 'accounts-chart'); areaChart('chart/account/frontpage', 'accounts-chart');
pieChart('chart/bill/frontpage', 'bills-chart'); pieChart('chart/bill/frontpage', 'bills-chart');
stackedColumnChart('chart/budget/frontpage', 'budgets-chart'); stackedColumnChart('chart/budget/frontpage', 'budgets-chart', {beforeDraw: beforeDrawIsEmpty});
columnChart('chart/category/frontpage', 'categories-chart'); columnChart('chart/category/frontpage', 'categories-chart', {beforeDraw: beforeDrawIsEmpty});
columnChart('chart/account/expense', 'expense-accounts-chart'); columnChart('chart/account/expense', 'expense-accounts-chart', {beforeDraw: beforeDrawIsEmpty});
getBoxAmounts(); getBoxAmounts();
} }
/**
* Removes a chart container if there is nothing for the chart to draw.
*
* @param data
* @param options
* @returns {boolean}
*/
function beforeDrawIsEmpty(data, options) {
"use strict";
// check if chart holds data.
if (data.labels.length === 0) {
// remove the chart container + parent
console.log(options.container + ' appears empty. Removed.');
$('#' + options.container).parent().parent().remove();
// return false so script stops.
return false;
}
return true;
}
function getBoxAmounts() { function getBoxAmounts() {
"use strict"; "use strict";
var boxes = ['in', 'out', 'bills-unpaid', 'bills-paid']; var boxes = ['in', 'out', 'bills-unpaid', 'bills-paid'];