最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: Data column(s) for axis #0 cannot be of type string - Stack Overflow

programmeradmin5浏览0评论

I'm trying to create a scatter chart with google charts and React/JS. I have made a test array to check if it was the correct way to send the data to the chart and it worked. But when I processed the real data and create a similar array, it gets me the error: "Data column(s) for axis #0 cannot be of type string".

This is the test array that works fine:

       let dataTest = [
        ['Día', 'Enfado', 'Irritabilidad','Dolor', 'Ansiedad'],
              ['1',  null, 10, 20, 30],
              ['2', 30, 20, 10, null],
             
            ]   
 

I'm trying to create a scatter chart with google charts and React/JS. I have made a test array to check if it was the correct way to send the data to the chart and it worked. But when I processed the real data and create a similar array, it gets me the error: "Data column(s) for axis #0 cannot be of type string".

This is the test array that works fine:

       let dataTest = [
        ['Día', 'Enfado', 'Irritabilidad','Dolor', 'Ansiedad'],
              ['1',  null, 10, 20, 30],
              ['2', 30, 20, 10, null],
             
            ]   
 

and this is the real one, which gives me the error:

[Real array that gives me the error][1]

If you pare both at the chrome console, they have the same structure, so I can't figure it out why isn't working: [Both arrays printed on chrome console][2]

Thank you very much in advance! [1]: https://i.sstatic/3efGN.png [2]: https://i.sstatic/hHzar.png

Share Improve this question edited Aug 25, 2020 at 11:33 WhiteHat 61.3k7 gold badges53 silver badges136 bronze badges asked Aug 25, 2020 at 9:38 JuaraJuara 974 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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

the error is caused by the use of static method --> 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.

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./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
...

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./charts/loader.js"></script>
<div id="chart"></div>

发布评论

评论列表(0)

  1. 暂无评论