【发布时间】:2012-11-26 16:31:49
【问题描述】:
我有一个使用 SQL 数据的 Google 折线图。但是,当查询返回 0 行时,它会在页面上显示一个大的空图表。我想改为显示一些文本,说明没有数据。我尝试将图表函数包装在另一个函数中,如果数据存在则调用该函数,但即使数据存在,也没有显示任何内容。这是我的一些代码:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
function displayChart()
{
$(document).ready(function()
{
google.setOnLoadCallback(drawChart);
});
}
function drawChart()
{
// Here we tell it to make an ajax call to pull the data from the .json file
var jsonData = $.ajax({
url: "Data.json",
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Options for the graph, we set chartArea to get rid of extra whitespace
var options = {
'width':1300,
'height':500,
'chartArea': {top:'10%', left:'5%', height:'75%', width:'85%'}
};
// Instantiate and draw our chart, passing in some options and putting it in the chart_div.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
</script>
<?
...
if($got_data == true)
{
displayChart();
}
else
echo "There is no data";
知道我做错了什么,或者有更好的方法来完成这个吗?提前致谢!
【问题讨论】:
-
$got_data 的值是如何以及在哪里设置的?
-
使用$.ajax的回调代替同步调用
-
@Vishal Kumar $got_data 如果返回 1 行或更多行,则在 sql 查询后设置为 true。我已经检查过,这工作正常
标签: javascript function charts call