d3 dynamic curved line in force layout [closed]

别来无恙 提交于 2019-12-11 18:57:45

问题


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?


回答1:


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;
    };


来源:https://stackoverflow.com/questions/17550195/d3-dynamic-curved-line-in-force-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!