谷歌图表数组到数据表可能的维度问题

Googlecharts arraytodatatable possible dimension issue?

本文关键字:问题 数据表 数组 谷歌      更新时间:2023-09-26

我有一个从php得到的2个数组,放入一个2D Javascript数组中。我正在尝试使用它来自动填充Google图表数据表,但到目前为止我还没有成功。我唯一能想到的是,也许数组 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>

用于获取 nb样本和样本的代码:

echo '<script type="text/javascript">';
echo 'var samples = new Array("', join($ydata,'","'), '");';
echo 'var nbsamples = new Array("', join($nbsamples,'","'), '");';
echo '</script>';

尝试创建数组:

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]