主頁 > 資料庫 > D3.js突出顯示相關節點/鏈接

D3.js突出顯示相關節點/鏈接

2021-10-18 10:57:05 資料庫

問題:我希望根據鏈接型別來淡化/突出顯示整個依賴鏈。

為了做到這一點,我利用了滑鼠進入事件,該事件目前存盤了所有鏈接和節點。此外,我還將淡化所有節點和鏈接,只突出顯示那些被過濾為相關節點和鏈接的節點。如果這些節點和鏈接也有need型別的連接,就需要再次檢查所有相關節點和鏈接。只要發現依賴性連接,就必須這樣做。我想不出一個合適的演算法。

例子:

為了更好地理解,我創建了一個啤酒成分的依賴關系,它看起來就像一個星星。為了這些目的,我的版本很好。BUT第二條鏈,關于汽車->輪子->輪胎->橡膠和收音機,讓我感到頭痛。無線電是一個"使用"的依賴關系,意味著它對這個鏈來說不是強制性的,不應該被強調。

預期的結果:

如果游標在car上,所有具有 "need "依賴關系的連接節點都應該被高亮顯示,其余的應該淡化。

對于那些想幫助我的人,請不要猶豫,如果有什么不清楚的地方,請提問。

<!DOCTYPE html>
<html> 

<head> 
    <meta charset="utf-8">/span>
    < meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- d3.js框架-->
    <script src="https://d3js. org/d3.v6.js"></script>
    <!-- fontawesome stylesheet https://fontawesome.com/ -->>
    <script src="https://kit.fontawesome.com/39094309d6. js" crossorigin="anonymous"></script> 
</head> 

