我正在遵循这个关于如何使用 d3.js 和 mapbox 制作地图的精彩示例:
https://franksh.com/posts/d3-mapboxgl/ https://franksh.com/posts/d3-mapboxgl/
它工作得很好,除了我想知道如何使用这个例子,使圆圈成比例符号,即圆圈的半径将反映数据值。
<div id="map"></div>
<script>
mapboxgl.accessToken = "YOUR_TOKEN";
var map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/streets-v9",
center: [-74.5, 40.0],
zoom: 9
});
var container = map.getCanvasContainer();
var svg = d3
.select(container)
.append("svg")
.attr("width", "100%")
.attr("height", "500")
.style("position", "absolute")
.style("z-index", 2);
function project(d) {
return map.project(new mapboxgl.LngLat(d[0], d[1]));
}
#Lat, long, and value
var data = [
[-74.5, 40.05, 23],
[-74.45, 40.0, 56],
[-74.55, 40.0, 1],
[-74.85, 40.0, 500],
];
var dots = svg
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("r", 20)
.style("fill", "#ff0000");
function render() {
dots
.attr("cx", function (d) {
return project(d).x;
})
.attr("cy", function (d) {
return project(d).y;
});
}
map.on("viewreset", render);
map.on("move", render);
map.on("moveend", render);
render(); // Call once to render
</script>
CSS:
#map {
position: relative;
z-index: 0;
width: 100%;
height: 500px;
}
如果我根据实际数据值设置半径,有些圆超级大,有些圆超级小:
.attr("r", function(d) { return d[2]; }) //trying to make radius a function of the actual value
同样,应用乘数效果也不好(同样,有些点超级大,有些点超级小):
.attr("r", function(d) { return d[2] * 2; })
我认为我需要做的是将数据域映射到表示半径的范围(即使用 d3 线性比例),但我不完全确定这里的最佳实践是什么。我见过使用的例子geoPath.pointRadius(function(d))
但我认为这并不适用于这里,因为我没有在这个特定示例中使用路径生成器。