当前位置:网站首页>D3.JS 纵向关系图(加箭头,连接线文字描述)
D3.JS 纵向关系图(加箭头,连接线文字描述)
2022-07-29 05:20:00 【Denny辉】

<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 + ")");
//箭头
var marker =
svg.append("marker")
.attr("id", "resolved")
.attr("markerUnits", "strokeWidth")//设置为strokeWidth箭头会随着线的粗细发生变化
.attr("markerUnits", "userSpaceOnUse")
.attr("viewBox", "0 -5 10 10")//坐标系的区域
.attr("refX", 10)//箭头坐标
.attr("refY", -1)
.attr("markerWidth", 12)//标识的大小
.attr("markerHeight", 12)
.attr("orient", "auto")//绘制方向,可设定为:auto(自动确认方向)和 角度值
.attr("stroke-width", 2)//箭头宽度
.append("path")
.attr("d", "M0,-5L10,0L0,5")//箭头的路径
.attr('fill', '#000000');//箭头颜色
var root = {
"name": "中国",
"number": "2000",
"children":
[
{
"name": "浙江",
"number": "100",
"children":
[
{
"name": "杭州",
"number": "100"
},
{
"name": "宁波",
"number": "200"
}
]
},
{
"name": "广西",
"number": "500",
"children":
[
{
"name": "桂林",
"number": "100"
},
{
"name": "南宁",
"number": "200"
}
]
},
{
"name": "黑龙江",
"number": "500",
"children":
[
{
"name": "哈尔滨",
"number": "100"
}
]
},
{
"name": "新疆",
"number": "500",
"children":
[
{
"name": "乌鲁木齐",
"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")//在指定元素之前插入一个元素
.attr("class", "link")
.attr("d", diagonal)
.attr("stroke", "white")
.attr("marker-end", "url(#resolved)")//根据箭头标记的id号标记箭头
.style("fill", "white")
.style("fill-opacity", 1)
// 首先为每条节点连线添加标识id
.attr("id", function (d, i) {
return "mypath" + i;
});
//为连线添加文字
linkEnter.enter().append('text')
.attr('x', 100)
.attr('y', 80)
.style('fill', 'green')
.style('font-size', '15px')
.style('font-weight', 'bold')
.append('textPath')
.attr({//引用路径
'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>更多文章请扫码关注公众号,有问题的小伙伴也可以公众号上提出哦

边栏推荐
- What is nmap and how to use it
- Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)
- 熊市下PLATO如何通过Elephant Swap,获得溢价收益?
- 与多家机构战略合作,背后彰显PlatoFarm元宇宙龙头的实力
- Common prompt pop-up box of uniapp
- Wechat applet - screen height
- Idea using JDBC to connect mysql database personal detailed tutorial
- Novice introduction: download from PHP environment to thinkphp6 framework by hand
- Crypto giants all in metauniverse, and platofarm may break through
- WIN10 编译ffmpeg(包含ffplay)
猜你喜欢

马斯克推崇的柏拉图式元宇宙,PlatoFarm早已验证出答案

极致通缩和永动机模型,将推动 PlatoFarm 爆发

Windows下cmd窗口连接mysql并操作表

量化开发必掌握的30个知识点【什么是分笔逐笔数据】?
![[typescript] learn typescript object types in depth](/img/87/a9fd2f177331863e06fcf14559eeed.png)
[typescript] learn typescript object types in depth

Wapiti是什么以及使用教程

iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration

QT setting background image method
随机推荐
Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
php写一个购买全网最低价的纸尿裤
How to survive in the bear market of encryption market?
Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)
北京宝德&TaoCloud共建信创之路
July 28 ens/usd Value Forecast: ENS attracts huge profits
Idea using JDBC to connect mysql database personal detailed tutorial
QT setting background image method
Starfish OS:以现实为纽带,打造元宇宙新范式
QPalette学习笔记
完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
DAY5:PHP 简单语法与使用
The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere
Go|gin quickly use swagger
“山东大学移动互联网开发技术教学网站建设”项目实训日志一
What is nmap and how to use it
“山东大学移动互联网开发技术教学网站建设”项目实训日志四
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
Wechat applet - component parameter transmission, state management