我正在使用 D3 制作一种特殊类型的 map ,它结合了多边形和点。
为此,我使用了两个 json 文件。第一个是多边形,其格式如下:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": "101", "name": "Place 1", "common_property":"P1" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ bla bla bla ] ] ] ] } },
{ "type": "Feature", "properties": { "id": "102", "name": "Place 2", "common_property":"P1" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ bla bla bla ] ] ] ] } },
{ "type": "Feature", "properties": { "id": "103", "name": "Place 3", "common_property":"P2" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ bla bla bla ] ] ] ] } }
]
}
另一方面,我有这个包含我的观点的 json 文件:
{"type": "FeatureCollection","crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "common_property": "P1" }, "geometry": { "type": "Point", "coordinates": [ 7.741, 48.58344 ] } },
{ "type": "Feature", "properties": { "common_property": "P2" }, "geometry": { "type": "Point", "coordinates": [ 7.76840, 48.58737 ] } }
]}
我的目标是在“common_property”值上链接鼠标悬停上的多边形和点:
- 当您将鼠标悬停在多边形上时,具有相同“common_property”的点具有不同的颜色
- 如果将鼠标悬停在某个点上,则具有相同“common_property”的所有多边形都具有相同的颜色。
理论上,如果我在脚本中做出良好的选择,事情就会很简单。但我的观点有问题。
首先,我制作了 css 表,并将投影、路径生成器、svg 等作为第一个变量...
然后,I used queuejs在我的主要功能之前读取几个文件:
queue()
.defer(d3.json,"data/polygons.json")
.defer(d3.json, 'data/points.json')
.await(function(err, polygons, points) {
THE CODE
});
在主函数中,我编写了这个用于显示多边形:
polyg = svg.append("g")
.selectAll("path")
.data(polygons.features)
.enter()
.append("path")
.attr("class", "polygon_area")
.attr("d", path)
这用于显示点:
places_point = svg.append("g")
.selectAll("path")
.data(points.features)
.enter()
.append("path")
.datum(function(d){
var origin = d.geometry.coordinates
var angle = scale*0.0000000018
return circle.angle(angle).origin(origin)(points.features)
})
.attr("class", "point")
.attr("d", path)
到目前为止,没什么复杂的,除了我没有 place_points 变量的属性。如果我测试一下:
polyg.on('mouseover', function(d) {
console.log(d)
places_point.attr("fill", function(d){
console.log(d)})
})
控制台显示:
Object { type: "Feature", properties: Object, geometry: Object }
Object { type: "Polygon", coordinates: Array[1] }
Object { type: "Polygon", coordinates: Array[1] }
所以它对于多边形来说效果很好,有属性和一切,但是对于点我输入的是“多边形”而不是“特征”,我不太明白为什么......
编辑:使用 D3 的 v3 来制作这个,而不是最后一个!
最佳答案
评论的新答案:
好吧,我知道这与 .datum
调用有关,但我最初的答案是不正确的。在这种情况下,您将使用 .datum
重新绑定(bind)数据并丢失您的properties 属性。因此,只需删除它并在路径 d
分配器中进行计算即可:
pt_bv = svg.append("g")
.selectAll("path")
.data(bv.features)
.enter()
.append("path")
.attr("class", "point")
.attr("d", function(d){
var origin = d.geometry.coordinates
var angle = scale * 0.0000000018
var rV = circle.angle(angle).origin(origin)(bv.features);
return path(rV);
});
bureaux.on('mouseover', function(d) {
console.log(d);
pt_bv.attr("fill", function(d) {
console.log(d);
})
});
这里有一些 runnable code .
关于javascript - D3 圆对象没有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40436622/