0

我有一个从 php 获得的 2 个数组,放入一个 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>';
4

1 回答 1

0

试试这个来创建数组:

var samptable = [];
samptable.push(nbsamples);
samptable.push(samples);

然后,由于您的数组似乎都是数据,请确保将 *opt_firstRowIsData* 选项指定为 true(默认为 false):

var data = google.visualization.arrayToDataTable(samptable, true);

您可能还想通过将nbsamplessamples记录到控制台来验证它们是否为有效数组:

console.log(nbsamples);
console.log(samples);

为了使其有效,它们应分别显示为一行数据(相同数量的项目),例如:

[5, 4, 10]
[1, 3, 3]
于 2012-06-04T04:06:12.573 回答