停止缩放D3世界地图中的气泡 [英] Stop zooming of bubbles in D3 World Map
问题描述
我目前正在处理D3世界地图,其中已将其缩放功能提升到了基于其点击的任何国家或县的边界水平.
I am currently working on a D3 world map in which I have brought in a zoom functionality up-to the boundary level of any country or county based on its click.
我添加了指向肯尼亚各县的泡泡,在添加的缩放功能上它得到了放大.但是,我想在地图缩放时停止泡泡的缩放.
I have Added Bubbles pointing the counties in Kenya,which gets enlarged on the zoom functionality that I have added.But I want to stop the zooming of bubbles,on zooming of the Map.
这是我目前的作品.
https://plnkr.co/edit/nZIlJxvU74k8Nmtpduzc?p=preview
下面是缩放和缩小的代码
And below is the code for zooming and zoom out
function clicked(d) {
var conditionalChange = d;
if(d.properties.hasOwnProperty("Country")){
var country = d.properties.Country;
var obj = data.objects.countries.geometries;
$.each(obj, function(key, value ) {
if(countries[key].properties.name == "Kenya")
{
conditionalChange = countries[key].geometry;
}
});
}
d = conditionalChange;
if (active.node() === this) return reset();
active.classed("active", false);
active = d3.select(this).classed("active", true);
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = 1.2/ Math.max(dx / width, dy / height),
translate = [width / 2 - scale * x, height / 2 - scale * y];
g.transition()
.duration(750)
.style("stroke-width", 1/ scale + "px")
.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
}
function reset() {
active.classed("active", false);
active = d3.select(null);
g.transition()
.duration(750)
.style("stroke-width", "1px")
.attr("transform", "");
}
推荐答案
您正在缩放整个 g
元素,这将有效地缩放地图.一切都会增加;但是,对于地图线,您已经调整了笔触以反映 g
比例因子的变化:
You are scaling the entire g
element, this effectively zooms the map. Everything will increase in size; however, for the map lines you have adjusted the stroke to reflect the change in g
scale factor:
g.transition()
.duration(750)
.style("stroke-width", 1/ scale + "px")
.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
要使圆保持相同大小,必须根据 g
比例因子为每个圆修改 r
属性,以对圆进行相同的调整:
To keep the circles the same size, you have to do the same adjustment for your circles by modifying the r
attribute for each circle according to the g
scale factor:
g.selectAll(".city-circle")
.transition()
.attr("r", 5 / scale )
.duration(750);
但是,由于您实际上并未在城市的圈子中应用城市圈子"类,因此在添加圈子时也需要这样做:
Though, since you don't actually apply the class city-circle on your circles you'll need to do that too when you append them:
.attr("class","city-circle")
而且,就像在重置时重置笔触宽度一样,您需要重置圆的 r
:
And, just as you reset the stroke width on reset, you need to reset the circles' r
:
g.selectAll(".city-circle")
.transition()
.attr("r", 5)
.duration(750);
这共同给了我们此.
这篇关于停止缩放D3世界地图中的气泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!