You must be logged in to post your query.
Home › Forums › Chart Support › Uncaught TypeError: Cannot read property 'getTime' of undefined
Tagged: ajax call to update datapoints
I am facing an issue when updating the datapoints of canvas chart, and the error message says “Uncaught TypeError: Cannot read property ‘getTime’ of undefined”. Below is my html (I am making ajax call to update the datapoints)
<html>
<head>
<script type="text/javascript">
function GetGraph(val) {
$("#dialogOpen").dialog({
autoOpen: false,
modal: true,
title: "Graph"
});
$.ajax({
type: 'POST',
url: '/Ranking/ReturnKeywordGraph',
contentType: "application/json; charset=utf-8",
dataType: 'html',
data: '{keywordId: "' + val + '" }'
}).success(function (response) {
//$('#dialogOpen').html(response);
var chart = new CanvasJS.Chart("chartContainer",
{
title: {
},
animationEnabled: true,
axisX: {
interval: 3
},
axisY: {
},
legend: {
verticalAlign: "bottom",
horizontalAlign: "center"
},
data: [{
name: "Graph Overview",
showInLegend: true,
legendMarkerType: "square",
type: "area",
color: "#0990B0",
markerSize: 0,
dataPoints: [
response
]
}]
});
chart.render();
$('#dialogOpen').dialog('open');
}).error(function (response) {
$('#dialogOpen').html(response.responseText);
$('#dialogOpen').dialog('open');
});
}
</script>
</head>
<body>
<div id="dialogOpen" style="display: block; width:100%;">
<div id="chartContainer" style="height: 289px; width: 100%;">
</div>
</div>
</body>
</html>
The output I receive in response is “{x: new Date(2017, 10, 20),y: 10},{x: new Date(2017, 10, 17),y: 15},” which when replaced with “response” renders the chart successfully. But passing response to datapoints gives the error.
Any help ????
dataPoints is an array of objects. But as per the code shared above, it seems like you are passing a string
("{x: new Date(2017, 10, 20),y: 10},{x: new Date(2017, 10, 17),y: 15}"
) to dataPoints array instead of objects. Therefore, parsing the data and converting it to objects like {x: new Date(2017, 10, 20),y: 10},{x: new Date(2017, 10, 17),y: 15}
should work fine in your case. Here is an example you can refer.
If this doesn’t suit your requirements, Can you please create a JSFiddle, along with your sample data, reproducing the issue?
___
Suyash Singh
Team CanvasJS
@Suyash Singh
Thanks mate, I got it working, thanks for the helping response.
Hi Mudassir
I am facing same problem,can u tell me how did it worked
As you are also facing a similar issue, you might also be passing a string to dataPoints array instead of an object. In case you are fetching the data from a JSON data source, I would suggest parsing the received to the format accepted by CanvasJS. Please refer to the code snippet below for an example:
dataPoints : [ //array of dataPoint
{x: new Date(2019, 00, 28), y: 10}, //dataPoint
{x: new Date(2019, 00, 29), y: 15} //dataPoint
]
In case you’re still facing the issue, kindly create a JSFiddle reproducing the issue so that we can look into the code and chart-options being used by you, understand the scenario better and help you out?
____
Shashi Ranjan
Team CanvasJS
This error occurs in Chrome Browser when you read a property or call a method on an undefined object . Uncaught TypeError: Cannot read property of undefined error is probably easiest to understand from the perspective of undefined, since undefined is not considered an object type at all (but its own undefined type instead), and properties can only belong to objects within JavaScript. There are a few variations of this error depending on the property you are trying to access. Sometimes instead of undefined it will say null.
Hello sir,
Sir, I have also the same output “{‘x’:new Date(2020,02,24),’y’:2},”. As you said then I tried to parse the string after that then this error are come “Uncaught SyntaxError: Unexpected token e in JSON at position 6”.
I need your help!!
Can you kindly create a sample project reproducing the issue and share it with us over Google-Drive or Onedrive along with sample JSON data so that we can look into the code, run it locally to understand the scenario better and help you resolve?
—
Shashi Ranjan
Team CanvasJS
Tagged: ajax call to update datapoints
You must be logged in to reply to this topic.