javascriptcoldfusioncoldfusion-10cfchart

ColdFusion 10 CFCHART, how to pass dynamic variables via JavaScript API method?


I am using the cfchart tag in ColdFusion 10 (zingcharts). The page containing the chart is receiving data via websocket and I am trying to pass some of this data to the chart at regular intervals with JavaScript, using:

var x = data.number;
var handle = ColdFusion.Chart.getChartHandle();
handle.exec('mychart', 'appendseriesvalues', '{"plotindex": 0, "values": [x]}');

The above code works fine if I enter an actual number instead of x, for example "values": [1.1] works fine. However if I try and pass in a number by way of a dynamic variable, it does not work and throws this error:

SyntaxError: JSON.parse: unexpected character

I was wondering if anyone knows how to accomplish this?


Solution

  • You're passing a JSON string to handle.exec, which is turned into a JS object in a different place to where your x variable is defined, and thus causing the error.

    Instead, let x be resolved as part of the string and it should work:

    handle.exec( ... , '{"plotindex": 0, "values": ['+x+']}');