【发布时间】:2017-06-27 00:10:11
【问题描述】:
我正在尝试为 d3 图动态生成 json 数据。我不确定这是否是最佳策略,但我的尝试本质上是将 d3 代码放在脚本标签中,然后通过 express 传递的对象生成 json。
我的问题是我不确定如何在脚本标签中动态生成 json 数据。如果有更好的代码设计策略来动态生成 json 对象以传递给 d3 以使用jade/pug 生成图表,请告诉我。下面的代码不起作用。
代码如下:
玉码
//...content above, d3 to draw the graph below
svg(height="800", width="1000").graph
script.
var svg = d3.select("svg"),
var data = #{dataObj}; // <--? how do I pass the json data object passed via route.js within the script tag?
...
route.js
let dataObj = {
"nodes": [
{"id": "Fantine", "group": 3},
{"id": "Blacheville", "group": 3},
{"id": "Favourite", "group": 3},
{"id": "test", "group": 3},
],
"links": [
{"source": "Fantine", "target": "Blacheville", "value": 3},
{"source": "Fantine", "target": "Favourite", "value": 4},
{"source": "Fantine", "target": "test", "value": 4},
]
};
res.render('drawGraph', { dataObj});
控制台
Uncaught SyntaxError: Unexpected identifier
var data = [object Object];
提前感谢您的帮助!
【问题讨论】:
标签: javascript node.js express d3.js pug