1 回答
TA贡献1877条经验 获得超1个赞
我已经使它与您提供的示例数据一起使用。您需要将 中的数字括起来.translate([width / 2, height / 2])。除此之外,我只需要将比例缩小一点以使其适合屏幕。
const jsonFeatures = [{
"type": "Feature",
"id": "49",
"properties": {
"name": "Utah",
"density": 34.30
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-112.164359, 41.995232],
[-111.047063, 42.000709],
[-111.047063, 40.998429],
[-109.04798, 40.998429],
[-109.053457, 39.125316],
[-109.058934, 38.27639],
[-109.042503, 38.166851],
[-109.042503, 37.000263],
[-110.499369, 37.00574],
[-114.048427, 37.000263],
[-114.04295, 41.995232],
[-112.164359, 41.995232]
]
]
}
}];
let width = 500,
height = 300;
// Projection from geoAlbersUsa
let projection = d3.geoAlbersUsa()
.translate([width / 2, height / 2])
.scale(1000);
// Path from geoPath with the previous projection
let path = d3.geoPath().projection(projection);
let svg = d3.select("svg").attr("width", width).attr("height", height);
svg.selectAll("path")
.data(jsonFeatures)
.enter()
.append("path")
.attr("d", path)
.style("fill", d => {
return d.properties.party;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.js"></script>
<svg></svg>
添加回答
举报