且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误:轴#0的数据列不能为字符串类型

更新时间:2023-11-02 08:57:52

该错误表明y轴的列不能为字符串类型.

the error indicates that columns for the y-axis cannot be of type string.

该错误是由使用静态方法引起的-> arrayToDataTable

the error is caused by the use of static method --> arrayToDataTable

arrayToDataTable 尝试猜测要传递给该方法的数据类型.
如果无法确定类型,则默认为字符串.

arrayToDataTable tries to guess at what type of data is being passed to the method.
if it cannot determine the type, it defaults to string.

数组,只有一行数据.
它唯一需要使用的值是 null .
因此它无法正确确定类型,并且默认为字符串.

in the example of the "real" array, there is only one row of data.
and the only value it has to work with is null.
so it cannot properly determine the type and defaults to string.

您可以在以下工作摘要中看到此结果...

you can see this result in the following working snippet...

google.charts.load('current', {
  packages:['corechart']
}).then(function () {
  var jsonData = [
    ['Día', 'Enfado', 'Irritabilidad','Dolor', 'Ansiedad', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'],
    ['28',  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  180,  190,  null]
  ];

  var data = google.visualization.arrayToDataTable(jsonData);

  for (var i = 0; i < data.getNumberOfColumns(); i++) {
    console.log(i, data.getColumnType(i));
  }
});

<script src="https://www.gstatic.com/charts/loader.js"></script>

相反,您将需要手动构建数据表,
设置每列的特定列类型.

instead, you will need to build the data table manually,
setting the specific column type for each column.

var data = new google.visualization.DataTable();
data.addColumn('string', 'Día');            // <-- x-axis - string
data.addColumn('number', 'Enfado');         // <-- y-axis - number
data.addColumn('number', 'Irritabilidad');  // <-- y-axis - number
...

但是我们可以动态地建立数据表,
基于收到的json数据.

but we can build the data table dynamically,
based on the json data received.

var jsonData = [
  ['Día', 'Enfado', 'Irritabilidad','Dolor', 'Ansiedad', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'],
  ['28',  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  180,  190,  null]
];

var data = new google.visualization.DataTable();

jsonData.forEach(function (row, indexRow) {
  if (indexRow === 0) {
    row.forEach(function (column, indexCol) {
      if (indexCol === 0) {
        data.addColumn('string', column);
      } else {
        data.addColumn('number', column);
      }
    });
  } else {
    data.addRow(row);
  }
});

请参阅以下工作摘要...

see following working snippet...

google.charts.load('current', {
  packages:['corechart']
}).then(function () {
  var jsonData = [
    ['Día', 'Enfado', 'Irritabilidad','Dolor', 'Ansiedad', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'],
    ['28',  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  null,  180,  190,  null]
  ];

  var data = new google.visualization.DataTable();

  jsonData.forEach(function (row, indexRow) {
    if (indexRow === 0) {
      row.forEach(function (column, indexCol) {
        if (indexCol === 0) {
          data.addColumn('string', column);
        } else {
          data.addColumn('number', column);
        }
      });
    } else {
      data.addRow(row);
    }
  });

  var options = {
    chartArea: {
      left: 64,
      top: 48,
      right: 32,
      bottom: 64,
      height: '100%',
      width: '100%'
    },
    height: '100%',
    legend: {
      position: 'none'
    },
    width: '100%'
  };

  var chart = new google.visualization.ScatterChart(document.getElementById('chart'));
  chart.draw(data, options);
});

html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  overflow: hidden;
  padding: 0px 0px 0px 0px;
}

#chart {
  height: 100%;
  min-height: 400px;
}

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>