Php Google图表:如何将JSON数据传递给ChartWrapper()?
虽然我使用drawChart()函数成功地创建并显示了图表,但是,当我试图通过在dataSourceUrl的ChartWrapper()函数中传递相同的JSON来加载图表编辑器时,编辑器加载了,但图表没有加载。在使用图表编辑器时,通过传递JSON数据来检索图表,我应该在代码中更改什么?代码如下:Php Google图表:如何将JSON数据传递给ChartWrapper()?,php,json,ajax,charts,Php,Json,Ajax,Charts,虽然我使用drawChart()函数成功地创建并显示了图表,但是,当我试图通过在dataSourceUrl的ChartWrapper()函数中传递相同的JSON来加载图表编辑器时,编辑器加载了,但图表没有加载。在使用图表编辑器时,通过传递JSON数据来检索图表,我应该在代码中更改什么?代码如下: <?php $result = $conn->query('SELECT * FROM googlechart'); /* ------
<?php
$result = $conn->query('SELECT * FROM googlechart');
/*
---------------------------
example data: Table (googlechart)
--------------------------
weekly_task percentage
Sleep 30
Watching Movie 10
job 40
Exercise 20
*/
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'Weekly Task', 'type' => 'string'),
array('label' => 'Percentage', 'type' => 'number')
);
foreach($result as $r) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp[] = array('v' => (string) $r['weekly_task']);
// Values of each slice
$temp[] = array('v' => (int) $r['percentage']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
// convert data into JSON format
$jsonTable = json_encode($table);
echo $jsonTable;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
<html>
<head>
<!--Load the Ajax API-->
<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" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.0', {packages: ['charteditor']});
</script>
<script type="text/javascript">
google.setOnLoadCallback(loadEditor);
var chartEditor = null;
var data = new google.visualization.DataTable+'(<?php=$jsonTable; ?>)';
function loadEditor() {
// Create the chart to edit.
var wrapper = new google.visualization.ChartWrapper({
'chartType':'LineChart',
'dataSourceUrl':'data', //**This is the area where I've my doubt. //It's not accepting JSON data and showing 404 error.**
'options': {'title':'My Daily Routine', 'legend':'none'}
});
chartEditor = new google.visualization.ChartEditor();
google.visualization.events.addListener(chartEditor, 'ok', redrawChart);
chartEditor.openDialog(wrapper, {});
}
// On "OK" save the chart to a <div> on the page.
function redrawChart(){
chartEditor.getChartWrapper().draw(document.getElementById('chart_div'));
}
</script>
</head>
<body>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
load('visualization','1.0',{packages:['charteditor']});
setOnLoadCallback(loadEditor);
var chartreditor=null;
var data=new google.visualization.DataTable+'()';
函数loadEditor(){
//创建要编辑的图表。
var wrapper=new google.visualization.ChartWrapper({
“图表类型”:“线条图”,
'dataSourceUrl':'data',//**这是我怀疑的地方。//它不接受JSON数据并显示404错误**
'options':{'title':'My Daily Routine','legend':'none'}
});
chartEditor=新的google.visualization.chartEditor();
google.visualization.events.addListener(图表编辑器'ok',重画图表);
openDialog(包装,{});
}
//在“确定”时,将图表保存到页面上的a。
函数重绘图表(){
getChartWrapper().draw(document.getElementById('chart_div'));
}
您是否真的将数据源URL
设置为字符文本“数据”
?如果是这样的话,你得到404也就不足为奇了<代码>“数据”不是url!这是我真正想学的东西。我希望传递JSON数据,而不是从url传递数据。我应该做些什么改变来实现这一目标?更准确地说,我应该使用什么参数来代替dataSourceUrl?@Simon M Kenzie谢谢你,先生……它起作用了。我不敢相信我没有看到答案在同一页!