我基本上遵循 Let's Make a Map 教程http://bost.ocks.org/mike/map/与世界地图。我想在教程中区分海岸线和陆地边界,并将国家代码添加为每个边界的类——这样当一个国家被突出显示时,填充和描边颜色都会改变。因为是世界地图,有太多国家手动做这个,但是我不知道怎么动态做。
我的代码的相关部分是:
var countryPaths = svg.append("g")
.attr("class", "countryPaths");
var countries = topojson.feature(world, world.objects.countries);
// add individual country paths
countryPaths.selectAll(".countries")
.data(countries.features)
.enter()
.append("path")
.attr("class", function(d) {return "country " + d.properties.adm0_a3; } )
.attr("d", path)
// add coastline
countryPaths.append("path")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a == b }))
.attr("d", path)
.attr("class","coastLine");
// add terrestrial borders
countryPaths.append("path")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return (a !== b)}))
.attr("d", path)
.attr("class", "country-boundary");
});
但是,当我这样做时,海岸线(和陆地边界)被创建为单一路径,因此无法将例如日本的海岸线更改为不同的颜色。有什么想法可以实现这一目标吗?