当前位置:网站首页>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>
更多文章请扫码关注公众号,有问题的小伙伴也可以公众号上提出哦
边栏推荐
- Masscan使用教程.
- ThinkPHP6 输出二维码图片格式 解决与 Debug 的冲突
- DAY6:利用 PHP 编写登陆页面
- 使用Qss设置窗体样式
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- 打印出1-100之间的所有质数
- 超简单集成HMS ML Kit 实现parental control
- The difference between link and @import importing external styles
- 『全闪实测』数据库加速解决方案
- [untitled]
猜你喜欢
How can Plato obtain premium income through elephant swap in a bear market?
[typescript] in depth study of typescript functions
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
“山东大学移动互联网开发技术教学网站建设”项目实训日志二
剑指核心-TaoCloud全闪SDS助力构建高性能云服务
Seay源代码审计系统
Crypto giants all in metauniverse, and platofarm may break through
Fantom (FTM) 价格将在未来几天飙升 20%
如何在加密市场熊市中生存?
极致通缩和永动机模型,将推动 PlatoFarm 爆发
随机推荐
Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
Idea using JDBC to connect mysql database personal detailed tutorial
如何 Pr 一个开源composer项目
Mobile terminal -flex item attribute
Fantom (FTM) prices will soar by 20% in the next few days
农村品牌建设给年轻人的一些机会
MOVE PROTOCOL全球健康宣言,将健康运动进行到底
Display effect of uniapp page title
“山东大学移动互联网开发技术教学网站建设”项目实训日志三
加密资产熊市之下,PlatoFarm的策略玩法依旧能获得稳定收益
量化开发必掌握的30个知识点【什么是Level-2数据】
浅谈分布式全闪存储自动化测试平台设计
WIN10 编译ffmpeg(包含ffplay)
H5 semantic label
Sports health is deeply rooted in the hearts of the people, and move protocol leads quality life
PHP如何生成二维码?
DAY6:利用 PHP 编写文件上传页面
DeFi 2.0的LaaS协议,重振DeFi赛道发展的关键
一文读懂Move2Earn项目——MOVE
What is nmap and how to use it