Javascript 带有Topojson的D3只渲染地图的一部分
我正试图创建一张美国的choropleth地图,并根据一些数据给它上色。因此,我得到数据(只是json文件): 然后在我的ready函数中,我这样做Javascript 带有Topojson的D3只渲染地图的一部分,javascript,d3.js,topojson,Javascript,D3.js,Topojson,我正试图创建一张美国的choropleth地图,并根据一些数据给它上色。因此,我得到数据(只是json文件): 然后在我的ready函数中,我这样做 function ready(error, us, education) { if (error) throw error; svg.append("g").selectAll( "path" ) .data(topojson.feature(us, us.objects.counties).features) .enter()
function ready(error, us, education) {
if (error) throw error;
svg.append("g").selectAll( "path" )
.data(topojson.feature(us, us.objects.counties).features)
.enter()
.append( "path" )
.attr("class", "county")
.attr( "fill", "red" )
.attr( "d", path )
(Mypath
变量定义在文件const path=d3.geoPath();
的顶部)
我得到了我的地图,但是上面有一些洞,就像有些县不渲染一样。我还没有实现着色,所以它应该是红色的,但是有很大的黑色片段(这也不会对mouseover
产生反应)。您可以在我的代码笔上看到:
请告诉我我的错误在哪里。您的问题在您的网格中:
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("d", path);
您需要在css中为其指定“无”填充:
.states {
fill:none;
}
或在其附加的代码中:
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr('fill','none')
.attr("d", path);
.非常感谢!
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr('fill','none')
.attr("d", path);