本文基于Adoins前輩D3 縱向組織結構樹文章實現(xiàn)個性化改變腋颠。
- 左側樹
<!DOCTYPE html>
<html>
<head>
<title>左側樹</title>
<script type="text/javascript" src="http://d3js.org/d3.v5.min.js">
</script>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<svg width="960" height="600"></svg>
<script>
//定義邊界
var marge = {top:50, bottom:0, left:10, right:0};
var svg = d3.select("svg");
var width = svg.attr("width");
var height = svg.attr("height");
var g = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
var scale = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
//數(shù)據(jù)
var dataset = {
name:"中國",
children:[
{
name:"浙江",
children:[
{name:"杭州" ,value:100},
{name:"寧波",value:100},
{name:"溫州",value:100},
{name:"紹興",value:100}
]
},
{
name:"廣西",
children:[
{
name:"桂林",
children:[
{name:"秀峰區(qū)",value:100},
{name:"疊彩區(qū)",value:100},
{name:"象山區(qū)",value:100},
{name:"七星區(qū)",value:100}
]
},
{name:"南寧",value:100},
{name:"柳州",value:100},
{name:"防城港",value:100}
]
},
{
name:"黑龍江",
children:[
{name:"哈爾濱",value:100},
{name:"齊齊哈爾",value:100},
{name:"牡丹江",value:100},
{name:"大慶",value:100}
]
},
{
name:"新疆" ,
children:
[
{name:"烏魯木齊"},
{name:"克拉瑪依"},
{name:"吐魯番"},
{name:"哈密"}
]
}
]
};
//創(chuàng)建一個hierarchy layout
var hierarchyData = d3.hierarchy(dataset)
.sum(function(d){
return d.value;
});
//創(chuàng)建一個樹狀圖
var tree = d3.tree()
.size([width-400,height-200])
.separation(function(a,b){
return (a.parent==b.parent?1:2)/a.depth;
})
//初始化樹狀圖,也就是傳入數(shù)據(jù),并得到繪制樹基本數(shù)據(jù)
var treeData = tree(hierarchyData);
console.log(treeData);
//得到節(jié)點
var nodes = treeData.descendants();
var links = treeData.links();
//輸出節(jié)點和邊
console.log(nodes);
console.log(links);
//創(chuàng)建一個貝塞爾生成曲線生成器
var Bézier_curve_generator = d3.linkHorizontal()
.x(function(d) { return d.y; })
.y(function(d) { return d.x; });
//有了節(jié)點和邊集的數(shù)據(jù)后拧抖,我們就可以開始繪制了,
//繪制邊
g.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("d",function(d){
var start = {x:d.source.x,y:d.source.y};
var end = {x:d.target.x,y:d.target.y};
return Bézier_curve_generator({source:start,target:end});
})
.attr("fill","none")
.attr("stroke","yellow")
.attr("stroke-width",1);
//繪制節(jié)點和文字
//老規(guī)矩绑嘹,先創(chuàng)建用以繪制每個節(jié)點和對應文字的分組<g>
var gs = g.append("g")
.selectAll("g")
.data(nodes)
.enter()
.append("g")
.attr("transform",function(d){
var cx = d.x;
var cy= d.y;
return "translate("+cy+","+cx+")";
});
//繪制節(jié)點
gs.append("circle")
.attr("r",6)
.attr("fill","white")
.attr("stroke","blue")
.attr("stroke-width",1);
//文字
gs.append("text")
.attr("x",function(d){
return d.children?-40:8;
})
.attr("y",-5)
.attr("dy",10)
.text(function(d){
return d.data.name;
});
</script>
</body>
</html>
-
縱向樹
<!DOCTYPE html>
<html>
<head>
<title>垂直樹</title>
<script type="text/javascript" src="http://d3js.org/d3.v5.min.js">
</script>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<svg width="960" height="600"></svg>
<script>
//定義邊界
var marge = {top:50, bottom:0, left:30, right:0};
var svg = d3.select("svg");
var width = svg.attr("width");
var height = svg.attr("height");
var g = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
var scale = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
//數(shù)據(jù)
var dataset = {
name:"中國",
children:[
{
name:"浙江",
children:[
{name:"杭州" ,value:100},
{name:"寧波",value:100},
{name:"溫州",value:100},
{name:"紹興",value:100}
]
},
{
name:"廣西",
children:[
{
name:"桂林",
children:[
{name:"秀峰區(qū)",value:100},
{name:"疊彩區(qū)",value:100},
{name:"象山區(qū)",value:100},
{name:"七星區(qū)",value:100}
]
},
{name:"南寧",value:100},
{name:"柳州",value:100},
{name:"防城港",value:100}
]
},
{
name:"黑龍江",
children:[
{name:"哈爾濱",value:100},
{name:"齊齊哈爾",value:100},
{name:"牡丹江",value:100},
{name:"大慶",value:100}
]
},
{
name:"新疆" ,
children:
[
{name:"烏魯木齊"},
{name:"克拉瑪依"},
{name:"吐魯番"},
{name:"哈密"}
]
}
]
};
//創(chuàng)建一個hierarchy layout
var hierarchyData = d3.hierarchy(dataset)
.sum(function(d){
return d.value;
});
//創(chuàng)建一個樹狀圖
var tree = d3.tree()
.size([width-400,height-200])
.separation(function(a,b){
return (a.parent==b.parent?1:2)/a.depth;
})
//初始化樹狀圖郭脂,也就是傳入數(shù)據(jù),并得到繪制樹基本數(shù)據(jù)
var treeData = tree(hierarchyData);
console.log(treeData);
//得到節(jié)點
var nodes = treeData.descendants();
var links = treeData.links();
//輸出節(jié)點和邊
console.log(nodes);
console.log(links);
//創(chuàng)建一個貝塞爾生成曲線生成器 // 水平:d3.linkHorizontal() 垂直:d3.linkVertical()
var Bézier_curve_generator = d3.linkVertical()
.x(function(d) { return d.x; }) // 水平:d.y 垂直:d.x
.y(function(d) { return d.y; });// 水平:d.x 垂直:d.y
//有了節(jié)點和邊集的數(shù)據(jù)后年碘,我們就可以開始繪制了,
//繪制邊
g.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("d",function(d){
var start = {x:d.source.x,y:d.source.y};
var end = {x:d.target.x,y:d.target.y};
return Bézier_curve_generator({source:start,target:end});
})
.attr("fill","none")
.attr("stroke","yellow")
.attr("stroke-width",1);
//繪制節(jié)點和文字
//老規(guī)矩展鸡,先創(chuàng)建用以繪制每個節(jié)點和對應文字的分組<g>
var gs = g.append("g")
.selectAll("g")
.data(nodes)
.enter()
.append("g")
.attr("transform",function(d){
var cx = d.y; // 水平:d.x 垂直:d.y
var cy= d.x; // 水平:d.y 垂直:d.x
return "translate(" + cy + "," + cx + ")";
});
//繪制節(jié)點
gs.append("circle")
.attr("r",6)
.attr("fill","white")
.attr("stroke","blue")
.attr("stroke-width",1);
//文字
gs.append("text")
.attr("x", function(d){
return d.children ? -15 : 0;
})
.attr("y", function(d) {
return d.children ? -10 : 15;
})
.attr("dy",0)
.text(function(d){
return d.data.name;
})
.style('writing-mode', function(d) {
return d.children ? 'horizontal-tb' : 'tb-rl';
});
</script>
</body>
</html>
-
縱向樹2
<!DOCTYPE html>
<html>
<head>
<title>垂直直線樹</title>
<script type="text/javascript" src="http://d3js.org/d3.v5.min.js">
</script>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<svg width="1280" height="600"></svg>
<script>
//定義邊界
var marge = {top:50, bottom:0, left:10, right:40};
var svg = d3.select("svg");
var width = svg.attr("width");
var height = svg.attr("height");
var g = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
var scale = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
//數(shù)據(jù)
var dataset = {
name:"中國",
children:[
{
name:"浙江",
children:[
{name:"杭州" ,value:100},
{name:"寧波",value:100},
{name:"溫州",value:100},
{name:"紹興",value:100}
]
},
{
name:"廣西",
children:[
{
name:"桂林",
children:[
{name:"秀峰區(qū)",value:100},
{name:"疊彩區(qū)",value:100},
{name:"象山區(qū)",value:100},
{name:"七星區(qū)",value:100}
]
},
{name:"南寧",value:100},
{name:"柳州",value:100},
{name:"防城港",value:100}
]
},
{
name:"黑龍江",
children:[
{name:"哈爾濱",value:100},
{name:"齊齊哈爾",value:100},
{name:"牡丹江",value:100},
{name:"大慶",value:100}
]
},
{
name:"新疆" ,
children:
[
{name:"烏魯木齊"},
{name:"克拉瑪依"},
{name:"吐魯番"},
{name:"哈密"}
]
}
]
};
//創(chuàng)建一個hierarchy layout
var hierarchyData = d3.hierarchy(dataset)
.sum(function(d){
return d.value;
});
console.log(hierarchyData);
//創(chuàng)建一個樹狀圖
var tree = d3.tree()
.size([width-80,height-200])
.separation(function(a,b){
return a.parent == b.parent ? 1 : 2;
})
//初始化樹狀圖屿衅,也就是傳入數(shù)據(jù),并得到繪制樹基本數(shù)據(jù)
var treeData = tree(hierarchyData);
console.log(treeData);
//得到節(jié)點
var nodes = treeData.descendants();
var links = treeData.links();
//輸出節(jié)點和邊
console.log(nodes);
console.log(links);
//創(chuàng)建一個貝塞爾生成曲線生成器 // 水平:d3.linkHorizontal() 垂直:d3.linkVertical()
var Bézier_curve_generator = d3.linkVertical()
.x(function(d) { return d.x; }) // 水平:d.y 垂直:d.x
.y(function(d) { return d.y; });// 水平:d.x 垂直:d.y
//有了節(jié)點和邊集的數(shù)據(jù)后,我們就可以開始繪制了娱颊,
//繪制邊
var boxWidth = 66, boxHeight = 40;
g.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("d",function(d){
let sourceX = d.source.x,
sourceY = d.source.y + boxHeight,
targetX = d.target.x,
targetY = d.target.y;
return "M" + sourceX + "," + sourceY +
"V" + ((targetY - sourceY) / 2 + sourceY) +
"H" + targetX +
"V" + targetY;
/*
var start = {x:d.source.x,y:d.source.y};
var end = {x:d.target.x,y:d.target.y};
return Bézier_curve_generator({source:start,target:end});
*/
})
.attr("fill","none")
.attr("stroke","red")
.attr("stroke-width",1);
//繪制節(jié)點和文字
//老規(guī)矩傲诵,先創(chuàng)建用以繪制每個節(jié)點和對應文字的分組<g>
var gs = g.append("g")
.selectAll("g")
.data(nodes)
.enter()
.append("g")
.attr("transform",function(d){
var cx = d.y; // 水平:d.x 垂直:d.y
var cy= d.x; // 水平:d.y 垂直:d.x
return "translate(" + cy + "," + cx + ")";
});
//繪制節(jié)點
/*
gs.append("circle")
.attr("r",6)
.attr("fill","white")
.attr("stroke","blue")
.attr("stroke-width",1);
*/
gs.append("rect")
.attr('y', 0)
.attr('x', function (d) {
boxWidth = d.data.name.length * 22;
return d.depth < 2 ? -(boxWidth / 2) : -(boxHeight / 2)
})
.attr('width', function (d) {
boxWidth = d.data.name.length * 22;
return d.depth < 2 ? boxWidth : boxHeight;
})
.attr('height', function (d) {
boxWidth = d.data.name.length * 22;
return d.depth < 2 ? boxHeight : boxWidth;
})
// 矩形背景色以及邊框顏色寬度
.attr('fill', '#fff')
.attr('stroke', 'steelblue')
.attr('strokeWidth', '1px')
.on('click', function (evt) {
console.log(evt); // 顯示所點擊節(jié)點數(shù)據(jù)
});
//文字
gs.append("text")
.attr('y', function (d) {
return d.depth < 2 ? boxHeight / 2 + 5 : 5;
})
.attr('style', function (d) {
return d.depth < 2 ? '' : "writing-mode: tb;letter-spacing:0px";
})
.attr("text-anchor", function (d) {
return d.depth < 2 ? 'middle' : "start";
})
.text(function(d){
return d.data.name;
});
</script>
</body>
</html>
-
左右反轉(zhuǎn)
<!DOCTYPE html>
<html>
<head>
<title>右側樹</title>
<script type="text/javascript" src="http://d3js.org/d3.v5.min.js">
</script>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<svg width="960" height="600"></svg>
<script>
//定義邊界
var marge = {top:50, bottom:0, left:10, right:0};
var svg = d3.select("svg");
var width = svg.attr("width");
var height = svg.attr("height");
var g = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
var scale = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
//數(shù)據(jù)
var dataset = {
name:"中國",
children:[
{
name:"浙江",
children:[
{name:"杭州" ,value:100},
{name:"寧波",value:100},
{name:"溫州",value:100},
{name:"紹興",value:100}
]
},
{
name:"廣西",
children:[
{
name:"桂林",
children:[
{name:"秀峰區(qū)",value:100},
{name:"疊彩區(qū)",value:100},
{name:"象山區(qū)",value:100},
{name:"七星區(qū)",value:100}
]
},
{name:"南寧",value:100},
{name:"柳州",value:100},
{name:"防城港",value:100}
]
},
{
name:"黑龍江",
children:[
{name:"哈爾濱",value:100},
{name:"齊齊哈爾",value:100},
{name:"牡丹江",value:100},
{name:"大慶",value:100}
]
},
{
name:"新疆" ,
children:
[
{name:"烏魯木齊"},
{name:"克拉瑪依"},
{name:"吐魯番"},
{name:"哈密"}
]
}
]
};
//創(chuàng)建一個hierarchy layout
var hierarchyData = d3.hierarchy(dataset)
.sum(function(d){
return d.value;
});
//創(chuàng)建一個樹狀圖
var treeWidth = width-400; // *****記錄一下樹的總寬度凯砍,做為左側樹反轉(zhuǎn)為右側樹時使用
var tree = d3.tree()
.size([treeWidth,height-200])
.separation(function(a,b){
return (a.parent==b.parent?1:2)/a.depth;
})
//初始化樹狀圖箱硕,也就是傳入數(shù)據(jù),并得到繪制樹基本數(shù)據(jù)
var treeData = tree(hierarchyData);
console.log(treeData);
//得到節(jié)點
var nodes = treeData.descendants();
var links = treeData.links();
//輸出節(jié)點和邊
console.log(nodes);
console.log(links);
//創(chuàng)建一個貝塞爾生成曲線生成器
var Bézier_curve_generator = d3.linkHorizontal()
.x(function(d) { return d.y; })
.y(function(d) { return d.x; });
//有了節(jié)點和邊集的數(shù)據(jù)后,我們就可以開始繪制了悟衩,
//繪制邊
g.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("d",function(d){
var start = {x:d.source.x,y:treeWidth - d.source.y};// *****使用treeWidth減去當前y剧罩,實現(xiàn)反轉(zhuǎn)
var end = {x:d.target.x,y:treeWidth - d.target.y}; // *****使用treeWidth減去當前y,實現(xiàn)反轉(zhuǎn)
return Bézier_curve_generator({source:start,target:end});
})
.attr("fill","none")
.attr("stroke","yellow")
.attr("stroke-width",1);
//繪制節(jié)點和文字
//老規(guī)矩座泳,先創(chuàng)建用以繪制每個節(jié)點和對應文字的分組<g>
var gs = g.append("g")
.selectAll("g")
.data(nodes)
.enter()
.append("g")
.attr("transform",function(d){
var cx = d.x;
var cy= treeWidth - d.y; // *****使用treeWidth減去當前y惠昔,實現(xiàn)反轉(zhuǎn)
return "translate("+cy+","+cx+")";
});
//繪制節(jié)點
gs.append("circle")
.attr("r",6)
.attr("fill","white")
.attr("stroke","blue")
.attr("stroke-width",1);
//文字
gs.append("text")
.attr("x",function(d){
var left = -20 * d.data.name.length;
return d.children ? 10 : left;
})
.attr("y",-5)
.attr("dy",10)
.text(function(d){
return d.data.name;
});
</script>
</body>
</html>
-
縱向反轉(zhuǎn)
<!DOCTYPE html>
<html>
<head>
<title>垂直樹(上)</title>
<script type="text/javascript" src="http://d3js.org/d3.v5.min.js">
</script>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<svg width="960" height="600"></svg>
<script>
//定義邊界
var marge = {top:50, bottom:0, left:120, right:0};
var svg = d3.select("svg");
var width = svg.attr("width");
var height = svg.attr("height");
var g = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
var scale = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
//數(shù)據(jù)
var dataset = {
name:"中國",
children:[
{
name:"浙江",
children:[
{name:"杭州" ,value:100},
{name:"寧波",value:100},
{name:"溫州",value:100},
{name:"紹興",value:100}
]
},
{
name:"廣西",
children:[
{
name:"桂林",
children:[
{name:"秀峰區(qū)",value:100},
{name:"疊彩區(qū)",value:100},
{name:"象山區(qū)",value:100},
{name:"七星區(qū)",value:100}
]
},
{name:"南寧",value:100},
{name:"柳州",value:100},
{name:"防城港",value:100}
]
},
{
name:"黑龍江",
children:[
{name:"哈爾濱",value:100},
{name:"齊齊哈爾",value:100},
{name:"牡丹江",value:100},
{name:"大慶",value:100}
]
},
{
name:"新疆" ,
children:
[
{name:"烏魯木齊"},
{name:"克拉瑪依"},
{name:"吐魯番"},
{name:"哈密"}
]
}
]
};
//創(chuàng)建一個hierarchy layout
var hierarchyData = d3.hierarchy(dataset)
.sum(function(d){
return d.value;
});
//創(chuàng)建一個樹狀圖
var treeHeight = height-200; //*******記錄一下樹的高度,上反轉(zhuǎn)使用
var tree = d3.tree()
.size([width-400,treeHeight])
.separation(function(a,b){
return (a.parent==b.parent?1:2)/a.depth;
})
//初始化樹狀圖挑势,也就是傳入數(shù)據(jù),并得到繪制樹基本數(shù)據(jù)
var treeData = tree(hierarchyData);
console.log(treeData);
//得到節(jié)點
var nodes = treeData.descendants();
var links = treeData.links();
//輸出節(jié)點和邊
console.log(nodes);
console.log(links);
//創(chuàng)建一個貝塞爾生成曲線生成器 // 水平:d3.linkHorizontal() 垂直:d3.linkVertical()
var Bézier_curve_generator = d3.linkVertical()
.x(function(d) { return d.x; }) // 水平:d.y 垂直:d.x
.y(function(d) { return d.y; });// 水平:d.x 垂直:d.y
//有了節(jié)點和邊集的數(shù)據(jù)后镇防,我們就可以開始繪制了,
//繪制邊
g.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("d",function(d){
var start = {x:d.source.x,y:treeHeight - d.source.y};
var end = {x:d.target.x,y:treeHeight - d.target.y};
return Bézier_curve_generator({source:start,target:end});
})
.attr("fill","none")
.attr("stroke","yellow")
.attr("stroke-width",1);
//繪制節(jié)點和文字
//老規(guī)矩潮饱,先創(chuàng)建用以繪制每個節(jié)點和對應文字的分組<g>
var gs = g.append("g")
.selectAll("g")
.data(nodes)
.enter()
.append("g")
.attr("transform",function(d){
var cx = treeHeight - d.y; // 水平:d.x 垂直:d.y
var cy= d.x; // 水平:d.y 垂直:d.x
return "translate(" + cy + "," + cx + ")";
});
//繪制節(jié)點
gs.append("circle")
.attr("r",6)
.attr("fill","white")
.attr("stroke","blue")
.attr("stroke-width",1);
//文字
gs.append("text")
.attr("x", function(d){
return d.children ? -15 : 0;
})
.attr("y", function(d) {
return d.children ? 20 : -20 * d.data.name.length - 5;
})
.attr("dy",0)
.text(function(d){
return d.data.name;
})
.style('writing-mode', function(d) {
return d.children ? 'horizontal-tb' : 'tb-rl';
});
</script>
</body>
</html>
- 縱向2
<!DOCTYPE html>
<html>
<head>
<title>垂直直線樹(上)</title>
<script type="text/javascript" src="http://d3js.org/d3.v5.min.js">
</script>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=GBK">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<svg width="1280" height="600"></svg>
<script>
//定義邊界
var marge = {top:50, bottom:0, left:120, right:40};
var svg = d3.select("svg");
var width = svg.attr("width");
var height = svg.attr("height");
var g = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
var scale = svg.append("g")
.attr("transform","translate("+marge.top+","+marge.left+")");
//數(shù)據(jù)
var dataset = {
name:"中國",
children:[
{
name:"浙江",
children:[
{name:"杭州" ,value:100},
{name:"寧波",value:100},
{name:"溫州",value:100},
{name:"紹興",value:100}
]
},
{
name:"廣西",
children:[
{
name:"桂林",
children:[
{name:"秀峰區(qū)",value:100},
{name:"疊彩區(qū)",value:100},
{name:"象山區(qū)",value:100},
{name:"七星區(qū)",value:100}
]
},
{name:"南寧",value:100},
{name:"柳州",value:100},
{name:"防城港",value:100}
]
},
{
name:"黑龍江",
children:[
{name:"哈爾濱",value:100},
{name:"齊齊哈爾",value:100},
{name:"牡丹江",value:100},
{name:"大慶",value:100}
]
},
{
name:"新疆" ,
children:
[
{name:"烏魯木齊"},
{name:"克拉瑪依"},
{name:"吐魯番"},
{name:"哈密"}
]
}
]
};
//創(chuàng)建一個hierarchy layout
var hierarchyData = d3.hierarchy(dataset)
.sum(function(d){
return d.value;
});
console.log(hierarchyData);
//創(chuàng)建一個樹狀圖
var treeHeight = height-200; //*******記錄一下樹的高度来氧,上反轉(zhuǎn)使用
var tree = d3.tree()
.size([width-80,treeHeight])
.separation(function(a,b){
return a.parent == b.parent ? 1 : 2;
})
//初始化樹狀圖,也就是傳入數(shù)據(jù),并得到繪制樹基本數(shù)據(jù)
var treeData = tree(hierarchyData);
console.log(treeData);
//得到節(jié)點
var nodes = treeData.descendants();
var links = treeData.links();
//輸出節(jié)點和邊
console.log(nodes);
console.log(links);
//創(chuàng)建一個貝塞爾生成曲線生成器 // 水平:d3.linkHorizontal() 垂直:d3.linkVertical()
var Bézier_curve_generator = d3.linkVertical()
.x(function(d) { return d.x; }) // 水平:d.y 垂直:d.x
.y(function(d) { return d.y; });// 水平:d.x 垂直:d.y
//有了節(jié)點和邊集的數(shù)據(jù)后香拉,我們就可以開始繪制了啦扬,
//繪制邊
var boxWidth = 66, boxHeight = 40;
g.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("d",function(d){
let sourceX = d.source.x,
sourceY = treeHeight - d.source.y + boxHeight,
targetX = d.target.x,
targetY = treeHeight - d.target.y;
return "M" + sourceX + "," + sourceY +
"V" + ((targetY - sourceY) / 2 + sourceY) +
"H" + targetX +
"V" + targetY;
/*
var start = {x:d.source.x,y:d.source.y};
var end = {x:d.target.x,y:d.target.y};
return Bézier_curve_generator({source:start,target:end});
*/
})
.attr("fill","none")
.attr("stroke","red")
.attr("stroke-width",1);
//繪制節(jié)點和文字
//老規(guī)矩,先創(chuàng)建用以繪制每個節(jié)點和對應文字的分組<g>
var gs = g.append("g")
.selectAll("g")
.data(nodes)
.enter()
.append("g")
.attr("transform",function(d){
var cx = treeHeight - d.y; // 水平:d.x 垂直:d.y
var cy= d.x; // 水平:d.y 垂直:d.x
return "translate(" + cy + "," + cx + ")";
});
//繪制節(jié)點
/*
gs.append("circle")
.attr("r",6)
.attr("fill","white")
.attr("stroke","blue")
.attr("stroke-width",1);
*/
gs.append("rect")
.attr('y', 0)
.attr('x', function (d) {
return d.depth < 2 ? -(boxWidth / 2) : -(boxHeight / 2)
})
.attr('width', function (d) {
return d.depth < 2 ? boxWidth : boxHeight;
})
.attr('height', function (d) {
return d.depth < 2 ? boxHeight : boxWidth;
})
// 矩形背景色以及邊框顏色寬度
.attr('fill', '#fff')
.attr('stroke', 'steelblue')
.attr('strokeWidth', '1px')
.on('click', function (evt) {
console.log(evt); // 顯示所點擊節(jié)點數(shù)據(jù)
});
//文字
gs.append("text")
.attr('y', function (d) {
return d.depth < 2 ? boxHeight / 2 + 5 : 5;
})
.attr('style', function (d) {
return d.depth < 2 ? '' : "writing-mode: tb;letter-spacing:0px";
})
.attr("text-anchor", function (d) {
return d.depth < 2 ? 'middle' : "start";
})
.text(function(d){
return d.data.name;
});
</script>
</body>
</html>