我有一个2个数组,我从PHP得到,放入一个2D Javascript数组。我正在尝试使用它来自动填充Google Chart DataTable,但到目前为止我还没有成功。 我唯一可以想到的是,可能是数组ix MxN维度,而函数需要一个NxM数组?
它是否不起作用,因为第一个数组是由数字组成的?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Pressure Monitor</title>
<script type="text/javascript">
var samptable = new Array();
samptable[0] = new Array(2);
samptable[0,0]= nbsamples; //first array obtained from php
samptable[0,1]= samples; //second array obtained from php. both are merged into a 2d array
</script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
//var data;
function drawVisualization() {
var data = google.visualization.arrayToDataTable(samptable[0]);
// Create and draw the visualization.
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {curveType: "function",
width: 500, height: 400,
vAxis: {maxValue: 10}}
);
}
//function draw(){
//}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 800px; height: 400px;"></div>
</body>
</html>
用于获取nbsamples和样本的代码:
echo '<script type="text/javascript">';
echo 'var samples = new Array("', join($ydata,'","'), '");';
echo 'var nbsamples = new Array("', join($nbsamples,'","'), '");';
echo '</script>';
答案 0 :(得分:0)
尝试使用它来创建数组:
var samptable = [];
samptable.push(nbsamples);
samptable.push(samples);
然后,由于您的数组似乎都是数据,请确保将* opt_firstRowIsData *选项指定为true(默认为false):
var data = google.visualization.arrayToDataTable(samptable, true);
您可能还希望通过将 nbsamples 和示例记录到控制台来验证它们是否为有效数组:
console.log(nbsamples);
console.log(samples);
为了使其有效,它们应该每个都显示为一行数据(相同数量的项目),例如:
[5, 4, 10]
[1, 3, 3]