[D3] Add image to the node

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[D3] Add image to the node相关的知识,希望对你有一定的参考价值。

We can create node with ‘g‘ container, then append ‘image‘ to the nodes.

 

            // Create container for the images
            const svgNodes = svg
                .append(‘g‘)
                .attr(‘class‘, ‘nodes‘)
                .selectAll(‘circle‘)
                .data(d3.values(nodes))
                .enter().append(‘g‘);

            // Add image to the nodes
            svgNodes
                .append(‘image‘)
                .attr(‘xlink:href‘, d => `/static/media/${d.name.toLowerCase()}.png`)
                .attr(‘x‘, -25)
                .attr(‘y‘, -25)
                .attr(‘height‘, 50)
                .attr(‘width‘, 50);

 

Then on each ‘tick‘, we need to position the nodes:

            simulation
                .nodes(d3.values(nodes))
                .on(‘tick‘, ticked);

            function dragstarted(d) {
                if (!d3.event.active) simulation.alphaTarget(0.3).restart();
                d.fx = d.x;
                d.fy = d.y;
            }

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

            function dragended(d) {
                if (!d3.event.active) simulation.alphaTarget(0);
                d.fx = null;
                d.fy = null;
            }

            function ticked() {
                svgNodes
                    .attr(‘transform‘, d =>`translate(${d.x},${d.y})`)
                    .call(d3.drag()
                        .on(‘start‘, dragstarted)
                        .on(‘drag‘, dragged)
                        .on(‘end‘, dragended));

            }

 

以上是关于[D3] Add image to the node的主要内容,如果未能解决你的问题,请参考以下文章

swift - use backslash to add the value in the string

How to add the ApplicationPoolIdentity to a SQL Server Login

How to add the custom nuget feed to TeamCity build?

ERROR in : Cannot determine the module for class xxxx Add to the NgModule to fix it.

spark启动失败:localhost failed to add the host to the list of known hosts

spark启动失败:localhost failed to add the host to the list of known hosts