我正在尝试使用谷歌图表在php和mysql中显示条形图。
这是我的代码
<?php
include('header.php');
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Month', 'Leads'],
/* ['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]*/
<?php echo implode(",", $myurl);
echo $myurl;
?>
]);
var options = {
title: 'Sales Graph',
vAxis: {title: 'Sales', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div class="container">
<div class="row-fluid">
<div class="span12">
<?php
if(isset($_POST['submit']))
{
$type = mysql_real_escape_string($_POST['whatt']);
$query = "SELECT YEAR(last_modified) as year, MONTH(last_modified) as month, COUNT(id) as count
FROM leads
WHERE lead_customer ='{$type}'
GROUP BY year, month
ORDER BY year, month";
echo $query;
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
// echo "Year: {$row['year']}, Month: {$row['month']}, Count: {$row['count']}<br>\n";
$myurl[] = "['".$row['month']."', ".$row['count']."]";
}
print_r($myurl);
echo implode(",", $myurl);
}
?>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
但我收到错误轴#0的数据列不能是字符串。 我检查了我的查询,这是对的。但我不明白为什么图表没有显示出来。
请建议我哪里出错了。
HTM数据
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Month', 'Leads'],
]);
var options = {
title: 'Sales Graph',
vAxis: {title: 'Sales', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>