当前位置:网站首页>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

边栏推荐
- 虚假新闻检测论文阅读(五):A Semi-supervised Learning Method for Fake News Detection in Social Media
- 【语义分割】SETR_Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformer
- Semaphore (semaphore) for learning notes of concurrent programming
- 有价值的博客、面经收集(持续更新)
- Detailed explanation of MySQL statistical function count
- 【Attention】Visual Attention Network
- 【卷积核设计】Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs
- ANR优化:导致 OOM 崩溃及相对应的解决方案
- Detailed explanation of tool classes countdownlatch and cyclicbarrier of concurrent programming learning notes
- 研究生新生培训第三周:ResNet+ResNeXt
猜你喜欢

tensorboard使用

迁移学习——Transitive Transfer Learning

Ffmpeg creation GIF expression pack tutorial is coming! Say thank you, brother black fly?

【Transformer】SOFT: Softmax-free Transformer with Linear Complexity

通过简单的脚本在Linux环境实现Mysql数据库的定时备份(Mysqldump命令备份)

Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl解决办法

The third week of postgraduate freshman training: resnet+resnext

Android studio login registration - source code (connect to MySQL database)

【Transformer】TransMix: Attend to Mix for Vision Transformers

PyTorch基础知识(可入门)
随机推荐
The third week of postgraduate freshman training: resnet+resnext
虚假新闻检测论文阅读(一):Fake News Detection using Semi-Supervised Graph Convolutional Network
Are you sure you know the interaction problem of activity?
Operation commands in anaconda, such as removing old environment, adding new environment, viewing environment, installing library, cleaning cache, etc
【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer
[semantic segmentation] setr_ Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformer
MySQL inserts millions of data (using functions and stored procedures)
clion+opencv+aruco+cmake配置
第2周学习:卷积神经网络基础
Android studio login registration - source code (connect to MySQL database)
mysql 的show profiles 使用。
【数据库】数据库课程设计一一疫苗接种数据库
简单聊聊 PendingIntent 与 Intent 的区别
第一周任务 深度学习和pytorch基础
The differences and reasons between MySQL with and without quotation marks when querying string types
ROS教程(Xavier)
迁移学习——Transitive Transfer Learning
【Attention】Visual Attention Network
Isaccessible() method: use reflection techniques to improve your performance several times
【卷积核设计】Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs