a. getTime is not a Function - Google Charts

Asked

Viewed 195 times

0

Good night

I wonder if anyone ever caught this mistake while creating the charts by Google Charts

a. getTime is not a Function

Return of the Json

{"cols":[{"label":"Nome","type":"string"},{"label":"Data","type":"date"},{"label":"Quantidade","type":"number"}],"rows":[{"c":[{"v":"Leonardo"},{"v":"2019-05-02"},{"v":"229"}]},{"c":[{"v":"Leonardo"}

<html>
  <head>
  <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  </head>
  <body>
    <div id="teste" style="width: 900px; height: 500px"></div>
    
    <script>

      google.charts.load('current', {'packages':['bar']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var jsonData = $.ajax({
                url: "control/getTeste.php",
                dataType: "json",
                async: false,
            }).responseText;

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

        
        

        var chart = new google.charts.Bar(document.getElementById('teste'));

        chart.draw(data, null);
      }
    </script>
    
    
  </body>
</html>

1 answer

0


I believe that as you stated the type of data date in your JSON, the code behind Google Charts is trying to call an existing method in the class Date on its date, but its date is a string, not a Date, therefore the mistake.

If you turn your string date on an object Date manually, error seems not to occur:

var jsonData = $.ajax({
    url: "control/getTeste.php",
    dataType: "json",
    async: false,
}).responseText;

jsonData.rows.forEach(r => r.c[1].v = new Date(r.c[1].v));

Another alternative would be to declare the date as type string.

  • Adjustment as string and worked @user140828

Browser other questions tagged

You are not signed in. Login or sign up in order to post.