这是我的谷歌图表脚本。如果没有数据,我试图显示一个空图表,并隐藏消息“没有足够的列来绘制请求的图表”。你知道我的代码有什么问题吗?每当没有数据时,该错误消息仍然会出现。我引用过How to customize "not enough columns given to draw the requested chart" message?但它不适用于我的情况。谢谢。
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChartCampaign);
function errorHandler(errorMessage) {
//curisosity, check out the error in the console
console.log(errorMessage);
//simply remove the error, the user never see it
google.visualization.errors.removeError(errorMessage.id);
}
function drawChartCampaign() {
var data = google.visualization.arrayToDataTable(<?= generateChartOverview($row['campaignId'], $start_date, $end_date, false, true, $ad); ?>);
var options = {
title: '',
pointSize: 3,
animation: { duration: 1000, easing: 'out' },
hAxis: {title: 'Date', titleTextStyle: {color: '#999999'}<?php if($boolInterval){ echo " , showTextEvery : $showTextEvery"; } ?>},
vAxis: {title: 'Clicks / Views', titleTextStyle: {color: '#999999'}, viewWindowMode:'explicit', viewWindow:{ min:0 }}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div_adsdetails'));
//attach the error handler here, before draw()
google.visualization.events.addListener(chart, 'error', errorHandler);
chart.draw(data, options);
}
</script>
最佳答案
我管理这个错误,用谷歌事件监听器捕获它们
var chart = new google.visualization.LineChart(document.querySelector('#chartContainer'));
google.visualization.events.addListener(chart, 'error', function (googleError) {
google.visualization.errors.removeError(googleError.id);
//here you can write your custom message or nothing if you want remove
alert("error catch")
});
chart.draw(yourData, yourOptions);
关于javascript - 删除谷歌图表错误消息并在数据库中没有数据时显示空图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24156341/