<style>
    body {
        height: 100%;
        background: #e6e7ee;
        overflow: hidden;
        margin: 0px;
    }

    .faded {
        opacity: 0.1;
        transition: 0.3s不透明度。
    }

    .highlight {
        opacity: 1;
    
</style>>

<body> 
    <svg id="svg"/span>> </svg>

    <script>
        var graph = {
            "nodes": [
                {
                    "id": 0,
                    "name": "啤酒"。
                },
                {
                    "id": 1,
                    "name": "water",
                },
                {
                    "id": 2,
                    "name": "hop"。
                },
                {
                    "id": 3,
                    "name": "malt",
                },
                {
                    "id": 4,
                    "name": "yeast",
                },
                {
                    "id": 10,
                    "name": "car",
                },
                {
                    "id": 11,
                    "name": "wheel",
                },
                {
                    "id": 12,
                    "name": "tires",
                },
                {
                    "id": 13,
                    "name": "橡膠"。
                },
                {
                    "id": 14,
                    "name": "radio"。
                }
            ],
            "鏈接": [
                {
                    "source": 0,
                    "目標": 1,
                    "type": "需要"。
                },
                {
                    "來源": 0,
                    "目標": 2,
                    "type": "需要"。
                },
                {
                    "來源": 0,
                    "目標": 3,
                    "type": "需要"。
                },
                {
                    "來源": 0,
                    "目標": 4,
                    "type": "需要"。
                },
                {
                    "來源": 10,
                    "目標": 11,
                    "type": "need"。
                },
                {
                    "來源": 11,
                    "目標": 12,
                    "type": "需要"。
                },
                {
                    "來源": 12,
                    "目標": 13,
                    "型別": "需要"。
                },
                {
                    "來源": 10,
                    "目標": 14,
                    "型別": "use", "type": "use"
                }

            ]
        }

        var svg = d3.select("svg"/span>)
            .attr("class"/span>, "canvas")
            .attr("width"/span>, window.innerWidth)
            .attr("height"window.innerHeight)
            .call(d3.zoom()。 on("zoom", function (event) {
                svg.attr("transform", event.transform)
            }))
            .append("g")

        //append markers to svg.
        svg.append("defs").append("marker")
            .attr("id", "箭頭")
            .attr("viewBox", "-0 -5 10 10")
            .attr("refX"/span>, 8)
            .attr("refY", 0)
            .attr("orient", "auto")
            .attr("markerWidth"/span>, 50)
            .attr("markerHeight"/span>, 50)
            .attr("xoverflow", " visible")
            .append("svg:path")
            .attr("d", "M 0,-1 L 2 ,0 L 0,1" )
            .attr("fill"/span>, "black")
            .style("stroke", "none")

        var linksContainer = svg.append("g"/span>).attr("class"/span>, linksContainer)
        var nodesContainer = svg.append("g") 。 attr("class"/span>, nodesContainer)

        var force = d3.forceSimulation()
            .force("link", d3.forceLink() 。 id(function (d) {
                returnd.id
            }).distance(80)
            .force("charge", d3.forceManyBody().strength(-100)
            .force("center", d3.forceCenter(window. innerWidth / 2, window.innerHeight / 2)
            .force("collision", d3.forceCollide().radius(90)

        initialize()

        function initialize) {

            link = linksContainer.selectAll(".link")
                .data(graph.links)
                .join("line"/span>)
                .attr("class"/span>, "link")
                .attr('marker-end', 'url(#arrowhead)')
                .style("display" , "block" )
                .style("stroke", "black")
                .style("stroke-width"1)

            linkPaths = linksContainer.selectAll(".linkPath")
                .data(graph.links)
                .join("path"/span>)
                .style("pointer-events", "none")
                .attr("class"/span>, "linkPath")
                .attr("fill-opacity"/span>, 1)
                .attr("stroke-opacity", 1)
                . attr("id", function(d, i) { return "linkPath"/span>   i })
                .style("display"/span>, "block"/span>)

            linkLabels = linksContainer.selectAll(".linkLabel")
                .data(graph.links)
                .join("text"/span>)
                .style("pointer-events"/span>, "none")
                .attr("class"/span>, "linkLabel")
                . attr("id", function(d, i) { return "linkLabel"/span>   i })
                .attr("font-size"16)
                .attr("fill", "black")
                .text("")

            鏈接標簽
                .append("textPath"/span>)
                . attr('xlink: href', function (d, i) { return '#linkPath'   i })
                .style("text-anchor"/span>, "middle")
                .style("pointer-events"/span>, "none")
                .attr("startOffset"/span>, "50%")
                .text(function (d) { return d.type })

            node = nodesContainer.selectAll(".node"/span>)
                .data(graph.nodes, d =>  d.id)
                .join("g"/span>)
                .attr("class"/span>, "node")
                .call(d3.drag()
                    .on("start"/span>, dragStarted)
                    .on("drag", dragged)
                    .on("end", dragEnded)
                )

            node.selectAll("circle")
                .data(d => [d])
                .join("circle"/span>)
                .attr("r"/span>, 30)
                .style("fill", "whitesmoke")
                .on("mouseenter"/span>, mouseEnter)
                .on("mouseleave"/span>, mouseLeave)

            node.selectAll("text")
                .data(d => [d])
                .join("text"/span>)
                .style("class"/span>, "icon")
                .attr("font-family"/span>, "FontAwesome")
                .attr("dominant-baseline"/span>, "central")
                .attr("text-anchor", "middle")
                .attr("font-size", 20)
                .attr("fill", "black")
                .attr("pointer-events"/span>, "none")
                .attr("dy"/span>, "-1em")
                .text(function (d) {
                    return d.name 
                })
            node.append("text")
                .attr("dominant-baseline"/span>, "central")
                .attr("text-anchor", "middle")
                .attr("font-size", 13)
                .attr("fill", "black")
                .attr("pointer-events"/span>, "none")
                .attr("dy"/span>, "0.5em"/span>)
                .text(function (d) {
                    returnd.id
                })

            力量
                .nodes(graph.nodes)
                .on("tick", ticked)。

            迫使
                .force("link")
                .links(graph.links)
        }

        function mouseEnter(event, d) {
            const selNodes = node.selectAll("circle")
            const selLink = link
            const selLinkLabel = linkLabels
            const selText = node.selectAll("text")
            const related = []
            const relatedLinks = [] 。

            related.push(d)
            force.force('link').links() 。 forEach((link) =>  {
                if (link.source == d || link.target == d) {
                    relatedLinks.push(link)
                    if (related.indexOf(link.source) ==-1) { related.push(link.source) }
                    if (related.indexOf(link.target) ==-1) { related.push(link.target) }
                }
            })
            selNodes.classed('faded', true)
            selNodes.filter((dNodes) => 相關。 indexOf(dNodes) > -1)
                .classed('highlight', true)
            selLink.classed('faded', true)
            selLink.filter((dLink) => dLink。 source ==d || dLink.target ==d)
                .classed('highlight', true)
            selLinkLabel.classed('faded', true)
            selLinkLabel.filter((dLinkLabel) => dLinkLabel。 source ==d || dLinkLabel.target ==d)
                .classed('highlight', true)
            selText.classed('faded', true)
            selText.filter((dText) => 相關。 indexOf(dText) > -1)
                .classed('highlight', true)
            
            force.alphaTarget(0.0001).restart()
        }

        function mouseLeave(event, d) {
            const selNodes = node.selectAll("circle")
            const selLink = link
            const selLinkLabel = linkLabels
            const selText = node.selectAll("text")

            selNodes.classed('faded', false)
            selNodes.classed('highlight', false)
            selLink.classed('faded', false)
            selLink.classed('highlight', false)
            selLinkLabel.classed('faded', false)
            selLinkLabel.classed('highlight', false)
            selText.classed('faded', false)
            selText.classed('highlight', false)
            
            force.restart()
        }

        function ticked) {
            //>更新鏈接位置。
            鏈接
                .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;
                });

            //更新節點位置 //更新節點位置。
            節點
                .attr("transform", function(d) {
                    return "translate("   d. x   ", "   d.y   ")"/span>。
                });

            linkPaths.attr('d', function (d) {
                return 'M '   d.source. x   ' '   d.source. y   ' L '/span>   d.target. x   ' '/span>   d.target.y。
            });

            linkLabels.attr('transform', function (d) {
                if (d.target.x < d.source.x) {
                    var bbox = this.getBBox()。

                    rx = bbox.x   bbox.width / 2。
                    ry = bbox.y   bbox.height / 2;
                    return 'rotate(180 '   rx   ' '   ry   ') '。
                }
                else {
                    return 'rotate(0)'。
                }
            });

        }

        function dragStartedevent, d) {
            if (!event.active) force.alphaTarget0.3).restart()。
            d.fx = d.x;
            d.fy = d.y;

            PosX = d.x; x
            PosY = d.y
        }

        function dragged(event, d) {
            d.fx = event.x;
            d.fy = event.y。
        }

        function dragEnded(event, d) {
            if (!event.active) force.alphaTarget0)。
            d.fx = undefined;
            d.fy = undefined;
        

    </script>>
