【发布时间】:2023-07-20 20:52:01
【问题描述】:
我正在使用 D3 和 topojson 过滤掉一部分美国县。我可以成功过滤出县并将它们绘制到 SVG 元素,但是如何更改地图边界以适应新选择的县?它将地图渲染为边界是所有美国县,而不是仅过滤的县。
var width=960,
height=600,
centered;
const svgElement = d3.select(ref.current)
.append("svg")
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", [0,0,width,height]);
var path = d3.geoPath();
d3.json("https://d3js.org/us-10m.v2.json").then((d) => {
d.objects.counties.geometries = d.objects.counties.geometries.filter(function(item) {
return props.data.indexOf(item.id) > -1
})
var counties = topojson.feature(d, d.objects.counties).features;
svgElement.append("g")
.attr("class", "filled-territory")
.selectAll("path")
.data(counties)
.enter()
.append("path")
.attr("d", path)
});
props.data 是一个县 ID 数组,例如 -
[
"45001",
"45003",
"45007",
"45083",
"45087",
"45091"
]
【问题讨论】:
标签: javascript d3.js topojson