【发布时间】:2018-01-04 12:00:04
【问题描述】:
我目前正在 d3 中处理一个项目,其中我从 csv 文件创建了一个嵌套的 json 对象(参见代码 sn-p)它有多个键和一组值,我想在地图上绘制它们。
0:
key: "Islamic State of Iraq and the Levant (ISIL)"
values:
kills: 30889
map_data:
Array(4287)
[0 … 99]
0: {longitude: "44.290423", latitude: "33.297678", nkill: "28", nwound: "50", city: "Baghdad", …}
1: {longitude: "44.356585", latitude: "32.985052", nkill: "0", nwound: "4", city: "Latifiyah", …}
1:
key: "Taliban"
values:
kills: 24482
map_data:
Array(6575)
[0 … 99]
0: {longitude: "", latitude: "", nkill: "0", nwound: "0", city: "Unknown", …}
1: {longitude: "65.675942", latitude: "31.617667", nkill: "0", nwound: "0", city: "Kandahar", …}
[编辑] 我想为 map_data 数组中的每个项目投影一个圆圈,但圆圈属性只取一个值,正如 Shashank 的评论中所说。还有其他方法可以在属性中获取我的正确数据吗?
g.selectAll("circle")
.data(topgroups)
.enter()
.append("a")
.attr("xlink:href", function(d) {
return "https://www.google.com/search?q="+d.key;}
)
.append("circle")
.attr("cx", function(d) {
d.values.map_data.map(function (t) {
return projection([t.Longitude, t.Latitude])[0];
})
})
.attr("cy", function(d) {
d.values.map_data.map(function (t) {
return projection([t.Longitude, t.Latitude])[1];
})
})
.attr("r", function(d) {
d.values.map_data.map(function (t) {
return t.nkill/500;
})
})
.style("fill", "red");
【问题讨论】:
-
感谢您回复编辑。所以here 是如何为圆定义属性的。在您的情况下,
cx, cy and r通过执行d.values.map_data.map()被分配了一个数组,这是行不通的。您能否澄清一下:由于屏幕截图中的map_data是 4287 元素的Array,您是否尝试添加位于projection([longitude, latitude)[0]的 4287 圈子? -
我正在尝试为 map_data 数组中的每个项目投影一个圆圈
标签: javascript json d3.js