user2093576
user2093576

Reputation: 3092

Passing JSON in variable to HighCharts

  function b(data){
           document.write(data);
          $('#graphContainer').highcharts(data);
      }

I need to pass JSON to .highcharts() API.

Using document.write() i have printed that the value of data is:

{"chart" : { "type" : "line"} , "title" : { "text" : "First Time Fix Rate"} , "xAxis" : { "title" : { "text" : "Time"} , "categories" : [ 2009.0 , 2010.0 , 2011.0 , 2012.0 , 2013.0]} , "yAxis" : { "title" : { "text" : "FTFR"}} , "plotOptions" : { "series" : { "cursor" : "pointer" , "point" : { "events" : { "click" :function () {alert (this.category+':' + this.y);}}}}} , "tooltip" : { "pointFormat" : "{series.name} produced {point.y:,.0f}
warheads in {point.x}"} , "series" : [ { "data" : [ 100.0 , 222.0 , 555.0 , 367.0 , 100.0]}]}

If I manually place this in

$('#graphContainer').highcharts(data);

removing the white spaces, graph is getting generated. but if want chart to be generated dynamically, its failing without any javascript error.

I tried json2.js, JSON.parse and Stringify, but that didn't solve the issue. Am i missing something?

If i set the data in session from my client code and access it using scriplet ie $('#graphContainer').highcharts(<%=session1.getAttribute("data")%>); it works fine. I wonder whats wrong with variables?

I cant use session here.

Upvotes: 2

Views: 686

Answers (2)

Sebastian Bochan
Sebastian Bochan

Reputation: 37588

Generally in json you cannot use functions, so it can cause problems.

Upvotes: 1

A Paul
A Paul

Reputation: 8271

I have created a chart from the json data from another source. But what I have done is, I have created the series array programmatically and then asigned the array in the Highchart chart' series value. Not sure though if this helps you.

Upvotes: 0

Related Questions