javascript - 将变量中的 JSON 传递给 HighCharts

标签 javascript jquery json highcharts

  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/

相关文章:

javascript - 是否可以将 stringByEvaluatingJavaScriptFromString 与引用本地文件的 Javascript 一起使用?

javascript - 对两个 ajax() 请求使用相同的回调会产生 "parsererror"

node.js - 文件上传后 jQuery 的 Ajax 回调未触发

javascript - 使用 CKEditor、BLOB 字段和 Rails 4 自定义上传图像

javascript - WebGL中GLMAT_ARRAY_TYPE()的作用是什么?

javascript - 使用 Worklight 适配器,我可以在运行时更改过程的域和端口吗?

json - 在单独的行上返回 Redshift JSON 数组的元素

ruby-on-rails - 使用 JBUILDER 呈现回形针 URL

javascript - 正则表达式在 C# 中工作正常,但在 Javascript 中不行

python : batch up a number of readings and send in json