function b(data){
document.write(data);
$('#graphContainer').highcharts(data);
}
我需要将 JSON 传递给 .highcharts() API。
使用 document.write() 我打印出数据的值为:
{"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]}]}
如果我手动把它放在
$('#graphContainer').highcharts(data);
删除空格,生成图表。但是如果想要动态生成图表,它会在没有任何 javascript 错误的情况下失败。
我尝试了 json2.js、JSON.parse 和 Stringify,但这并没有解决问题。我错过了什么吗?
如果我从我的客户端代码在 session 中设置数据并使用 scriplet 访问它,即 $('#graphContainer').highcharts(<%=session1.getAttribute("data")%>);
它工作正常。我想知道变量有什么问题?
我不能在这里使用 session 。
最佳答案
一般在json中你不能使用函数,所以它会导致问题。
关于javascript - 将变量中的 JSON 传递给 HighCharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20612820/