d3单击节点后折叠树缩放和平移 - 跳跃

时间:2013-12-12 12:08:18

标签: d3.js tree zoom pan

如果我点击一个节点后缩放/平移,我的缩放和缩放会跳回到原始位置。

这是我的缩放功能:

zoom: function() {
var scale = d3.event.scale,
    translation = d3.event.translate,
    tbound = -h * scale,
    bbound = h * scale,
    lbound = (-w + m[1]) * scale,
    rbound = (w - m[3]) * scale;
// limit translation to thresholds
translation = [
    Math.max(Math.min(translation[0], rbound), lbound),
    Math.max(Math.min(translation[1], bbound), tbound)
];
d3.select(".drawarea")
    .attr("transform", "translate(" + translation + ")" +
          " scale(" + scale + ")");
}

我知道我需要存储缩放值并重复使用它们,但我不知道如何。

我目前没有代码的工作方式,但我发现this one有同样的问题。

这是我的结构:

            postCreate: function () {...},

//--------------------------------------------------------------

            getTreeStructureFromMapModel: function () {...},

            _getDiagonal: function () {...},

            getDrawingArea: function (w, h) {...},

//--------------------------------------------------------------

            update: function (source) {...},

            updateTheNodes: function (source, root) {...},

            enterNewNodesAtParentsOldPosition: function (source, root, node) {...},

            transitionNodesToTheirNewPosition: function (node) {...},

            transitionExistingNodesToParentsNewPosition: function (node, source) {...},

//--------------------------------------------------------------

            updateTheLinks: function (source) {...},

            enterNewLinksAtParentsOldPosition: function (link, source) {...},

            transitionLinksToTheirNewPosition: function (link) {...},

            transitionExistingLinksToParentsNewPosition: function (link, source) {...},

//--------------------------------------------------------------

            onNodeClick: function () {...},

            toggleAll: function (d) {...},

            toggle: function (d) {...},

            zoom: function() {...}

我在postCreate()内添加了缩放功能的代码。

这是我的postCreate:

postCreate: function () {
    this.inherited(arguments);

    var d3 = this.d3;
    this._i = 0;

    var margins = this.margins;
    var w = this._w = 900 - margins.left - margins.right;
    var h = this._h = 900 - margins.top - margins.bottom;

    this._tree = d3.layout.tree()
        .size([h, w]);

    his._drawingArea = this.getDrawingArea(w, h);

    d3.select("svg")
        .call(d3.behavior.zoom()
        .scaleExtent([0.5, 5])
        .on("zoom", this.zoom));

    var root = this._root = this.getTreeStructureFromMapModel();
    root.x0 = h / 2;
    root.y0 = 0;

    this.toggleAll(root);
    this.update(root);
},

vis在getDrawingArea()

中创建
getDrawingArea: function (w, h) {
    var margins = this.margins;

    var vis = this.d3.select(this.domNode).append("svg:svg")
        .attr("width", w + margins.left + margins.right)
        .attr("height", h + margins.top + margins.bottom)
        .append("svg:g")
        .attr("class", "drawarea")
        .append("svg:g")
        .attr("transform", "translate(" + margins.left + "," + margins.top + ")")
    return vis;

 },

1 个答案:

答案 0 :(得分:2)

将附加缩放行为的代码移动到update函数之外的SVG。工作示例here