当前位置:网站首页>D3.js vertical relationship diagram (with arrows and text description of connecting lines)
D3.js vertical relationship diagram (with arrows and text description of connecting lines)
2022-07-29 06:06:00 【Denny Hui】
<script src="d3.js"></script>
<style>
.node {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<script>
var width = 1100,
height = 700;
var cluster = d3.layout.tree()
.size([width, height - 200]);
var diagonal = d3.svg.diagonal()
.projection(function (d) {
return [d.x, d.y];
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + 612 + "," + 50 + ")");
// arrow
var marker =
svg.append("marker")
.attr("id", "resolved")
.attr("markerUnits", "strokeWidth")// Set to strokeWidth The arrow will change with the thickness of the line
.attr("markerUnits", "userSpaceOnUse")
.attr("viewBox", "0 -5 10 10")// The area of the coordinate system
.attr("refX", 10)// Arrow coordinates
.attr("refY", -1)
.attr("markerWidth", 12)// Size of identity
.attr("markerHeight", 12)
.attr("orient", "auto")// Draw the direction , Can be set to :auto( Automatically confirm the direction ) and Angle value
.attr("stroke-width", 2)// Arrow width
.append("path")
.attr("d", "M0,-5L10,0L0,5")// The path of the arrow
.attr('fill', '#000000');// The arrow color
var root = {
"name": " China ",
"number": "2000",
"children":
[
{
"name": " Zhejiang ",
"number": "100",
"children":
[
{
"name": " Hangzhou ",
"number": "100"
},
{
"name": " ningbo ",
"number": "200"
}
]
},
{
"name": " guangxi ",
"number": "500",
"children":
[
{
"name": " guilin ",
"number": "100"
},
{
"name": " nanning ",
"number": "200"
}
]
},
{
"name": " heilongjiang ",
"number": "500",
"children":
[
{
"name": " Harbin ",
"number": "100"
}
]
},
{
"name": " xinjiang ",
"number": "500",
"children":
[
{
"name": " urumqi ",
"number": "100"
}
]
}]
}
var i = 0;
var nodes = cluster.nodes(root).reverse();
nodes.forEach(function (d) { d.y = d.depth * 200; });
var links = cluster.links(nodes);
var linkEnter = svg.selectAll("path.link")
.data(links);
linkEnter.enter().append("path")// Insert an element before the specified element
.attr("class", "link")
.attr("d", diagonal)
.attr("stroke", "white")
.attr("marker-end", "url(#resolved)")// According to the arrow mark id Mark arrow
.style("fill", "white")
.style("fill-opacity", 1)
// First, add an identifier for each node connection id
.attr("id", function (d, i) {
return "mypath" + i;
});
// Add text to the line
linkEnter.enter().append('text')
.attr('x', 100)
.attr('y', 80)
.style('fill', 'green')
.style('font-size', '15px')
.style('font-weight', 'bold')
.append('textPath')
.attr({// Reference path
'xlink:href': function (d, i) {
return "#mypath" + i;
}
})
.text(function (d, i) {
debugger
return d.target.number
});
var node = svg.selectAll(".node")
.data(nodes)
.enter()
.append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "translate(" + (d.x + -50) + "," + (d.y) + ")";
})
node.append("rect")
.attr("width", 100)
.attr("height", 40)
.attr("x", 0)
.attr("y", 0)
.attr("style", "fill:#2990ca;")
.attr("rx", 3);
node.append("text")
.attr("dx", function (d) {
return 30;
})
.attr("dy", 25)
.style("text-anchor", function (d) {
return "middle";
})
.style("fill", "#fff")
.text(function (d) { return d.name; });
node.append("text")
.attr("dx", function (d) {
return 70;
})
.attr("dy", 25)
.style("text-anchor", function (d) {
return "middle";
})
.style("fill", "#fff")
.text(function (d) { return d.number; });
</script>
For more articles, please scan the code and follow the official account , If you have problems, you can also raise them on the official account
边栏推荐
- 虚假新闻检测论文阅读(二):Semi-Supervised Learning and Graph Neural Networks for Fake News Detection
- 【语义分割】语义分割综述
- Markdown syntax
- 虚假新闻检测论文阅读(三):Semi-supervised Content-based Detection of Misinformation via Tensor Embeddings
- 【语义分割】SETR_Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformer
- Exploration of flutter drawing skills: draw arrows together (skill development)
- Transformer回顾+理解
- 主流实时流处理计算框架Flink初体验。
- 简单聊聊 PendingIntent 与 Intent 的区别
- Tear the ORM framework by hand (generic + annotation + reflection)
猜你喜欢
第三周周报 ResNet+ResNext
【Attention】Visual Attention Network
C # judge whether the user accesses by mobile phone or computer
[tensorrt] convert pytorch into deployable tensorrt
【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer
ROS常用指令
Configuration and use of Nacos external database
[semantic segmentation] Introduction to mapillary dataset
Ribbon学习笔记一
Flink, the mainstream real-time stream processing computing framework, is the first experience.
随机推荐
迁移学习—— Transfer Feature Learning with Joint Distribution Adaptation
Research on the implementation principle of reentrantlock in concurrent programming learning notes
【语义分割】语义分割综述
并发编程学习笔记 之 工具类CountDownLatch、CyclicBarrier详解
五、图像像素统计
[tensorrt] convert pytorch into deployable tensorrt
ROS教程(Xavier)
【TensorRT】将 PyTorch 转化为可部署的 TensorRT
【Clustrmaps】访客统计
【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer
【数据库】数据库课程设计一一疫苗接种数据库
Valuable blog and personal experience collection (continuous update)
并发编程学习笔记 之 Lock锁及其实现类ReentrantLock、ReentrantReadWriteLock和StampedLock的基本用法
Analysis on the principle of flow
并发编程学习笔记 之 原子操作类AtomicInteger详解
【Transformer】SOFT: Softmax-free Transformer with Linear Complexity
Ribbon学习笔记二
Is flutter being quietly abandoned? On the future of flutter
【卷积核设计】Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs
并发编程学习笔记 之 ReentrantLock实现原理的探究