You must be logged in to post your query.
Home › Forums › Chart Support › Cannot pass dataPoints.push() from string
Tagged: .push, chart, dataPoints, date
I have spent a day working on this. When I pass the dataString variable it throws exception : 0x800a138f – JavaScript runtime error: Unable to get property ‘getTime’ of undefined or null reference. But when I past the value of the string it works. Please help! Fiddle code below:
var dataString = "{ x: new Date(2017,06,13), y: 490 } , { x: new Date(2017,06,14), y: 403 } , { x: new Date(2017,06,15), y: 452 }"; alert(dataString); var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, title: { text: "Checking Matching Count Totals" }, axisX: { valueFormatString: "DD-MMM" }, data: [{ type: "column", dataPoints: [] }] }); chart.options.data[0].dataPoints.push(dataString); chart.render();
@jhair01,
The dataPoints that you’re pushing into data is a string which should be an array of objects. You can change the string of dataPoints to array of objects and assign it directly to dataPoints of chart. Please, take a look at this JSFiddle.
—- Bivek Singh, Team CanvasJS
Hi Bivek,
Thank you. To give you a better idea of what is happening, the chart is rendered in the V part of an ASP.NET MVC. I am looking at the Object returned from the controller which is a C# string containing all the data. So my code looks like this:
var dataString = "@Model";
The problem is that the variable is a string so when the value is passed it has the quotes around it. Please look at this fiddle for me?? Thank you very much!
https://jsfiddle.net/gq7ucw53/3/
I have an update. I am passing the string and then pushing it into a value array in JS. Here is the value of the array in JS:
["{ x: new Date(2017,06,13), y: 490 }" , "{ x: new Date(2017,06,14), y: 403 }"]
This still throws an exception, “Unable to get property ‘getTime’ of undefined or null reference”. The format looks correct to me. The only thing I notice is that when I look in the debugger, array is surrounded by double quotes. Can you please help me again??
Namaste Bivek,
My solution was to Serialize the Data from a List and then pass it in the ViewBag to the View of the MVC. It works. Thank you for your time and your consideration.
Jon
@jhair01
Glad you figured it out.
You must be logged in to reply to this topic. Login/Register