This chart displays information from a Database. How do I display a more user-friendly message or not draw the chart at all if there are no results retrieved from the database? I already tried placing the lines of code to draw the chart inside the length check but the error message was still displayed.
google.charts.load('current', {'packages':['bar']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
$.getJSON('includes/get_info_bar_chart.php', function(jsonData){
var gglData = [];
if(jsonData.length > 0) {
// load column headings
var colHead = [];
Object.keys(jsonData[0]).forEach(function (key) {
colHead.push(key);
});
gglData.push(colHead);
// load data rows
jsonData.forEach(function (row) {
var gglRow = [];
Object.keys(row).forEach(function (key) {
gglRow.push(row[key]);
});
gglData.push(gglRow);
});
}
var data = google.visualization.arrayToDataTable(gglData);
var chart = new google.charts.Bar(document.getElementById('d_bar'));
chart.draw(data);
});
}
you could check the number of rows in the data table before drawing the chart.
if no data, then display a message in the chart's container.
var container = document.getElementById('d_bar');
var data = google.visualization.arrayToDataTable(gglData);
if (data.getNumberOfRows() > 0) {
var chart = new google.charts.Bar(container);
chart.draw(data);
} else {
container.innerHTML = 'No data found, nothing to see here...';
}