首先,让我们嵌套两个d3.json 函数:
d3.json("https://rawgit.com/mbostock/topojson/master/examples/world-50m.json", function(error, world) {
d3.json("data.json", function(coord){
//code here
});
});
这样,我们可以同时访问两个 JSON。
由于source 和target 都是地理坐标,我们会将您的JSON 更改为:
[{"source":[-74, 40],"destination":[37, 55]},
{"source":[20, 50],"destination":[-60, -39]},
{"source":[37, 55],"destination":[32, -15]},
//
{"source":[147, -37],"destination":[-80, 37]}]
因此,每个source 和destination 都将坐标作为一个数组。
然后,在您的代码中,我们附加路径:
var line = svg.selectAll(".paths")
.data(coord)
.enter()
.append("path");
这是你的小伙伴:https://plnkr.co/edit/Ax4Tby47lFlryzVWCHi2?p=preview