</body> 

</html>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>

uj5u.com熱心網友回復:

有幾個問題你可以看看,以獲得淡化/高亮運行:

首先,注意到force方法改變了graph中的links陣列。

例如,你的第一個鏈接是這樣開始的:

{
  "source"0,
  "目標": 1,
  "type": "需要", "型別": "需要".
}

但變成這樣:

{
  "index"0.
  "source": {
    "id": 0
    "index": 0
    "name": "啤酒": "啤酒""vx": 0.036971029563580046
    "vy": 0.04369386654517388
    "x": 394.1514674087123
    "y": 220.18458726626062
  },
  "target": {
    "id": 1: "id".
    "index": 1
    "name": "water": "water".
    "vx": -0.021212609689083086
    "vy": 0.01105162589441528
    "x": 568.911363724937
    "y": 177.07991527420614
  },
  "type": "需要"

所以你需要一個遞回函式,但是如果你參考link.source,你會得到空陣列--相反,你需要參考link.source.id,因為這就是force根據上述例子更新你的圖物件的方式。

下面是一個相當冗長的遞回函式,它可以回傳給定節點ID的所有節點和鏈接,這些節點和鏈接是由給定的型別的鏈接連接起來的:

function nodesByTypeAfterForcenodeId, sieved, type) {

  //獲得節點的鏈接,根據型別const newLinks = graph.links.
    .filter(link => link。 type == type && link.source.id == nodeId)。)

  //從鏈接中獲得與nodeId相連的節點const newNodes = newLinks
    .map(link => graph.nodesfind(span class="hljs-params">newNode => newNode。 id === link.target.id)。)

  //連接新節點和鏈接。
  (sieved.links = sieved.links || []).push(...newLinks) 。
  (sieved.nodes = sieved.nodes || []).push(...newNodes) 。

  //遞回訪問鏈接節點,直到用盡選項。
  newNodes.forEach(node => nodesByTypeAfterForce(node. id, sieved, type))。)

  //回傳相關節點和鏈接的索引return {
    nodes: sieved.nodes. map(span class="hljs-params">node => node.index) 。
    links: sieved.links. map(span class="hljs-params">link => link.index)
  };
}

