日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

javascript – d3.js:使用帶有強(qiáng)制布局的縮放時(shí)禁用拖動(dòng)

 印度阿三17 2019-07-02

我見(jiàn)過(guò)這個(gè)問(wèn)題:Is there a way to zoom into a D3 force layout graph?

但是我從我的圖形中得到了一些意想不到的行為 – 在幾次拖動(dòng)或縮放或平移后所有節(jié)點(diǎn)都凍結(jié)并拖動(dòng)停止工作.

我創(chuàng)造了這個(gè)小提琴:http:///7gpweae9/9/

所以要求代碼,所以這里是主要部分:

var svg = d3.select("#graph")
    .append("svg:svg")
        .attr("width", width)
        .attr("height", height)
        .attr("pointer-event", "all")
    .append("svg:g")
        .call(d3.behavior.zoom().on("zoom", zoom))
    .append("svg:g");

svg.append("svg:rect")
    .attr("width", width)
    .attr("height", height)
    .attr('fill', 'white');

var link = svg.selectAll(".link");
var node = svg.selectAll(".node");

var force = d3.layout.force()
    .nodes(nodes)
    .links(links)
    .size([width,height])
    .linkDistance(100)
    .charge(-400)
    .start();

var drag = d3.behavior.drag()
    .origin(function(d) { return d; })
    .on("dragstart", dragstarted)
    .on("drag", dragged)
    .on("dragend", dragended);

node = svg.selectAll(".node")
    .data(nodes)
    .enter().append("g")
    .attr("class", "node")
    .call(drag);

node.append("circle")
    .attr("class", "node-circle")
    .attr("r", 12);

node.append("text")
    .attr("x", 12)
    .attr("y", ".35em")
    .text(function(d) { return d.word; });

link = svg.selectAll(".link")
    .data(links)
    .enter().append("line")
    .attr("class", "link");

force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("transform", function(d) {
        return "translate("   d.x   ","   d.y   ")";
    });
});

function zoom() {
    svg.attr("transform",
        "translate("   d3.event.translate   ")scale("   d3.event.scale   ")");
}

function dragstarted(d) {
    d3.event.sourceEvent.stopPropagation();
    d3.select(this).classed("dragging", true);
}

function dragged(d) {
    d3.select(this).attr("x", d.x = d3.event.x).attr("y", d.y = d3.event.y);
}

function dragended(d) {
    d3.select(this).classed("dragging", false);
}

也許我錯(cuò)過(guò)了一些東西,我之前從未使用過(guò)d3.

UPD:似乎在一段時(shí)間后發(fā)生凍結(jié).

解決方法:

我將d3.layout.force()替換為force.drag(),現(xiàn)在它幾乎可以正常工作.

    var nodes;
    var links;
    prepareData();

    var graph = document.querySelectorAll("#graph")[0];
    var height = 500;
    var width = 500;

    var svg = d3.select("#graph").append("svg:svg")
            .attr("width", width)
            .attr("height", height)
            .attr("pointer-event", "all")
        .append("svg:g")
            .call(d3.behavior.zoom().on("zoom", zoom))
        .append("svg:g");

    svg.append("svg:rect")
        .attr("width", width)
        .attr("height", height)
        .attr('fill', 'white');



    var link = svg.selectAll(".link");
    var node = svg.selectAll(".node");

    var force = d3.layout.force()
        .nodes(nodes)
        .links(links)
        .size([width,height])
        .linkDistance(100)
        .charge(-400)
        .start();

    var drag = force.drag()
        .origin(function(d) { return d; })
        .on("dragstart", dragstarted)
        .on("drag", dragged)

    node = svg.selectAll(".node")
        .data(nodes)
        .enter().append("g")
        .attr("class", "node")
        .call(drag);

    node.append("circle")
        .attr("class", "node-circle")
        .attr("r", 12);

    node.append("text")
        .attr("x", 12)
        .attr("y", ".35em")
        .text(function(d) { return d.word; });

    link = svg.selectAll(".link")
        .data(links)
        .enter().append("line")
        .attr("class", "link");

    force.on("tick", function() {
        link.attr("x1", function(d) { return d.source.x; })
            .attr("y1", function(d) { return d.source.y; })
            .attr("x2", function(d) { return d.target.x; })
            .attr("y2", function(d) { return d.target.y; });

        node.attr("transform", function(d) { return "translate("   d.x   ","   d.y   ")"; });
    });

    function zoom() {
        svg.attr("transform", "translate("   d3.event.translate   ")scale("   d3.event.scale   ")");
    }

    function dragstarted(d) {
        d3.event.sourceEvent.stopPropagation();
    }

    function dragged(d) {
        d3.select(this).attr("x", d.x = d3.event.x).attr("y", d.y = d3.event.y);
    }

    function prepareData() {
        nodes = [{"index":0,"word":"edit"},{"index":1,"word":"course","sentences":[29859]},{"index":2,"word":"needs","sentences":[29859]},{"index":3,"word":"fit","sentences":[29859]},{"index":4,"word":"slides","sentences":[29859]},{"index":5,"word":"print","sentences":[29859]},{"index":6,"word":"can","sentences":[29859]}];
                links = [{"source":0,"target":1},{"source":0,"target":2},{"source":0,"target":3},{"source":0,"target":4},{"source":0,"target":5},{"source":0,"target":6}]
    }
svg {
    border: 1px solid black;
}

.link {
    stroke: #000;
    stroke-width: 1px;
}

.node-circle {
    cursor: move;
    fill: #ccc;
    stroke: #000;
    stroke-width: 2px;
}
<script src="https://cdnjs./ajax/libs/d3/3.4.11/d3.min.js"></script>
<body>
    <div id="graph"></div>
</body>
來(lái)源:https://www./content-1-292751.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多