[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));

            }
时间: 2024-10-12 19:48:01

[D3] Add image to the node的相关文章

ceph add a osd

Proper Way to Remove an OSD  Run 'ceph osd tree' to list OSD devices and look for the number of the failed OSD or the OSD to be removed.  #  ceph osd tree  We are going to remove OSD.8 for example. Login to the OSD node first and start the following

Some words about Qt DOM Node, Element and Attribut

Add Instruction Node Every valid XML must contain processing instruction. XML is widely used for HTML, SVG, XLS etc. So make sure your XML file has valid instruction of its type and encoding. The following line is a sample XML processing instruction.

关于Visual Studio Code的Node.js提示

试用了下VSC, 感觉比sublime还不错, 但是苦恼的时候编写node.js的时候没有任何的提示. 原来很简单, 例如一个最简单的http.js: var http = require('http'); var port = process.env.port || 1337;http.createServer(function(req,res){ res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello node.j

类型TTreeView.items.add 与 TTreeView.items.addchild有何区别?(10分)

我看了书上例子,好象两者都可以实现treeview中的node 的构建. addchild是给当前的node建一个子node,它比当前node要向右缩进几格add建立同级的node,不缩进 aNode :=TreeView1.Items.Add(Node, S)aNode 与 Node 是兄弟.bNode :=TreeView1.Items.AddChild(Node, s);bNode 是 Node 的儿子. TreeView.Item.add增加的是同一级的结点:TreeView.Item.

D3js-API介绍【中】

JavaScript可视化图表库D3.js API中文參考,d3.jsapi D3 库所提供的全部 API 都在 d3 命名空间下.d3 库使用语义版本号命名法(semantic versioning). 你能够用 d3.version 查看当前的版本号信息. d3 (核心部分) 选择集 d3.select - 从当前文档中选择一系列元素. d3.selectAll - 从当前文档中选择多项元素. selection.attr - 设置或获取指定属性. selection.classed - 加

java实现简单LinkedList

1 package LinkedListClass; 2 3 public class Node { 4 Node parents; 5 Object myself; 6 Node child; 7 8 public Node() { 9 10 } 11 12 public Node(Node parents, Object myself, Node child) { 13 super(); 14 this.parents = parents; 15 this.myself = myself;

HDU 1828 Picture(矩形周长并)

HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4个方向都加完就是答案 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 5005; int n; struct Rec { int

设计模式09-组合模式

1. 概念 有时候又叫做部分-整体模式    存在整体和部分的时候  希望客户端忽略整体和部分的区别 2. 案例 /********************************************************************** * <pre> * FILE : Demo01.java * CLASS : Demo01 * * AUTHOR : Liaokailin * * FUNCTION : TODO * * *=========================

94. Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 在做99. Recover Binary Search Tree 时,要求不用O(n)的