注意該函式回傳一個index的陣列,這是force分配給每個節點和鏈接的屬性。這使得以后對淡出/高亮的過濾更加明確。


現在,在mouseEnter中,你可以先呼叫這個函式,只回傳由某個型別的鏈接連接起來的節點,并通過d來初始化搜索:

function mouseEnterevent, d) {

  //被懸停的節點的子圖const sieved = nodesByTypeAfterForce(d.id, {nodes: [d]}, "need")。)
  //...。
}

這取代了related陣列的構造,在你的OP中,它不是與type相關的。雖然這個問題很容易解決,但你目前的mouseEnter中的另一個問題是你有這些行。

selLink
  .filter((dLink) => dLink.source ==d || dLink.target ==d)

并且

selLinkLabel
  .filter((dLinkLabel) => dLinkLabel。 source ==d || dLinkLabel.target ==d)
  .classed('highlight', true)

這導致鏈接(和鏈接標簽)的高亮顯示到任何鏈接的節點,而不僅僅是由型別(如need)鏈接的節點。

所以,我建議你用這個代碼塊來代替(我移動了所有的行,把所有的東西都淡化到他們自己的部分):

// Only highlight from sieved node.selectAll("clar") .filter(node => sieved.nodesindexOf(node.index) > -1) .classed('highlight', true) 鏈接 .filter(span class="hljs-params">link => sieved.linksindexOf(link.index) > -1) .classed('highlight', true) 鏈接標簽 .filter(span class="hljs-params">link => sieved.linksindexOf(link.index) > -1) .classed('highlight', true) node.selectAll("text") .filter(node => sieved.nodesindexOf(node.index) > -1) .classed('highlight', true)

現在只根據上面nodeByTypeAfterForce函式回傳的index來突出顯示節點和鏈接。


作業實體如下,其中nodeByTypeAfterForce只是在graph的定義之后被丟進去,唯一的其他編輯是在mouseEnter

<!DOCTYPE html>
<html> 

<head> 
    <meta charset="utf-8">/span>
    < meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- d3.js框架-->
    <script src="https://d3js. org/d3.v6.js"></script>
    <!-- fontawesome stylesheet https://fontawesome.com/ -->>
    <script src="https://kit.fontawesome.com/39094309d6. js" crossorigin="anonymous"></script> 
</head> 

