0

我想在像这个cytoscape 示例这样的力布局中为线提供动态贝塞尔曲线,在 d3 中是否可行?请参阅此d3 示例,但使用 arc。我不知道算法,有人有想法吗?

4

1 回答 1

2

我已经看到了cytoscape库,并且使用这个算法可以工作

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;
    };
于 2013-07-17T10:08:12.373 回答