【发布时间】:2020-12-29 17:09:42
【问题描述】:
有没有办法用 D3.js 将某些县分组在一起?
我正在显示美国的县级地图,我想在县组周围绘制边框或突出显示县组。
我会有一个包含每个县的数组(由d.id 在绘制县时分配),但我不确定如何定位该属性或在具有这些属性的一组元素周围绘制边框.
<script>
var svg = d3.select("svg");
var path = d3.geoPath();
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
// draw counties
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("d", path)
.on("click", clicked)
svg.append("path")
.attr("class", "county-borders")
.attr("d", path(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b; })));
// draw states
svg.append("g")
.attr("class", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path);
svg.append("path")
.attr("class", "state-borders")
.attr("d", path(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })));
function clicked(d) {
console.log(d.id);
}
var example_group = [45001, 45003, 45005, 45007, 45009, 45011];
});
</script>
【问题讨论】:
标签: javascript d3.js topojson