如何使用Django将本地JSON对象加载到D3.js frontend? [英] How to load a local JSON object to D3.js frontend with Django?
本文介绍了如何使用Django将本地JSON对象加载到D3.js frontend?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用django后端创建一个d3.js可视化。
我试图将一些json渲染到地图中:
I'm trying to create a d3.js visualization with a django backend.
I'm trying to render some json into a map:
d3.json("world-countries.json", function(json) {
self.countries.selectAll("path")
.data(json.features)
.enter().append("path")
.attr("d", self.path)
.on("mouseover", function(d) {
d3.select(this).style("fill","#6C0");})
.on("mouseout", function(d) {
d3.select(this).style("fill","#000000");})
});
但我一直收到错误:
Uncaught TypeError: Cannot read property 'features' of null
我不知道如何通过Django访问json对象;有没有任何想法?
I'm not sure how to access json objects through Django; does anyone have any ideas?
推荐答案
检查您使用的是什么版本的D3。
我有同样的问题,结果是回调需要两个参数 - 错误和数据。
check what version of D3 you are using. I had the same problem and it turns out the callback needs two arguments - error and data.
示例:
d3.json("world-countries.json", function(error, json) {
// your code
}
因为第一个参数是'error' - 当没有错误时, json对象为null。
since the first argument is 'error' - when there is no error it appears that the json object is null.
这篇关于如何使用Django将本地JSON对象加载到D3.js frontend?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文