Why error "c.getTimezoneOffset is not a function"?
Asked Answered
N

2

6

I was try to make simple line graph using Google chart that retrieve data from mysql. My json work but when I try call it in full coding, it say "c.getTimezoneOffset is not a function".

Here my json php

<?php
include("config.php");

$query = "SELECT * FROM reading";
$sql = mysqli_query($con, $query);

if (!$sql) {
die("Error running $sql: " . mysql_error());
}

$results = array(
'cols' => array (
array('label' => 'Date', 'type' => 'datetime'),
array('label' => 'API', 'type' => 'number')
),
'rows' => array()
);
while($row = mysqli_fetch_assoc($sql)) {

$year=date("Y", strtotime($row['time']));
$month=date("m", strtotime($row['time']));
$day=date("d", strtotime($row['time']));
$hour=date("H", strtotime($row['time']));
$minute=date("i", strtotime($row['time']));
$second=date("s", strtotime($row['time']));

$results['rows'][] = array('c' => array(
array('v' => "time($year, $month, $day, $hour, $minute, $second)"),
array('v' => $row['api'])
));
}
$json = json_encode($results, JSON_NUMERIC_CHECK);
echo $json;
?>

Here my json

{"cols":[{"label":"Date","type":"datetime"},
"label":"API","type":"number"}],
"rows":[{"c":[{"v":"time(2016, 05, 22, 12, 24, 26)"},{"v":26}]},
{"c":[{"v":"time(2016, 05, 22, 12, 24, 41)"},{"v":26}]},
{"c":[{"v":"time(2016, 05, 22, 12, 24, 56)"},{"v":27}]}]}

And here is my html coding

<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {
$.ajax({
url: 'getData.php',
dataType: 'json',
success: function (jsonData) {
// 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.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
});
}

</script>
</head>

<body>
<div id="chart_div"></div>
</body>
</html>
Nathannathanael answered 22/5, 2016 at 8:51 Comment(1)
Are you calling c.getTimezoneOffset by yourself, and c comes from json object which you injected from PHP script, or that error comes from google lib?Pau
G
0

I had the same error and had to modify my JSON to generate a string that looked like this:

"c":[{"v":"Date(2018,9,16)","f":null}

This means that my code had to parse out the year, month and day in order to piece it back together. I am using JSP, but hopefully this helps somebody out there:

createDateObj.put("v", "Date("+ createYear + "," + createMonth + "," + createDay + ")");

instead of simply using the date returned from mySQL which was like this:

createDateObj.put("v", createDate);
Garneau answered 20/9, 2018 at 1:17 Comment(0)
G
0

It looks like the problem is with the calls to time(...); I modified them to instead instantiate new Date objects (e.g. Date(...)) and everything seems to work as expected.

Example Codepen here

I modified drawChart() to skip the ajax call and just use your example JSON directly; I also updated the calls to use the latest version of Google Charts API.

Gardenia answered 29/6, 2016 at 21:58 Comment(0)
G
0

I had the same error and had to modify my JSON to generate a string that looked like this:

"c":[{"v":"Date(2018,9,16)","f":null}

This means that my code had to parse out the year, month and day in order to piece it back together. I am using JSP, but hopefully this helps somebody out there:

createDateObj.put("v", "Date("+ createYear + "," + createMonth + "," + createDay + ")");

instead of simply using the date returned from mySQL which was like this:

createDateObj.put("v", createDate);
Garneau answered 20/9, 2018 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.