<style>
    body {
        height: 100%;
        background: #e6e7ee;
        overflow: hidden;
        margin: 0px;
    }

    .faded {
        opacity: 0.1;
        transition: 0.3s不透明度。
    }

    .highlight {
        opacity: 1;
    
</style>>

<body> 
    <svg id="svg"/span>> </svg>

    <script>
        var graph = {
            "nodes": [
                {
                    "id": 0,
                    "name": "啤酒"。
                },
                {
                    "id": 1,
                    "name": "water",
                },
                {
                    "id": 2,
                    "name": "hop"。
                },
                {
                    "id": 3,
                    "name": "malt",
                },
                {
                    "id": 4,
                    "name": "yeast",
                },
                {
                    "id": 10,
                    "name": "car",
                },
                {
                    "id": 11,
                    "name": "wheel",
                },
                {
                    "id": 12,
                    "name": "tires",
                },
                {
                    "id": 13,
                    "name": "橡膠"。
                },
                {
                    "id": 14,
                    "name": "radio"。
                }
            ],
            "鏈接": [
                {
                    "source": 0,
                    "目標": 1,
                    "type": "需要"。
                },
                {
                    "來源": 0,
                    "目標": 2,
                    "type": "需要"。
                },
                {
                    "來源": 0,
                    "目標": 3,
                    "type": "需要"。
                },
                {
                    "來源": 0,
                    "目標": 4,
                    "type": "需要"。
                },
                {
                    "來源": 10,
                    "目標": 11,
                    "type": "need"。
                },
                {
                    "來源": 11,
                    "目標": 12,
                    "type": "需要"。
                },
                {
                    "來源": 12,
                    "目標": 13,
                    "型別": "需要"。
                },
                {
                    "來源": 10,
                    "目標": 14,
                    "型別": "use", "type": "use"
                }

            ]
        }


        function nodesByTypeAfterForce(nodeId、sieved、type) {

          //獲得節點的鏈接,根據型別const newLinks = graph.links.
            .filter(link => link。 type == type && link.source.id == nodeId)。)

          //從鏈接中獲得與nodeId相連的節點const newNodes = newLinks
            .map(link => graph.nodesfind(span class="hljs-params">newNode => newNode。 id === link.target.id)。)

          //連接新節點和鏈接。
          (sieved.links = sieved.links || []).push(...newLinks) 。
          (sieved.nodes = sieved.nodes || []).push(...newNodes) 。

          //遞回訪問鏈接節點,直到用盡選項。
          newNodes.forEach(node => nodesByTypeAfterForce(node. id, sieved, type))。)

          //回傳相關節點和鏈接的索引return {
            nodes: sieved.nodes. map(span class="hljs-params">node => node.index) 。
            links: sieved.links. map(span class="hljs-params">link => link.index)
          };
        }

        var svg = d3.select("svg"/span>)
            .attr("class"/span>, "canvas")
            .attr("width"/span>, window.innerWidth)
            .attr("height"window.innerHeight)
            .call(d3.zoom()。 on("zoom", function (event) {
                svg.attr("transform", event.transform)
            }))
            .append("g")

        //append markers to svg.
        svg.append("defs").append("marker")
            .attr("id", "箭頭")
            .attr("viewBox", "-0 -5 10 10")
            .attr("refX"/span>, 8)
            .attr("refY", 0)
            .attr("orient", "auto")
            .attr("markerWidth"/span>, 50)
            .attr("markerHeight"/span>, 50)
            .attr("xoverflow", " visible")
            .append("svg:path")
            .attr("d", "M 0,-1 L 2 ,0 L 0,1" )
            .attr("fill"/span>, "black")
            .style("stroke", "none")

        var linksContainer = svg.append("g"/span>).attr("class"/span>, linksContainer)
        var nodesContainer = svg.append("g") 。 attr("class"/span>, nodesContainer)

        var force = d3.forceSimulation()
            .force("link", d3.forceLink() 。 id(function (d) {
                returnd.id
            }).distance(80)
            .force("charge", d3.forceManyBody().strength(-100)
            .force("center", d3.forceCenter(window. innerWidth / 2, window.innerHeight / 2)
            .force("collision", d3.forceCollide().radius(90)

        initialize()

        function initialize) {

            link = linksContainer.selectAll(".link")
                .data(graph.links)
                .join("line"/span>)
                .attr("class"/span>, "link")
                .attr('marker-end', 'url(#arrowhead)')
                .style("display" , "block" )
                .style("stroke", "black")
                .style("stroke-width"1)

            linkPaths = linksContainer.selectAll(".linkPath")
                .data(graph.links)
                .join("path"/span>)
                .style("pointer-events", "none")
                .attr("class"/span>, "linkPath")
                .attr("fill-opacity"/span>, 1)
                .attr("stroke-opacity", 1)
                . attr("id", function(d, i) { return "linkPath"/span>   i })
                .style("display"/span>, "block"/span>)

            linkLabels = linksContainer.selectAll(".linkLabel")
                .data(graph.links)
                .join("text"/span>)
                .style("pointer-events"/span>, "none")
                .attr("class"/span>, "linkLabel")
                . attr("id", function(d, i) { return "linkLabel"/span>   i })
                .attr("font-size"16)
                .attr("fill", "black")
                .text("")

            鏈接標簽
                .append("textPath"/span>)
                . attr('xlink: href', function (d, i) { return '#linkPath'   i })
                .style("text-anchor"/span>, "middle")
                .style("pointer-events"/span>, "none")
                .attr("startOffset"/span>, "50%")
                .text(function (d) { return d.type })

            node = nodesContainer.selectAll(".node"/span>)
                .data(graph.nodes, d =>  d.id)
                .join("g"/span>)
                .attr("class"/span>, "node")
                .call(d3.drag()
                    .on("start"/span>, dragStarted)
                    .on("drag", dragged)
                    .on("end", dragEnded)
                )

            node.selectAll("circle")
                .data(d => [d])
                .join("circle"/span>)
                .attr("r"/span>, 30)
                .style("fill", "whitesmoke")
                .on("mouseenter"/span>, mouseEnter)
                .on("mouseleave"/span>, mouseLeave)

            node.selectAll("text")
                .data(d => [d])
                .join("text"/span>)
                .style("class"/span>, "icon")
                .attr("font-family"/span>, "FontAwesome")
                .attr("dominant-baseline"/span>, "central")
                .attr("text-anchor", "middle")
                .attr("font-size", 20)
                .attr("fill", "black")
                .attr("pointer-events"/span>, "none")
                .attr("dy"/span>, "-1em")
                .text(function (d) {
                    return d.name 
                })
            node.append("text")
                .attr("dominant-baseline"/span>, "central")
                .attr("text-anchor", "middle")
                .attr("font-size", 13)
                .attr("fill", "black")
                .attr("pointer-events"/span>, "none")
                .attr("dy"/span>, "0.5em"/span>)
                .text(function (d) {
                    returnd.id
                })

            力量
                .nodes(graph.nodes)
                .on("tick", ticked)。

            迫使
                .force("link")
                .links(graph.links)
        }

        function mouseEnter(event, d) {

            //被懸停的節點的子圖const sieved = nodesByTypeAfterForce(d.id, {nodes: [d]}, "need")。)
            
            //淡化一切。
            node.selectAll("circle").classed('faded'true)
            node.selectAll("circle").classed('highlight'false)
            link.classed('faded', true)
            link.classed('highlight', false)
            linkLabels.classed('faded', true)
            linkLabels.classed('highlight', false)
            node.selectAll("text").classed('faded'true)
            node.selectAll("text").classed('highlight', false)
            
            //僅從篩分中突出顯示。
            node.selectAll("circle")
              .filter(span class="hljs-params">node => sieved.nodesindexOf(node.index) > -1)
              .classed('highlight', true)
            鏈接
              .filter(span class="hljs-params">link => sieved.linksindexOf(link.index) > -1)
              .classed('highlight', true)
            鏈接標簽
              .filter(span class="hljs-params">link => sieved.linksindexOf(link.index) > -1)
              .classed('highlight', true)
            node.selectAll("text")
              .filter(node => sieved.nodesindexOf(node.index) > -1)
              .classed('highlight', true)
            
            force.alphaTarget(0.0001).restart()
        }

        function mouseLeave(event, d) {
            const selNodes = node.selectAll("circle")
            const selLink = link
            const selLinkLabel = linkLabels
            const selText = node.selectAll("text")

            selNodes.classed('faded', false)
            selNodes.classed('highlight', false)
            selLink.classed('faded', false)
            selLink.classed('highlight', false)
            selLinkLabel.classed('faded', false)
            selLinkLabel.classed('highlight', false)
            selText.classed('faded', false)
            selText.classed('highlight', false)
            
            force.restart()
        }

        function ticked) {
            //>更新鏈接位置。
            鏈接
                .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;
                });

            //更新節點位置 //更新節點位置。
            節點
                .attr("transform", function(d) {
                    return "translate("   d. x   ", "   d.y   ")"/span>。
                });

            linkPaths.attr('d', function (d) {
                return 'M '   d.source. x   ' '   d.source. y   ' L '/span>   d.target. x   ' '/span>   d.target.y。
            });

            linkLabels.attr('transform', function (d) {
                if (d.target.x < d.source.x) {
                    var bbox = this.getBBox()。

                    rx = bbox.x   bbox.width / 2。
                    ry = bbox.y   bbox.height / 2;
                    return 'rotate(180 '   rx   ' '   ry   ') '。
                }
                else {
                    return 'rotate(0)'。
                }
            });

        }

        function dragStartedevent, d) {
            if (!event.active) force.alphaTarget0.3).restart()。
            d.fx = d.x;
            d.fy = d.y;

            PosX = d.x; x
            PosY = d.y
        }

        function dragged(event, d) {
            d.fx = event.x;
            d.fy = event.y。
        }

        function dragEnded(event, d) {
            if (!event.active) force.alphaTarget0)。
            d.fx = undefined;
            d.fy = undefined;
        

    </script>>
