console
var treeData = {
"name": "fdsafad",
"children": [{
"name": "浙江CDC",
"children": [{
"name": "杭州"
},
{
"name": "宁波"
},
{
"name": "温州"
},
{
"name": "绍兴"
}]
},
{
"name": "广西CDC",
"children": [{
"name": "桂林",
"children": [{
"name": "东湖南路社区服务中心",
"children": [{
"name": "张医生"
},
{
"name": "李医生"
},
{
"name": "李医生"
},
{
"name": "李医生"
},
{
"name": "李医生"
},
{
"name": "李医生"
}]
},
{
"name": "解放北路社区服务中心",
"children": [{
"name": "张医生"
},
{
"name": "李医生"
},
{
"name": "李医生"
}]
}]
},
{
"name": "南宁"
},
{
"name": "柳州"
},
{
"name": "防城港"
}]
},
{
"name": "黑龙江CDC",
"children": [{
"name": "哈尔滨"
},
{
"name": "齐齐哈尔"
},
{
"name": "牡丹江"
},
{
"name": "大庆"
}]
},
{
"name": "新疆CDC",
"children": [{
"name": "乌鲁木齐"
},
{
"name": "克拉玛依"
},
{
"name": "吐鲁番"
},
{
"name": "哈密"
}]
}]
};;
var margin = {
top: 20,
right: 90,
bottom: 30,
left: 90
},
width = 1220 - margin.left - margin.right,
height = 900 - margin.top - margin.bottom;
var svg = d3
.select("body")
.append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom);
var view = svg
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var zoom = d3
.zoom()
.scaleExtent([0.1, 100])
.on("zoom", () => {
view.attr(
"transform",
"translate(" +
(d3.event.transform.x + margin.left) +
"," +
(d3.event.transform.y + margin.top) +
") scale(" +
d3.event.transform.k +
")"
);
});
svg.call(zoom).on("dblclick.zoom", () => {});
var i = 0,
duration = 750,
root;
var userMenuData = [
[{
text: "菜单1",
func: function () {
var id = Number($(this).attr("id"))
alert("菜单1", id)
}
},
{
text: "菜单2",
func: function () {
var id = Number($(this).attr("id"))
alert("菜单1", id)
}
}
]
];
initData();
function initData() {
root = d3.hierarchy(treeData, function (d) {
return d.children;
});
root.x0 = height / 2;
root.y0 = 0;
updateChart(root);
}
function updateChart(source) {
var scale =
(getDepth(root) / 8 || 0.5) +
(getMax(root) / 12 || 0.5);
var treemap = d3.tree().size([height * scale, width]);
var treeData = treemap(root);
var nodes = treeData.descendants(),
links = treeData.descendants().slice(1);
nodes.forEach(function (d) {
d.y = d.depth * 180;
});
updateNodes(source, nodes);
updateLinks(source, links);
nodes.forEach(function (d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
function updateNodes(source, nodes) {
var node = view.selectAll("g.node").data(nodes, function (d) {
return d.id || (d.id = ++i);
});
var nodeEnter = node
.enter()
.append("g")
.attr("class", "node")
.attr("id", d => d.id)
.attr("transform", function (d) {
return "translate(" + source.y0 + "," + source.x0 + ")";
})
.on("mouseover", d => {
var transform = d3.event;
var yPosition = transform.offsetY + 20;
var xPosition = transform.offsetX + 20;
var chartTooltip = d3
.select(".chartTooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px");
chartTooltip.select(".name").text(d.data.name);
chartTooltip.classed("hidden", false);
})
.on("mouseout", () => {
d3.select(".chartTooltip").classed("hidden", true);
})
.on("click", click)
.on("dblclick", dblclick);
nodeEnter
.append("circle")
.attr("class", "node")
.attr("r", 1e-6)
.style("fill", function (d) {
return d._children ? "lightsteelblue" : d.data.children ? "#fff" : "#aaa";
});
nodeEnter
.append("text")
.attr("dy", ".35em")
.attr("x", function (d) {
return d.children || d._children ? -13 : 13;
})
.attr("text-anchor", function (d) {
return d.children || d._children ? "end" : "start";
})
.text(function (d) {
return d.data.name;
});
var nodeUpdate = nodeEnter.merge(node);
nodeUpdate
.transition()
.duration(duration)
.attr("transform", function (d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeUpdate
.select("circle.node")
.attr("r", 10)
.style("fill", function (d) {
return d._children ? "lightsteelblue" : d.data.children ? "#fff" : "#aaa";
})
.attr("cursor", "pointer");
var nodeExit = node
.exit()
.transition()
.duration(duration)
.attr("transform", function (d) {
return "translate(" + source.y + "," + source.x + ")";
})
.remove();
nodeExit.select("circle").attr("r", 1e-6);
nodeExit.select("text").style("fill-opacity", 1e-6);
}
function updateLinks(source, links) {
var link = view.selectAll("path.link").data(links, function (d) {
return d.id;
});
var linkEnter = link
.enter()
.insert("path", "g")
.attr("class", "link")
.attr("d", function (d) {
var o = {
x: source.x0,
y: source.y0
};
return diagonal(o, o);
});
var linkUpdate = linkEnter.merge(link);
linkUpdate
.transition()
.duration(duration)
.attr("d", function (d) {
return diagonal(d, d.parent);
});
var linkExit = link
.exit()
.transition()
.duration(duration)
.attr("d", function (d) {
var o = {
x: source.x,
y: source.y
};
return diagonal(o, o);
})
.remove();
}
function click(d) {
if (d._clickid) {
clearTimeout(d._clickid);
d._clickid = null;
} else {
d._clickid = setTimeout(() => {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
updateChart(d);
d._clickid = null;
}, 350);
}
}
function dblclick(d) {
if (!(d.data && d.data.children)) {
var randomNum = Math.floor(Math.random() * 5) + 1;
var children = {
"name": "Test 1",
"children": [
{
"name": "解放北路社区服务中心A" + randomNum
},
{
"name": "解放北路社区服务中心B" + randomNum
},
{
"name": "解放北路社区服务中心C" + randomNum
},
{
"name": "解放北路社区服务中心D" + randomNum
},
{
"name": "解放北路社区服务中心E" + randomNum
},
{
"name": "解放北路社区服务中心F" + randomNum
}
]
}
.children.map(x => {
return {
name: x.name,
parent: d,
depth: d.depth + 1,
data: {
...x
}
}
});
if (children.length) d.children = children;
d.data.children = children;
updateChart(d);
}
}
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
function diagonal(s, d) {
path =
`M ${s.y} ${s.x}
C ${(s.y + d.y) / 2} ${s.x},
${(s.y + d.y) / 2} ${d.x},
${d.y} ${d.x}`;
return path;
}
function getMax(obj) {
let max = 0;
if (obj.children) {
max = obj.children.length;
obj.children.forEach(d => {
const tmpMax = this.getMax(d);
if (tmpMax > max) {
max = tmpMax;
}
});
}
return max;
}
function getDepth(obj) {
var depth = 0;
if (obj.children) {
obj.children.forEach(d => {
var tmpDepth = this.getDepth(d);
if (tmpDepth > depth) {
depth = tmpDepth;
}
});
}
return 1 + depth;
}
<div class="chartTooltip hidden">
<p>
<strong class="name"></strong>
</p>
</div>
<script src="https://d3js.org/d3.v5.min.js"></script>
@charset "utf-8";
.smart_menu_box{display:none; width:300px; position:absolute; z-index:201105;}
.smart_menu_body{padding:1px; border:1px solid #B8CBCB; background-color:#fff; -moz-box-shadow:2px 2px 5px #666; -webkit-box-shadow:2px 2px 5px #666; box-shadow:2px 2px 5px #666;}
.smart_menu_ul{margin:0; padding:0; list-style-type:none;}
.smart_menu_li{position:relative;}
.smart_menu_a{display:block; height:25px; line-height:24px; padding:0 5px 0 25px; color:#000; font-size:12px; text-decoration:none; overflow:hidden;}
.smart_menu_a:hover, .smart_menu_a_hover{background-color:#348CCC; color:#fff; text-decoration:none;}
.smart_menu_li_separate{line-height:0; margin:3px; border-bottom:1px solid #B8CBCB; font-size:0;}
.smart_menu_triangle{width:0; height:0; border:5px dashed transparent; border-left:5px solid #666; overflow:hidden; position:absolute; top:7px; right:5px;}
.smart_menu_a:hover .smart_menu_triangle, .smart_menu_a_hover .smart_menu_triangle{border-left-color:#fff;}
.smart_menu_li_hover .smart_menu_box{top:-1px; left:130px;}
.smart_menu_li:first-child a, .smart_menu_li:nth-child(2) a, .smart_menu_li:nth-child(3) a, .smart_menu_li:nth-child(4) a{
cursor: default;
}
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
.chartTooltip {
position: absolute;
width: 200px;
height: auto;
padding: 10px;
box-sizing: border-box;
background-color: white;
border-radius: 5px;
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.4);
pointer-events: none;
}
.chartTooltip.hidden {
display: none;
}
.chartTooltip p {
margin: 0;
font-size: 14px;
line-height: 20px;
word-wrap: break-word;
}