力布局中的d3动态曲线 [英] d3 dynamic curved line in force layout

查看:221
本文介绍了力布局中的d3动态曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的力布局中的线具有动态贝塞尔曲线,例如 cytoscape示例,在d3中可以吗?请参见此 d3示例,但请使用arc. 我不知道算法,有人知道吗?

I'd like to have a dynamic bezier curve for the line in a force layout like this cytoscape example, is possible in d3? see this d3 example but use arc. I have no idea of the algorithm, someone has an idea?

推荐答案

我已经看到 cytoscape 库,并且可以使用此algorytm

i've see the cytoscape library and with this algorytm works

function tick() {
     path.attr("d", function (d) {
          var coordinatesP = findEdgeControlPoints(d);
          return "M" + d.source.x + "," + d.source.y + "S" + coordinatesP.xp + "," + coordinatesP.yp + " " + d.target.x + "," + d.target.y;
     });
     hashTable = {};
          nodes.attr("cx", function (d) { return d.x; })
               .attr("cy", function (d) { return d.y; });
}

var findEdgeControlPoints = function (d) {
        var midPointX = (d.source.x + d.target.x) / 2;
        var midPointY = (d.source.y + d.target.y) / 2;

        var displacementX, displacementY;

        displacementX = d.target.y - d.source.y;
        displacementY = d.source.x - d.target.x;

        var displacementLength = Math.sqrt(displacementX * displacementX + displacementY * displacementY);

        displacementX /= displacementLength;
        displacementY /= displacementLength;

        var distanceFromMidpoint = findPathDeltaControlPoint(d);


        var xp = midPointX + displacementX * distanceFromMidpoint;
        var yp = midPointY + displacementY * distanceFromMidpoint;

        return {xp : xp, yp : yp};

    };
    var findPathDeltaControlPoint = function (edge) {
        /*caso statico il primo al centro e tutti gli altri ai lati*/
        /*conto le occorrenze */
        var pairId,
            delta,
            TICK = 20;
        pairId = edge.source.id > edge.target.id ?
                edge.target.id + '-' + edge.source.id :
                edge.source.id + '-' + edge.target.id;

        if (hashTable[pairId] == undefined) {
            hashTable[pairId] = [];
        }
        if (edge[pairId] == undefined) {
            edge[pairId] = [];
        }
        if (edge[pairId].length == 0) {
            edge[pairId].push(pairId);
        }
        hashTable[pairId].push(edge);
        // Ceck if is the first occurence
        var pairIdOccurence = hashTable[pairId].length;
        if (pairIdOccurence == 1) {
            delta = 0;
        } else if (pairIdOccurence > 1) {
            // Check if is equal
            if (pairIdOccurence % 2 == 0) {
                delta = (TICK * pairIdOccurence) / 2;
            } else {
                delta = -((TICK * (pairIdOccurence - 1)) / 2);
            }
        }
        return delta;
    };

这篇关于力布局中的d3动态曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