</body> 

</html>/span>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>

如果你像這樣定義一個鏈接,例如用 "Yeast "來定義,你會遇到堆疊溢位:

"link": [ // Yeast need yeast ? { "來源": 4, "目標": 4, "type": "需要"。 }, ... ]

所以在nodesByTypeAfterForce中需要一些額外的邏輯,以適應自我參考的鏈接。

uj5u.com熱心網友回復:

使用遞回(getNeedChain呼叫自己直到完成):

const getNeedChain = id => {
  const links = graph.links.filter(l =>  l。 source === id && l.type === "need")。)
  const nodes = links.map(l => graph.nodes. find(span class="hljs-params">n => n.id == l.target)) 。
  const linked = nodes.reduce((c, n) =>  [. ...c, ...getNeedChain(n.id)], [])。)
  return [...nodes, ... linked];
}

console. log('Beer needs: ', getNeedChain(0)); // Returns 4 nodes.
consolelog('Car needs: ', getNeedChain(10)); //回傳 3 個節點

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/320663.html

標籤:

上一篇:在d3.jsv6 中標記元素為已點擊

下一篇:如何在X軸上繪制日期,同時跳過周末d3.js?

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • GPU虛擬機創建時間深度優化

    **?桔妹導讀:**GPU虛擬機實體創建速度慢是公有云面臨的普遍問題,由于通常情況下創建虛擬機屬于低頻操作而未引起業界的重視,實際生產中還是存在對GPU實體創建時間有苛刻要求的業務場景。本文將介紹滴滴云在解決該問題時的思路、方法、并展示最終的優化成果。 從公有云服務商那里購買過虛擬主機的資深用戶,一 ......

    uj5u.com 2020-09-10 06:09:13 more
  • 可編程網卡芯片在滴滴云網路的應用實踐

    **?桔妹導讀:**隨著云規模不斷擴大以及業務層面對延遲、帶寬的要求越來越高,采用DPDK 加速網路報文處理的方式在橫向縱向擴展都出現了局限性。可編程芯片成為業界熱點。本文主要講述了可編程網卡芯片在滴滴云網路中的應用實踐,遇到的問題、帶來的收益以及開源社區貢獻。 #1. 資料中心面臨的問題 隨著滴滴 ......

    uj5u.com 2020-09-10 06:10:21 more
  • 滴滴資料通道服務演進之路

    **?桔妹導讀:**滴滴資料通道引擎承載著全公司的資料同步,為下游實時和離線場景提供了必不可少的源資料。隨著任務量的不斷增加,資料通道的整體架構也隨之發生改變。本文介紹了滴滴資料通道的發展歷程,遇到的問題以及今后的規劃。 #1. 背景 資料,對于任何一家互聯網公司來說都是非常重要的資產,公司的大資料 ......

    uj5u.com 2020-09-10 06:11:05 more
  • 滴滴AI Labs斬獲國際機器翻譯大賽中譯英方向世界第三

    **桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......

    uj5u.com 2020-09-10 06:11:29 more
  • MPP (Massively Parallel Processing)大規模并行處理

    1、什么是mpp? MPP (Massively Parallel Processing),即大規模并行處理,在資料庫非共享集群中,每個節點都有獨立的磁盤存盤系統和記憶體系統,業務資料根據資料庫模型和應用特點劃分到各個節點上,每臺資料節點通過專用網路或者商業通用網路互相連接,彼此協同計算,作為整體提供 ......

    uj5u.com 2020-09-10 06:11:41 more
  • 滴滴資料倉庫指標體系建設實踐

    **桔妹導讀:**指標體系是什么?如何使用OSM模型和AARRR模型搭建指標體系?如何統一流程、規范化、工具化管理指標體系?本文會對建設的方法論結合滴滴資料指標體系建設實踐進行解答分析。 #1. 什么是指標體系 ##1.1 指標體系定義 指標體系是將零散單點的具有相互聯系的指標,系統化的組織起來,通 ......

    uj5u.com 2020-09-10 06:12:52 more
  • 單表千萬行資料庫 LIKE 搜索優化手記

    我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......

    uj5u.com 2020-09-10 06:13:25 more
  • 滴滴Ceph分布式存盤系統優化之鎖優化

    **桔妹導讀:**Ceph是國際知名的開源分布式存盤系統,在工業界和學術界都有著重要的影響。Ceph的架構和演算法設計發表在國際系統領域頂級會議OSDI、SOSP、SC等上。Ceph社區得到Red Hat、SUSE、Intel等大公司的大力支持。Ceph是國際云計算領域應用最廣泛的開源分布式存盤系統, ......

    uj5u.com 2020-09-10 06:14:51 more
  • es~通過ElasticsearchTemplate進行聚合~嵌套聚合

    之前寫過《es~通過ElasticsearchTemplate進行聚合操作》的文章,這一次主要寫一個嵌套的聚合,例如先對sex集合,再對desc聚合,最后再對age求和,共三層嵌套。 Aggregations的部分特性類似于SQL語言中的group by,avg,sum等函式,Aggregation ......

    uj5u.com 2020-09-10 06:14:59 more
  • 爬蟲日志監控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替換IP與用戶 導讀: 現ELK四大組件分別為:Elasticsearch(核心)、logstash(處理)、filebeat(采集)、kibana(可視化) 下載均在https://www.elastic.co/cn/downloads/下tar包,各組件版本最好一致,配合fdm會 ......

    uj5u.com 2020-09-10 06:15:05 more
