缩放时D3.js重定位元素

时间:2019-04-01 14:05:13

标签: d3.js svg

我有多个组元素,其中有text元素。当我用鼠标滚轮缩放时,一切都很好,我的文本仍在我的路径(多边形)之内。

但是当我自动放大时,我的文本不会重新定位。

这是我的自动缩放功能,我正在尝试通过ID查找特定路径,并用黄色,中心填充并缩放至该路径。

function findByID(ID) {
                svgContainer.selectAll("path")
                    .data(feat.features)
                    .filter(function (d) {
                        if (d.properties.myID == ID) {

                            centered = centered !== d && d;

                            var paths = svgContainer.selectAll("path")
                                .classed("active", function (d) {
                                    d === centered;
                                });

                            var t0 = projection.translate(),
                                s0 = projection.scale();

                            projection.fitSize([width, height], centered);

                            var interpolateTranslate = d3.interpolate(t0, projection.translate()),
                                interpolateScale = d3.interpolate(s0, projection.scale());

                            var interpolator = function (t) {
                                projection.scale(interpolateScale(t))
                                    .translate(interpolateTranslate(t));
                                paths.attr("d", path);
                            };

                            d3.transition()
                                .duration(5000)
                                .tween("projection", function () {

                                    return interpolator;
                                });
                            return true;
                        }
                    })
                    .attr("fill", "#e9f356");
            }

这是我使用鼠标滚轮的屏幕截图: enter image description here

这是我的自动缩放完成后的屏幕截图。我的台词也消失了,为什么会这样?

enter image description here

编辑:这是我添加文字的方式:

svgContainer.selectAll(null)
                .data(feat.features.filter(function (d) { return d.properties.myId > 0; }))
                .enter()
                .append("g").attr("id", "txt")
                .attr("transform", function (a) {
                    var centro = path.centroid(a);
                    return "translate(" + centro[0] + "," + centro[1] + ")";
                })
                .append("text")
                .attr("text-anchor", "middle")
                .attr("font-size", function (d) {
                    var bb = path.bounds(d)
                    return ((bb[1][0] - bb[0][0]) / 10) + "px";
                })
                .text("A/10/10/3");

1 个答案:

答案 0 :(得分:0)

好的,我做到了,但是当我尝试使用鼠标滚轮进行缩小时,它会立即完全缩小。如何使它平滑?

function findByID(ID) {
                svgContainer.selectAll("path")
                    .data(feat.features)
                    .filter(function (d) {
                        if (d.properties.myID == ID) {

                            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 = .9 / Math.max(dx / width, dy / height),
                                translate = [width / 2 - scale * x, height / 2 - scale * y];


                            d3.select("#mainGroup").transition()
                                .duration(5000)
                                .style("stroke-width", 1.5 / scale + "px")
                                .attr("transform", "translate(" + translate + ")scale(" + scale + ")");

                            return true;
                        }
                    })
                    .attr("fill", "#e9f356");
            }
相关问题