How to set data types for my google chart columns?

1k Views Asked by At

I'm writing a new code for displaying a chart using data from mysql, however I wasn't able to get any trendlines appearing and apparently the reason is that the API sees my both columns as numbers, rather than a date and a number.

I tried simply putting braces around my column names and giving them some data types like this [{label: 'ActivationDate', type: 'date'},{label: 'dial count', type: 'number'}] but it renders another kind of a message which is "c.getTimezoneOffset is not a function×".

Any help with rewriting this would be appreciated. Thanks,

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

        google.charts.setOnLoadCallback(drawChart);
        function drawChart(){
            var data = new google.visualization.DataTable();
            var data = google.visualization.arrayToDataTable([
                ['ActivationDate','dial count'],
                <?php
                    while($row = mysqli_fetch_assoc($aresult)){
                        echo "['".$row["activationdate"]."', ".$row["dialcount"]."],";
                    }
                ?>
               ]);
1

There are 1 best solutions below

1
Rahul On

You can create JSON first with the data you have and put it in as jsonData.

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

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});
    }

Reference from link.