最新发布
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:33:24 more
  • MySQL中binlog備份腳本分享

    關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......

    uj5u.com 2023-04-20 08:28:06 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:27:27 more
  • 快取與資料庫雙寫一致性幾種策略分析

    本文將對幾種快取與資料庫保證資料一致性的使用方式進行分析。為保證高并發性能,以下分析場景不考慮執行的原子性及加鎖等強一致性要求的場景,僅追求最終一致性。 ......

    uj5u.com 2023-04-20 08:26:48 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:26:35 more
  • 云時代,MySQL到ClickHouse資料同步產品對比推薦

    ClickHouse 在執行分析查詢時的速度優勢很好的彌補了MySQL的不足,但是對于很多開發者和DBA來說,如何將MySQL穩定、高效、簡單的同步到 ClickHouse 卻很困難。本文對比了 NineData、MaterializeMySQL(ClickHouse自帶)、Bifrost 三款產品... ......

    uj5u.com 2023-04-20 08:26:29 more
  • sql陳述句優化

    問題查找及措施 問題查找 需要找到具體的代碼,對其進行一對一優化,而非一直把關注點放在服務器和sql平臺 降低簡化每個事務中處理的問題,盡量不要讓一個事務拖太長的時間 例如檔案上傳時,應將檔案上傳這一步放在事務外面 微軟建議 4.啟動sql定時執行計劃 怎么啟動sqlserver代理服務-百度經驗 ......

    uj5u.com 2023-04-20 08:25:13 more
  • Redis 報”OutOfDirectMemoryError“(堆外記憶體溢位)

    Redis 報錯“OutOfDirectMemoryError(堆外記憶體溢位) ”問題如下: 一、報錯資訊: 使用 Redis 的業務介面 ,產生 OutOfDirectMemoryError(堆外記憶體溢位),如圖: 格式化后的報錯資訊: { "timestamp": "2023-04-17 22: ......

    uj5u.com 2023-04-20 08:24:54 more
  • day02-2-商鋪查詢快取

    功能02-商鋪查詢快取 3.商鋪詳情快取查詢 3.1什么是快取? 快取就是資料交換的緩沖區(稱作Cache),是存盤資料的臨時地方,一般讀寫性能較高。 快取的作用: 降低后端負載 提高讀寫效率,降低回應時間 快取的成本: 資料一致性成本 代碼維護成本 運維成本 3.2需求說明 如下,當我們點擊商店詳 ......

    uj5u.com 2023-04-20 08:24:03 more
  • day02-短信登錄

    功能實作02 2.功能01-短信登錄 2.1基于Session實作登錄 2.1.1思路分析 2.1.2代碼實作 2.1.2.1發送短信驗證碼 發送短信驗證碼: 發送驗證碼的介面為:http://127.0.0.1:8080/api/user/code?phone=xxxxx<手機號> 請求方式:PO ......

    uj5u.com 2023-04-20 08:23:11 more