Home Forums Chart Support Cannot pass dataPoints.push() from string

Cannot pass dataPoints.push() from string

Viewing 6 posts - 1 through 6 (of 6 total)
  • #15333

    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();
    
    #15335

    @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.
    column chart with date time axis

    —-
    Bivek Singh,
    Team CanvasJS

    #15337

    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/

    • This reply was modified 7 years, 6 months ago by jhair01. Reason: more info to help mod determine
    • This reply was modified 7 years, 6 months ago by jhair01. Reason: typo
    #15341

    Hi Bivek,

    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??

    • This reply was modified 7 years, 6 months ago by jhair01. Reason: more info
    • This reply was modified 7 years, 6 months ago by jhair01. Reason: updated code
    #15351

    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

    #15371

    @jhair01

    Glad you figured it out.

    —-
    Bivek Singh,
    Team CanvasJS

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.