错误:给定大小不同于5的行(表中的列数)

时间:2018-04-05 09:13:47

标签: javascript php charts google-visualization

很难理解一个,大概是基本的东西。你能帮帮我吗?

我正在尝试在Google Charts中创建折线图。由于某种原因,它不允许我使用NULL值。

首先我从Postgres通过PHP获取数据:

$rows = pg_fetch_all($fetch); 

然后我尝试绘制图表:

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {packages: ['corechart', 'line']});
      google.charts.setOnLoadCallback(drawBasic);

      function drawBasic() {

            var data = new google.visualization.DataTable();
            data.addColumn('string', 'X');
            data.addColumn('number', 'PD1');
            data.addColumn('number', 'CD1');
            data.addColumn('number', 'PD2');
            data.addColumn('number', 'CD2');

            data.addRows([<?php
            $result = '';
            foreach($rows as $data) {
              $result .= "['{$data['m']}', {$data['PD1']}, {$data['CD1']}, {$data['PD2']}, {$data['CD2']}],";
            }
            $result = rtrim($result,',');
            echo $result;
            ?>])

            var options = {
              hAxis: {
                title: 'T'
              },
              vAxis: {
                title: 'C'
              },
              interpolateNulls: true,
              width: 900,
              height: 500,
              title: 'Blahblah'
            };

            var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

            chart.draw(data, options);
          }
    </script>
  </head>
  <body>
    <div id="chart_div"></div>
  </body>
</html>

此尝试会导致以下错误:

jsapi_compiled_default_module.js:97 Uncaught (in promise) Error: Row given with size different than 5 (the number of columns in the table).
  at gvjs_P.gvjs_.fZ (jsapi_compiled_default_module.js:97)
  at gvjs_P.gvjs_.Op (jsapi_compiled_default_module.js:98)
  at drawBasic (view_charts.php:18)

当我的数组中的某个值丢失时会发生这种情况,例如:

data.addRows([['00:00', 85, 88, 6, 10],['00:01', 78, 86, 4, 9],['00:02', 110, 64, 8, 8],['00:03', 105, 82, , 4]])

如果我更改查询以选择所有值都存在的数据,则显示图表时没有任何问题。我认为传递空值很好,所以图表的某些部分会有空白点?将NULL值更改为0不是一个选项,因为结果会产生误导。

也许NULLS应该以某种特定的方式从PHP传递给JS?

提前致谢!

0 个答案:

没有答案