gpt4 book ai didi

d3.js - 如何调整 D3 中某个点的缩放大小?

转载 作者:行者123 更新时间:2023-12-04 10:40:28 24 4
gpt4 key购买 nike

这可能是“你做错了”的经典案例,但我迄今为止的所有搜索都没有得到任何帮助。

这是我的场景:

我正在使用albersUSA map 投影以及国家和县GeoJson 文件来绘制所有内容。

我还有一个自己创建的“城市”文件,其中包含每个州的主要城市。坐标是准确的,一切看起来都很好。

enter image description here

当用户点击给定的州时,我会隐藏所有州的形状,然后计算使该州的县形状适合我的视口(viewport)所需的变换。然后我将该变换应用于所有必要的县形状,以获得“缩放” View 。我的代码如下:

function CalculateTransform(objectPath)
{
var results = '';

// Define bounds/points of viewport
var mapDimensions = getMapViewportDimensions();
var baseWidth = mapDimensions[0];
var baseHeight = mapDimensions[1];

var centerX = baseWidth / 2;
var centerY = baseHeight / 2;

// Get bounding box of object path and calculate centroid and zoom factor
// based on viewport.
var bbox = objectPath.getBBox();
var centroid = [bbox.x + bbox.width / 2, bbox.y + bbox.height / 2];
var zoomScaleFactor = baseHeight / bbox.height;
var zoomX = -centroid[0];
var zoomY = -centroid[1];

// If the width of the state is greater than the height, scale by
// that property instead so that state will still fit in viewport.
if (bbox.width > bbox.height) {
zoomScaleFactor = baseHeight / bbox.width;
}

// Calculate how far to move the object path from it's current position to
// the center of the viewport.
var augmentX = -(centroid[0] - centerX);
var augmentY = -(centroid[1] - centerY);

// Our transform logic consists of:
// 1. Move the state to the center of the screen.
// 2. Move the state based on our anticipated scale.
// 3. Scale the state.
// 4. Move the state back to accomodate for the scaling.
var transform = "translate(" + (augmentX) + "," + (augmentY) + ")" +
"translate(" + (-zoomX) + "," + (-zoomY) + ")" +
"scale(" + zoomScaleFactor + ")" +
"translate(" + (zoomX) + "," + (zoomY) + ")";

return results;
}

...和绑定(bind)功能
// Load county data for the state specified.
d3.json(jsonUrl, function (json) {
if (json === undefined || json == null || json.features.length == 0)
{
logging.error("Failed to retrieve county structure data.");
showMapErrorMessage("Unable to retrieve county structure data.");
return false;
}
else
{
counties.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("id", function (d, i) {
return "county_" + d.properties.GEO_ID
})
.attr("data-id", function (d, i) { return d.properties.GEO_ID })
.attr("data-name", function (d, i) { return countyLookup[d.properties.GEO_ID] })
.attr("data-stateid", function (d, i) { return d.properties.STATE })
.attr("d", path);

// Show all counties for state specified and apply zoom transform.
d3.selectAll(countySelector).attr("visibility", "visible");
d3.selectAll(countySelector).attr("transform", stateTransform);

// Show all cities for the state specified and apply zoom transform
d3.selectAll(citySelector).attr("visibility", "visible");
d3.selectAll(citySelector).attr("transform", stateTransform);
}
});

enter image description here

这在这里工作得很好,除了非常小的状态,缩放因子要大得多,并且圆圈会失真。

enter image description here

即使在变换发生后,有没有办法强制点的大小为固定大小(比如 15px 半径)?

最佳答案

对于您不想缩放的东西,只需将它们除以 'scale' 。就我而言,

var zoom = d3.behavior.zoom()
.on("zoom",function() {
g.attr("transform","translate("+d3.event.translate.join(",")+")scale("+d3.event.scale+")");

g.selectAll(".mapmarker")
.attr("r",6/d3.event.scale)
.attr("stroke-width",1/d3.event.scale);

});

关于d3.js - 如何调整 D3 中某个点的缩放大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15141232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com