我正在使用Google Charts创建折线图。但是在JavaScript部分我尝试将我的PHP变量传递到数组中,但是我收到一条错误消息:
没有足够的列来绘制所请求的图表。
我的代码:
> final
A B df1.x1 df1.x2 df2.x1 df2.x2 df3.x1 df3.x2
1 1 Q 0.45840140 0.67613686 0.45840140 0.67613686 0.45840140 0.67613686
2 1 Q 0.04184920 0.49420098 0.04184920 0.49420098 0.04184920 0.49420098
3 2 Q 0.79841159 0.53494071 0.79841159 0.53494071 0.79841159 0.53494071
4 2 P 0.89847872 0.22059712 0.89847872 0.22059712 0.89847872 0.22059712
5 3 P 0.06430706 0.48076174 0.06430706 0.48076174 0.06430706 0.48076174
6 3 P 0.17436400 0.02307715 0.17436400 0.02307715 0.17436400 0.02307715
每次运行它都会收到相同的错误消息。我尝试输出结果并将其直接复制并粘贴到JavaScript中,然后创建了图表。
为什么这不起作用的任何想法?
编辑:
数据:
<!-- Google Charts -->
<div id="chart_line_graph" style="width: 100%; height: 400px; margin: 0 auto;"></div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<?
$titleArray = array(
'Week'
);
$count = 0;
//Get all headers
$query = 'SELECT `keyword` FROM `'.$tableName.'` ORDER BY `keyword`';
$results = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($results)){
if($count < 5) //Get first 5 keywords
$titleArray[] = $row['keyword'];
$count++;
}
$data = array();
$data[] = json_encode($titleArray);
foreach($columns as $column){
$weekData = array();
$weekData[] = "'".$column."'";
$query = 'SELECT `'.$column.'` FROM `'.$tableName.'` ORDER BY `keyword`';
$results = mysqli_query($conn, $query);
$count = 0;
while($row = mysqli_fetch_assoc($results)){
if($count < 5)
$weekData[] = (Integer)$row[$column];
$count++;
}
$data[] = json_encode($weekData);
}
?>
<script>
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
<?php
foreach($data as $weekData){
echo $weekData.',';
}
?>
],false);
var options = {
title: '<?echo ucwords($_SESSION['clientName']);?> Keywords',
curveType: 'function',
legend: { position: 'right' }
};
var chart = new google.visualization.LineChart(document.getElementById('chart_line_graph'));
chart.draw(data, options);
}
</script>
答案 0 :(得分:0)
我明白了。出于某种原因,mysqli无法正常工作。我将它从程序方式改为面向对象方式,这一切都奏效了。我不知道为什么会这样,我才知道它有效。