D3 v5.16做了个关系图,可折叠展开、拖动固定
文章目录
用D3 v5.16做了个关系图,
可以平移缩放,
可折叠展开,双击展开,
可以拖动固定,
简单粗糙了点。
最开始是用echart做的,但是单击之后展开的那个扇形没法在echart中画,最后只能用d3去画。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../libs/jquery1.8.3.js"></script>
<script src="../libs/d3/d3-5.16.0.js"></script>
<script src="../libs/lodash3.9.0.js"></script>
<script src="./data.js"></script>
<title>Document</title>
<style>
svg {
position: relative;
font-size: 12px;
}
text,
tspan {
font-size: 12px;
}
.svg-thbincrease {
fill: #E53228
}
.svg-thbdecrease {
fill: #45DB45
}
.selected {
stroke-width: 2px;
stroke: #F9F9F9;
}
#expand .a,
#remove .a {
fill: none;
stroke: currentColor;
stroke-linecap: round;
stroke-linejoin: round;
stroke-width: 1.5px;
}
.node-circle {
stroke-width: 2px;
}
.node-circle:hover {
stroke-width: 5px;
}
.node-text {
font-size: 12px;
font-weight: bold;
fill: #656363;
}
.line-text text {
font-size: 12px;
text-anchor: middle;
opacity: 0;
transition: all 0.1s;
cursor: default;
}
.line-text text:hover {
opacity: 1;
}
.lines {
stroke: #555;
stroke-width: 1px;
}
.svg-tip {
box-shadow: 0px 0px 10px #999;
padding: 10px;
position: absolute;
background-color: #fff;
display: none;
z-index: 119891020;
}
.tip-content {
width: 250px;
min-height: 50px;
font-size: 12px;
color: #6b828f;
}
.tipcircle {
display: inline-block;
width: 10px;
height: 10px;
background-color: #8DB6FF;
border-radius: 50% 50%;
margin-right: 10px;
}
.tipitem {
margin: 10px 10px;
clear: both;
}
.svgtypes {
position: absolute;
z-index: 2;
top: 5px;
left: 5px;
font-size: 12px;
height: 25px;
line-height: 14px;
display: none;
background-color: #c1e5f8;
border-radius: 10px;
padding: 0px 10px;
}
.svgtypes td {
height: 25px;
vertical-align: middle;
width: 100px;
font-size: 14px;
}
.svgtypes .first-index>div,
.svgtypes .index>div,
.svgtypes .org>div {
width: 30px;
height: 14px;
border-radius: 5px;
vertical-align: middle;
display: inline-block;
margin-right: 2px;
}
.svgtypes .first-index>div {
background-color: #036DFF
}
.svgtypes .index>div {
background-color: #27B8A9
}
.svgtypes .org>div {
background-color: #FF6E05
}
.svgbuttons {
position: absolute;
z-index: 2;
right: 0px;
top: 0px;
height: 25px;
line-height: 14px;
background-color: #c1e5f8;
border-radius: 20px;
height: 175px;
padding: 10px 0px;
}
.svgbuttons div {
width: 80px;
height: 20px;
line-height: 20px;
border-radius: 5px;
border: #6b828f solid 1px;
margin: 5px 10px;
text-align: center;
cursor: pointer;
background-color: #8DB6FF;
color: #fff;
font-size: 14px;
}
#chart {
position: relative;
}
.more,
.less {
fill: #adb3bb;
opacity: 0.8;
transition: opacity 0.1s;
}
.more:hover,
.less:hover {
opacity: 1;
}
.line-path {
stroke-width: 1.5px;
}
text {
fill: rgb(107, 105, 105);
cursor: default;
}
#svg-container text {
font-size: 12px;
cursor: default;
}
.bar-svg {
z-index: 0;
}
.grap-box {
position: absolute;
width: 1000px;
height: 600px;
box-shadow: 0px 0px 5px #999;
}
</style>
<script>
var grap = {
"kg": {
"nodes": [
{
"parent": [],
"node_data": {
"name": "规模报表指标",
"label": "Indicator",
"index_id": "JHCW_100"
},
"node_id": "JHCW_100",
"child": [
"JHCW_100001",
"JHCW_100002",
"JHCW_100003"
]
},
{
"parent": [
"JHCW_100"
],
"node_data": {
"buss_desc": "\"(营业收入-营业支出+营业外收入-营业外支出-总行列支应由分行分摊的支出-应减除的处置非生息资产净收益-超支的招待费-所得税)-(考核期加权风险资产×10.5%*10%)JHCW_100002 - JHCW_100003 + JHCW_100005 - JHCW_100006 - JHCW_100007 - JHCW_100087 - JHCW_100009 - JHCW_100015 * 0.105 * 0.1\"",
"name": "经济利润",
"data_type": "金额(单位:元)",
"tech_desc": "JHCW_100002 - JHCW_100003 + JHCW_100005 - JHCW_100006 - JHCW_100007 - JHCW_100087 - JHCW_100009 - JHCW_100015 * 0.105 * 0.1",
"label": "Indicator",
"index_id": "JHCW_100001"
},
"node_id": "JHCW_100001",
"child": [
"JHCW_100015",
"JHCW_100087",
"JHCW_100006",
"JHCW_100005",
"JHCW_100002",
"JHCW_100003",
"JHCW_100009",
"JHCW_100007"
]
},
{
"parent": [
"JHCW_100",
"JHCW_100001"
],
"node_data": {
"buss_desc": "\"分行利润表\" 营业收入",
"name": "营业收入",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100002"
},
"node_id": "JHCW_100002",
"child": []
},
{
"parent": [
"JHCW_100",
"JHCW_100001"
],
"node_data": {
"buss_desc": "\"分行利润表\" 营业支出",
"name": "营业支出",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100003"
},
"node_id": "JHCW_100003",
"child": []
},
{
"parent": [
"JHCW_100001"
],
"node_data": {
"buss_desc": "JHCW_100016 + JHCW_100017 + JHCW_100018 + JHCW_100019+JHCW_100020 + JHCW_100021 + JHCW_100022",
"name": "调整后风险加权资产",
"data_type": "金额(单位:元)",
"tech_desc": "JHCW_100016 + JHCW_100017 + JHCW_100018 + JHCW_100019+JHCW_100020 + JHCW_100021 + JHCW_100022",
"label": "Indicator",
"index_id": "JHCW_100015"
},
"node_id": "JHCW_100015",
"child": [
"JHCW_100022",
"JHCW_100020",
"JHCW_100016",
"JHCW_100017",
"JHCW_100021",
"JHCW_100018",
"JHCW_100019"
]
},
{
"parent": [
"JHCW_100001"
],
"node_data": {
"buss_desc": "(正数为超支)\"实际列支招待费-可列支招待费额度=JHCW_100083 - JHCW_100086\"",
"name": "超支招待费",
"data_type": "金额(单位:元)",
"tech_desc": "JHCW_100083 - JHCW_100086",
"label": "Indicator",
"index_id": "JHCW_100087"
},
"node_id": "JHCW_100087",
"child": [
"JHCW_100086",
"JHCW_100083"
]
},
{
"parent": [
"JHCW_100001"
],
"node_data": {
"buss_desc": "\"分行损益表\" 科目6711 营业外支出或分行利润表",
"name": "营业外支出",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100006"
},
"node_id": "JHCW_100006",
"child": []
},
{
"parent": [
"JHCW_100001"
],
"node_data": {
"buss_desc": "\"分行损益表\" 科目6301 营业外支出或分行利润表",
"name": "营业外收入",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100005"
},
"node_id": "JHCW_100005",
"child": []
},
{
"parent": [
"JHCW_100001"
],
"node_data": {
"buss_desc": null,
"name": "所得税(分配)",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100009"
},
"node_id": "JHCW_100009",
"child": []
},
{
"parent": [
"JHCW_100001"
],
"node_data": {
"buss_desc": "\"以下指标合计JHCW_100135 无形资产摊销JHCW_100061 网络通讯费JHCW_100062 结算手续费支出JHCW_100063 代理手续费支出JHCW_100064 信用卡手续及佣金支出JHCW_100065 电子设备运转维护费JHCW_100066 存款保险费JHCW_100067 其他费用\"",
"name": "总行列支应由分行分摊的支出",
"data_type": "金额(单位:元)",
"tech_desc": "JHCW_100135 + JHCW_100061 + JHCW_100062 + JHCW_100063 + JHCW_100064 + JHCW_100065 + JHCW_100066 + JHCW_100067",
"label": "Indicator",
"index_id": "JHCW_100007"
},
"node_id": "JHCW_100007",
"child": [
"JHCW_100061",
"JHCW_100066",
"JHCW_100135",
"JHCW_100067",
"JHCW_100064",
"JHCW_100065",
"JHCW_100062",
"JHCW_100063"
]
},
{
"parent": [
"JHCW_100015"
],
"node_data": {
"buss_desc": "前端补录\"交叉销售明细表\",按主办行汇总FTP净利差为减少",
"name": "交叉销售风险资产",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100022"
},
"node_id": "JHCW_100022",
"child": []
},
{
"parent": [
"JHCW_100015"
],
"node_data": {
"buss_desc": "前端补录\"投行业务投资收益分",
"name": "11111",
"data_type": "111111",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100020"
},
"node_id": "JHCW_100020",
"child": []
},
{
"parent": [
"JHCW_100015"
],
"node_data": {
"buss_desc": "111",
"name": "1",
"data_type": "1111",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100016"
},
"node_id": "JHCW_100016",
"child": []
},
{
"parent": [
"JHCW_100015"
],
"node_data": {
"buss_desc": "1111",
"name": "1111",
"data_type": "1",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100017"
},
"node_id": "JHCW_100017",
"child": []
},
{
"parent": [
"JHCW_100015"
],
"node_data": {
"buss_desc": "11111",
"name": "1111",
"data_type": "11111",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100021"
},
"node_id": "JHCW_100021",
"child": []
},
{
"parent": [
"JHCW_100015"
],
"node_data": {
"buss_desc": "1111",
"name": "1111",
"data_type": "11111",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100018"
},
"node_id": "JHCW_100018",
"child": []
},
{
"parent": [
"JHCW_100015"
],
"node_data": {
"buss_desc": "数据补录",
"name": "表外风险资产调整项",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100019"
},
"node_id": "JHCW_100019",
"child": []
},
{
"parent": [
"JHCW_100087"
],
"node_data": {
"buss_desc": "\"扣除总行落地分行业务后的营业收入总额 * 5/1000JHCW_100074 * 5 /1000\"",
"name": "可列支招待费额度",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100086"
},
"node_id": "JHCW_100086",
"child": []
},
{
"parent": [
"JHCW_100087"
],
"node_data": {
"buss_desc": "补录\"分行损益明细表\" 66010623",
"name": "实际列支招待费",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100083"
},
"node_id": "JHCW_100083",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "总行\"损益明细表\" 6601060402",
"name": "网络通讯费",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100061"
},
"node_id": "JHCW_100061",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "由计财补录总行数据",
"name": "存款保险费",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100066"
},
"node_id": "JHCW_100066",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "总行\"损益明细表\" 66010619",
"name": "无形资产摊销",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100135"
},
"node_id": "JHCW_100135",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "由计财补录总行数据",
"name": "其他费用",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100067"
},
"node_id": "JHCW_100067",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "总行\"损益明细表\" 642112",
"name": "信用卡手续及佣金支出",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100064"
},
"node_id": "JHCW_100064",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "总行\"损益明细表\" 66010601",
"name": "电子设备运转维护费",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100065"
},
"node_id": "JHCW_100065",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "总行\"损益明细表\" 642109",
"name": "结算手续费支出",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100062"
},
"node_id": "JHCW_100062",
"child": []
},
{
"parent": [
"JHCW_100007"
],
"node_data": {
"buss_desc": "总行\"损益明细表\" 642110",
"name": "代理手续费支出",
"data_type": "金额(单位:元)",
"tech_desc": null,
"label": "Indicator",
"index_id": "JHCW_100063"
},
"node_id": "JHCW_100063",
"child": []
}
],
"links": [
{
"end_node": "JHCW_100001",
"start_node": "JHCW_100",
"rel": "包含"
},
{
"end_node": "JHCW_100002",
"start_node": "JHCW_100",
"rel": "包含"
},
{
"end_node": "JHCW_100003",
"start_node": "JHCW_100",
"rel": "包含"
},
{
"end_node": "JHCW_100015",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100087",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100006",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100005",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100002",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100003",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100009",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100007",
"start_node": "JHCW_100001",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100022",
"start_node": "JHCW_100015",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100020",
"start_node": "JHCW_100015",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100016",
"start_node": "JHCW_100015",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100017",
"start_node": "JHCW_100015",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100021",
"start_node": "JHCW_100015",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100018",
"start_node": "JHCW_100015",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100019",
"start_node": "JHCW_100015",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100086",
"start_node": "JHCW_100087",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100083",
"start_node": "JHCW_100087",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100061",
"start_node": "JHCW_100007",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100066",
"start_node": "JHCW_100007",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100135",
"start_node": "JHCW_100007",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100067",
"start_node": "JHCW_100007",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100064",
"start_node": "JHCW_100007",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100065",
"start_node": "JHCW_100007",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100062",
"start_node": "JHCW_100007",
"rel": "计算依赖"
},
{
"end_node": "JHCW_100063",
"start_node": "JHCW_100007",
"rel": "计算依赖"
}
]
},
"indicators": [
"经济利润",
"营业收入",
"营业支出"
]
}
</script>
</head>
<body>
<div class="grap-box svg-container full-container">
<table class="svgbuttons svggraph">
<tr>
<td class="show">
<div>显示全部</div>
</td>
</tr>
<tr>
<td class="hide">
<div>收缩全部</div>
</td>
</tr>
<tr>
<td class="next">
<div>扩展</div>
</td>
</tr>
<tr>
<td class="order">
<div>整理</div>
</td>
</tr>
<tr>
<td class="fixed">
<div>固定</div>
</td>
</tr>
</table>
<svg class="fullMap" id="fullMap" width="100%" height="100%">
<defs>
<g id="expand" class="expand_node" transform="translate(0,0) scale(0.7)" color="#FFFFFF">
<circle class="a" cx="13.5" cy="10.498" r="3.75"></circle>
<circle class="a" cx="21" cy="2.998" r="2.25"></circle>
<circle class="a" cx="21" cy="15.748" r="2.25"></circle>
<circle class="a" cx="13.5" cy="20.998" r="2.25"></circle>
<circle class="a" cx="3" cy="20.998" r="2.25"></circle>
<circle class="a" cx="3.75" cy="5.248" r="2.25"></circle>
<line class="a" x1="16.151" y1="7.848" x2="19.411" y2="4.588"></line>
<line class="a" x1="16.794" y1="12.292" x2="19.079" y2="14.577"></line>
<line class="a" x1="13.5" y1="14.248" x2="13.5" y2="18.748"></line>
<line class="a" x1="10.851" y1="13.147" x2="4.59" y2="19.408"></line>
<line class="a" x1="10.001" y1="9.149" x2="5.61" y2="6.514"></line>
</g>
<g id="remove" class="remove_node" transform="translate(0,0) scale(0.7)" color="#FFFFFF">
<path class="a"
d="M8.153,13.664a12.271,12.271,0,0,1-5.936-4.15L1.008,7.96a.75.75,0,0,1,0-.92L2.217,5.486A12.268,12.268,0,0,1,11.9.75h0a12.269,12.269,0,0,1,9.684,4.736L22.792,7.04a.748.748,0,0,1,0,.92L21.584,9.514">
</path>
<path class="a" d="M10.4,10.937A3.749,3.749,0,1,1,15.338,9"></path>
<circle class="a" cx="17.15" cy="17.25" r="6"></circle>
<line class="a" x1="14.15" y1="17.25" x2="20.15" y2="17.25"></line>
</g>
</defs>
</svg>
</div>
</body>
<script>
var R = 15;
var S = 2;
var W = 25;
var L = 150;
var TypeR = 10;
var TypeColor = {
'firstIndex': {
color: '#036DFF',
label: '查询指标'
},
'Indicator': {
color: '#27B8A9',
label: '相关指标'
},
'Org': {
color: '#FF6E05',
label: '机构'
},
'Operator': {
color: '#FF6E05',
label: '查询指标'
},
'other': {
color: '#E03A82',
label: '其他'
},
'nope': {
color: '#FBEDCC',
label: '备用'
}
};
var lineColor = {
'包含': '#914CE5',
'下级是': '#2BE760',
'计算依赖': '#FF3976',
'其他': '#7289AB'
};
function Grap(option) {
this.svg = d3.select(option.svg)
this.svgId = option.svgId;
this.originNodes = $.extend(true, [], option.originNodes);
this.originLinks = $.extend(true, [], option.originLinks);
this.indicators = $.extend(true, [], option.indicators);
this.forceSimulation = null;
this.nodesData = [];
this.linksData = [];
this.nodes = [];
this.links = [];
this.linksText = [];
this.selectNode = null;
this.showNodeIds = [];
this.showLinksIds = [];
this.dragging = false;
this.allIds = [];
this.roots = [];
this.clickTime = null;
}
Grap.prototype.init = function () {
var that = this;
var roots = this.roots;
var indicators = this.indicators;
$.each(this.originNodes, function (i, v) {
v.expand = false;
v.rel = [];
v.id = v.node_id;
that.allIds.push(v.id);
if (indicators.indexOf(v.node_id) !== -1) {
roots.push(v);
v.node_data.label = 'firstIndex'
}
if (v.node_data.label === 'Org') {
roots.push(v);
}
v.rel = v.rel.concat(v.child, v.parent);
});
console.log(roots)
$.each(roots, function (i, v) {
var d = findAllRelIds(v)
that.showNodeIds = that.showNodeIds.concat(d);
})
$.each(roots, function (i, v) {
that.showNodeIds.push(v.node_id);
})
that.showNodeIds = _.uniq(that.showNodeIds);
that.nodesData = findNodesByIdAry(that.showNodeIds, this.originNodes);
$.each(this.originLinks, function (i, v) {
v.source = v.start_node;
v.target = v.end_node;
v.id = v.start_node + '-' + v.end_node;
});
this.linksData = this.originLinks.filter(function (e) {
if (that.showNodeIds.indexOf(e.start_node) !== -1 && that.showNodeIds.indexOf(e.end_node) !== -1) {
return true
} else {
return false
}
})
console.log(this.originNodes)
if (this.svg.select('g.box').size()) {
this.svg.select('g.box').remove();
}
this.svg.append('g').attr('class', 'box');
this.svg
.call(
d3.zoom()
.scaleExtent([0.1, 4])
.on("zoom", function () {
var transform = d3.event.transform;
that.svg.select("g.box")
.attr("transform", "translate("
+ transform.x + "," + transform.y
+ ")scale(" + transform.k + ")");
})
)
.on('dblclick.zoom', null);
this.createLinks();
this.createNodes();
this.simulation();
this.createButtons();
}
Grap.prototype.createButtons = function () {
$('body').off('click ', '.svgbuttons .show')
.on('click ', '.svgbuttons .show', this.showAll.bind(this));
$('body').off('click ', '.svgbuttons .hide')
.on('click ', '.svgbuttons .hide', this.hide.bind(this));
$('body').off('click ', '.svgbuttons .next')
.on('click ', '.svgbuttons .next', this.next.bind(this));
$('body').off('click ', '.svgbuttons .order')
.on('click ', '.svgbuttons .order', this.order.bind(this));
$('body').off('click ', '.svgbuttons .fixed')
.on('click ', '.svgbuttons .fixed', this.fixed.bind(this));
$('body').off('.close-dialog').on('close-dialog', this.onClose.bind(this));
}
Grap.prototype.onClose = function () {
this.forceSimulation.stop();
}
function findLinksByNodeIds(ids, originLinks) {
return originLinks.filter(function (link) {
return ids.indexOf(link.start_node) !== -1 && ids.indexOf(link.end_node) !== -1
})
}
Grap.prototype.showAll = function () {
var that = this;
if (this.originNodes.length === this.nodesData.length) {
alert('已经显示全部!')
return;
}
var newNodes = this.originNodes.filter(function (node) {
return that.showNodeIds.indexOf(node.id) === -1
})
var newLinks = this.originLinks.filter(function (link) {
return !(that.showNodeIds.indexOf(link.start_node) !== -1 && that.showNodeIds.indexOf(link.end_node) !== -1)
})
this.showNodeIds = this.allIds;
this.update(newNodes, newLinks);
}
Grap.prototype.hide = function () {
console.log('hide=====', this.roots)
if (this.nodesData.length === 1) {
alert('已经收缩全部!')
return;
}
var ids = [];
var newNodes = []
console.log(this.roots)
this.roots.forEach(function (d) {
ids.push(d.id);
newNodes.push(d);
});
this.showNodeIds = ids;
this.nodesData = newNodes;
this.linksData = findLinksByNodeIds(this.showNodeIds, this.originLinks);
this.removeNodes();
}
Grap.prototype.next = function () {
if (this.originNodes.length === this.nodesData.length) {
alert('已经显示全部!')
return;
}
var allRelIds = [];
this.nodesData.forEach(function (d) {
allRelIds = allRelIds.concat(d.rel);
})
allRelIds = _.uniq(allRelIds);
this.addNodesAndLinks(allRelIds);
}
Grap.prototype.addNodesAndLinks = function (allRelIds) {
var that = this;
var newIds = allRelIds.filter(function (v) {
return that.showNodeIds.indexOf(v) === -1
})
var newNodes = findNodesByIdAry(newIds, that.originNodes);
newNodes = _.uniq(newNodes, 'id')
var newLinks = that.originLinks.filter(function (link) {
return (newIds.indexOf(link.start_node) != -1 && newIds.indexOf(link.end_node) != -1)
|| (that.showNodeIds.indexOf(link.start_node) != -1 && newIds.indexOf(link.end_node) != -1)
|| (that.showNodeIds.indexOf(link.end_node) != -1 && newIds.indexOf(link.start_node) != -1)
})
newLinks = _.uniq(newLinks, 'id')
this.showNodeIds = allRelIds;
this.roots.forEach(function (d) {
that.showNodeIds.push(d.id);
})
this.update(newNodes, newLinks)
}
Grap.prototype.prev = function () {
console.log('prev')
this.nodesData = this.originNodes;
this.linksData = this.originLinks;
this.update();
}
Grap.prototype.order = function () {
console.log('order')
this.nodesData.forEach(function (d) {
if (d.fx || d.fy) {
d.fx = null;
d.fy = null;
}
})
this.forceSimulation.alphaDecay(0.03).alpha(1).restart();
}
Grap.prototype.fixed = function () {
console.log('fixed')
this.nodesData.forEach(function (d) {
d.fx = d.x;
d.fy = d.y;
})
this.forceSimulation.alphaTarget(0.8);
}
Grap.prototype.hasOtherRel = function (d) {
var sum = 0;
this.linksData.forEach(function (e) {
var ids = e.id.split('-');
if (ids[0] === d.id || ids[1] === d.id) {
sum = sum + 1;
}
})
return sum == d.rel.length ? false : true;
}
Grap.prototype.update = function (newNodes, newLinks) {
var that = this;
var clickTime = this.clickTime
newNodes.forEach(function (node) {
that.nodesData.push(node);
})
newLinks.forEach(function (link) {
that.linksData.push(link);
})
that.svg.select('.box').selectAll('.node-circle')
.data(that.nodesData, function (d) {
return d.id;
})
.attr("stroke", function (d, i) {
return TypeColor[d.node_data.label].color;
})
.attr("fill", function (d, i) {
return that.hasOtherRel(d) ? TypeColor[d.node_data.label].color : "#FFF";
})
newNodes.forEach(function (node) {
var enter = that.svg.select('.box').selectAll('.node')
.data([node], function (d) { return d.id })
.enter()
.append("g").attr('class', 'node')
.attr('id', function (d) {
return d.id
})
.attr("transform", function (d, i) {
return "translate(" + Math.random() * L + "," + Math.random() * L + ")";
})
.call(d3.drag()
.on("start", that.started.bind(that))
.on("drag", that.dragged.bind(that))
.on("end", that.ended.bind(that))
);
enter.append("circle")
.attr('class', 'node-circle')
.attr("r", R)
.attr("stroke", function (d, i) {
return TypeColor[d.node_data.label].color;
})
.attr("fill", function (d, i) {
return that.hasOtherRel(d) ? TypeColor[d.node_data.label].color : "#FFF";
})
.on('mouseover', that.showTip.bind(that))
.on('mouseout', that.hideTip.bind(that))
.on("click", function (node) {
console.log('click')
var pnode = (d3.event.target.parentNode)
clearTimeout(clickTime);
clickTime = setTimeout(function () {
that.nodeClick.call(that, node, pnode)
}, 250)
})
.on('dblclick', function (node) {
console.log('dblclick')
clearTimeout(clickTime);
var newIds = findNewIds(node, that.showNodeIds);
if (!newIds.length) {
that.showLess.call(that, node)
} else {
that.showMore.call(that, node)
}
})
enter.append("text")
.attr('class', 'node-text')
.attr("x", R + 5)
.attr("y", 0)
.text(function (d) {
return d.node_data.name;
})
})
newLinks.forEach(function (link) {
that.svg.select('g.line').selectAll('.line-path')
.data([link], function (d) { return d.id })
.enter()
.append("path")
.attr('id', function (d) {
return d.id;
})
.attr('class', 'line-path')
.attr('stroke', function (d) {
return lineColor[d.rel];
})
that.svg.select('g.line-arrow').selectAll('.arrow')
.data([link], function (d) { return d.id })
.enter()
.append("path")
.attr('id', function (d) {
return d.id;
})
.attr('class', 'arrow')
.attr('stroke', function (d) {
return lineColor[d.rel];
})
.attr('fill', function (d) {
return lineColor[d.rel];
})
that.svg.select('g.line-text').selectAll('text')
.data([link], function (d) { return d.id })
.enter()
.append("text")
.text(function (d) {
return d.rel;
})
})
this.forceSimulation.nodes(this.nodesData);
this.forceSimulation.force("link").links(this.linksData);
this.forceSimulation.alphaDecay(0.03).alpha(1).restart();
}
Grap.prototype.simulation = function () {
var w = parseInt(this.svg.style('width'))
var h = parseInt(this.svg.style('height'))
var nodesData = this.nodesData;
var linksData = this.linksData;
var that = this;
this.forceSimulation = d3.forceSimulation(nodesData)
.force("link",
d3.forceLink(linksData)
.distance(L)
.id(function (d) { return d.id })
)
.force("charge", d3.forceManyBody().strength(-250))
.force("center", d3.forceCenter(w / 2, h / 2))
.force('collision', d3.forceCollide().radius(20))
.alphaDecay(0.1)
.on("tick", that.ticked.bind(that))
.on('end', function () {
})
}
Grap.prototype.createLinks = function () {
var linksData = this.linksData;
this.links = this.svg.select('.box').append("g")
.attr('class', 'line')
.selectAll("path")
.data(linksData, function (d) {
return d.id;
})
.enter()
.append("path")
.attr('class', 'line-path')
.attr('stroke', function (d) {
return lineColor[d.rel];
})
.attr('id', function (d) {
return d.id;
})
this.linksArrow = this.svg.select('.box').append("g")
.attr('class', 'line-arrow')
.selectAll("path")
.data(linksData, function (d) {
return d.id;
})
.enter()
.append("path")
.attr('class', 'arrow')
.attr('id', function (d) {
return d.id;
})
.attr('stroke', function (d) {
return lineColor[d.rel];
})
.attr('fill', function (d) {
return lineColor[d.rel];
})
this.linksText = this.svg.select('.box').append("g")
.attr('class', 'line-text')
.selectAll("text")
.data(linksData, function (d) {
return d.id;
})
.enter()
.append("text")
.attr('id', function (d) {
return d.id;
})
.text(function (d) {
return d.rel;
})
}
Grap.prototype.createNodes = function () {
var nodesData = this.nodesData;
var clickTime = this.clickTime
var that = this;
this.nodes = this.svg.select('.box').selectAll('.node')
.data(nodesData, function (d) { return d.id })
.enter()
.append("g").attr('class', 'node')
.attr('id', function (d) {
return d.id
})
.attr("transform", function (d, i) {
return "translate(" + L + "," + L + ")";
})
.call(d3.drag()
.on("start", that.started.bind(that))
.on("drag", that.dragged.bind(that))
.on("end", that.ended.bind(that))
);
this.nodes.append("circle")
.attr('class', 'node-circle')
.attr("r", R)
.attr("stroke", function (d, i) {
return TypeColor[d.node_data.label].color;
})
.attr("fill", function (d, i) {
return that.hasOtherRel(d) ? TypeColor[d.node_data.label].color : "#FFF";
})
.on('mouseover', that.showTip.bind(that))
.on('mouseout', that.hideTip.bind(that))
.on("click", function (node) {
console.log('click')
var pnode = (d3.event.target.parentNode)
clearTimeout(clickTime);
clickTime = setTimeout(function () {
that.nodeClick.call(that, node, pnode)
}, 250)
})
.on('dblclick', function (node) {
console.log('dblclick')
clearTimeout(clickTime);
var newIds = findNewIds(node, that.showNodeIds);
console.log(newIds.length)
if (!newIds.length) {
that.showLess.call(that, node)
} else {
that.showMore.call(that, node)
}
})
this.nodes.append("text")
.attr('class', 'node-text')
.attr("x", R + 5)
.attr("y", 0)
.text(function (d) {
return d.node_data.name;
})
}
Grap.prototype.started = function (d) {
this.hideTip()
if (!d3.event.active) {
this.forceSimulation.alphaDecay(0.01).alpha(1).restart();
}
this.dragging = true;
d.fx = d.x;
d.fy = d.y;
}
Grap.prototype.dragged = function (d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
this.dragging = true;
}
Grap.prototype.ended = function (d) {
if (!d3.event.active) {
this.forceSimulation.stop();
}
d.fx = d3.event.x;
d.fy = d3.event.y;
this.dragging = false;
}
Grap.prototype.ticked = function (event) {
this.svg.select('g.line').selectAll('.line-path')
.attr("d", function (d) {
var tar = direction(d.source, d.target, R + 5)
var ds = 'M ' + tar.begin[0] + ',' + tar.begin[1] + "L" + tar.end[0] + ',' + tar.end[1];
return ds;
})
this.svg.select('g.line-arrow').selectAll('.arrow')
.attr("d", function (d) {
var tar = direction(d.source, d.target, R + 5)
var ds = 'M ' + tar.end[0] + ' ' + tar.end[1]
+ "L" + tar.end[0] + ' ' + (tar.end[1] - 3)
+ "L" + (tar.end[0] + 3) + ' ' + (tar.end[1])
+ "L" + (tar.end[0]) + ' ' + (tar.end[1] + 3)
+ "L" + (tar.end[0]) + ' ' + (tar.end[1])
return ds;
})
.attr("transform", function (d) {
var angle = arrowDirection(d.source, d.target, R + 5).angle * 180 / Math.PI;
var tar = direction(d.source, d.target, R + 5)
return "rotate(" + (angle) + " " + tar.end[0] + " " + tar.end[1] + ")"
});
this.svg.select('g.line-text').selectAll('text')
.attr("x", function (d) {
return (d.source.x + d.target.x) / 2;
})
.attr("y", function (d) {
return (d.source.y + d.target.y) / 2;
})
.attr("transform", function (d) {
var angle = direction(d.source, d.target, R + 5).angle * 180 / Math.PI;
var cx = (d.source.x + d.target.x) / 2;
var cy = (d.source.y + d.target.y) / 2;
return "rotate(" + angle + " " + cx + " " + cy + ")" + " translate(" + 5 + "," + "-5" + ")";
})
this.svg.select('.box').selectAll('.node').attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
Grap.prototype.nodeClick = function (node, pnode) {
var that = this;
var nodesData = that.nodesData;
d3.selectAll('g.selected').remove();
nodesData.forEach(function (e) {
if (e.node_id == node.node_id) {
e.expand = !e.expand;
} else {
e.expand = false;
}
that.selectNode = null;
})
nodesData.forEach(function (e) {
if (e.node_id == node.node_id) {
if (e.expand == true) {
var pobj = d3.select(pnode).append('g').attr('class', 'selected');
that.addSelectedNode(pobj)
that.selectNode = node;
}
}
})
}
Grap.prototype.addSelectedNode = function (pobj) {
var that = this;
pobj.append('path')
.attr('class', 'more')
.attr('d', moreSector(R, S, W))
.on('click', that.showMore.bind(that));
pobj.append('use').attr('xlink:href', '#expand')
.attr('x', 20).attr('y', -8)
.on('click', that.showMore.bind(that));
pobj.append('path')
.attr('class', 'less')
.attr('d', lessSector(R, S, W))
.on('click', that.showLess.bind(that));
pobj.append('use').attr('xlink:href', '#remove')
.attr('x', -38).attr('y', -6)
.on('click', that.showLess.bind(that));
}
Grap.prototype.showMore = function (node) {
hideSelectSector(this)
var that = this;
var newIds = findNewIds(node, this.showNodeIds);
if (newIds.length) {
var allRelIds = this.showNodeIds.concat(newIds);
allRelIds = _.uniq(allRelIds);
this.addNodesAndLinks(allRelIds);
}
}
Grap.prototype.showLess = function (node) {
hideSelectSector(this);
var that = this;
var inter = intersection(node.rel, this.showNodeIds);
var tarNode = [];
inter.forEach(function (e) {
var sum = 0;
that.linksData.forEach(function (link) {
if (link.start_node == e || link.end_node == e) {
sum = sum + 1;
}
})
if (sum == 1) {
tarNode.push(e)
}
})
var tarLink = [];
tarNode.forEach(function (e) {
that.linksData.forEach(function (link) {
if (link.start_node == e || link.end_node == e) {
tarLink.push(link.id)
}
})
})
if (!tarNode.length || !tarLink.length) {
return;
}
this.showNodeIds = this.showNodeIds.filter(function (e) {
return tarNode.indexOf(e) == -1;
})
this.nodesData = this.nodesData.filter(function (e) {
return tarNode.indexOf(e.id) == -1;
})
this.linksData = this.linksData.filter(function (e) {
return tarLink.indexOf(e.id) == -1;
})
that.svg.select('.box').selectAll('.node-circle')
.data(that.nodesData, function (d) {
return d.id;
})
.attr("stroke", function (d, i) {
return TypeColor[d.node_data.label].color;
})
.attr("fill", function (d, i) {
return that.hasOtherRel(d) ? TypeColor[d.node_data.label].color : "#FFF";
})
this.removeNodes();
}
Grap.prototype.removeNodes = function () {
this.svg.select('.box').selectAll('.node')
.data(this.nodesData, function (d) { return d.id })
.exit()
.remove();
this.svg.select('g.line').selectAll('path')
.data(this.linksData, function (d) { return d.id })
.exit()
.remove();
this.svg.select('g.line-arrow').selectAll('path')
.data(this.linksData, function (d) { return d.id })
.exit()
.remove();
this.svg.select('g.line-text').selectAll('text')
.data(this.linksData, function (d) { return d.id })
.exit()
.remove();
}
function findNewIds(node, showNodeIds) {
var tar = [];
$.each(node.rel, function (i, v) {
if (showNodeIds.indexOf(v) == -1) {
tar.push(v)
}
})
return tar;
}
function hideSelectSector(obj) {
d3.selectAll('g.selected').remove();
obj.nodesData.forEach(function (e) {
e.expand = false;
})
obj.selectNode = null;
}
function intersection(A, B) {
var tar = [];
A.forEach(function (a) {
if (B.indexOf(a) != -1) {
tar.push(a)
}
})
return tar;
}
Grap.prototype.showTip = function (d, event) {
if (d.node_data && d.node_data.buss_desc && !this.dragging) {
var contetent = $('.svg-tip .tip-content');
if (d.node_data.tech_desc != '' && d.node_data.tech_desc != null && d.node_data.tech_desc != undefined) {
contetent.append($(' <div class="tipitem"><span class="tipcircle"></span>' + d.node_data.buss_desc + '</div>'))
}
if (d.node_data.data_type != '' && d.node_data.data_type != null && d.node_data.data_type != undefined) {
contetent.append($(' <div class="tipitem"><span class="tipcircle"></span>' + d.node_data.data_type + '</div>'))
}
var x = d3.event.pageX;
var y = d3.event.pageY;
var dh = $('.svg-tip').height();
var rw = document.body.scrollWidth;
var rh = document.body.scrollHeight;
x = (rw - d3.event.pageX < 380) ? rw - 380 : d3.event.pageX;
y = (rh - d3.event.pageY < dh) ? rh - dh : d3.event.pageY;
$('.svg-tip')
.css({ 'left': x + 2 * R + 'px', 'top': y + 2 * R + 'px' })
.show(1);
}
}
Grap.prototype.hideTip = function (d) {
$('.svg-tip').hide();
$('.svg-tip .tip-content').empty();
}
function arrowDirection(source, target, r) {
var x0 = source.x;
var y0 = source.y;
var x1 = target.x;
var y1 = target.y;
var tar0 = [];
var tar1 = [];
var angle = 0;
if (x0 === x1 && y0 < y1) {
tar0 = [x0, y0 - r];
tar1 = [x1, y1 + r];
angle = Math.PI / 2;
}
if (x0 < x1 && y0 === y1) {
tar0 = [x0 + r, y0];
tar1 = [x1 - r, y1];
angle = 0;
}
if (x0 === x1 && y0 > y1) {
tar0 = [x0, y0 + r];
tar1 = [x1, y1 - r];
angle = Math.PI / 2;
}
if (x0 > x1 && y0 === y1) {
tar0 = [x0 - r, y0];
tar1 = [x1 + r, y1];
angle = 0;
}
var tan = Math.atan(Math.abs((y1 - y0) / (x1 - x0)));
var R = Math.sqrt(Math.pow((y1 - y0), 2) + Math.pow((x1 - x0), 2));
var sin = Math.abs((y1 - y0)) / R;
var cos = Math.abs((x1 - x0)) / R;
if (x0 < x1 && y0 > y1) {
tar0 = [x0 + r * cos, y0 - r * sin];
tar1 = [x1 - r * cos, y1 + r * sin];
angle = -tan;
}
if (x0 < x1 && y0 < y1) {
tar0 = [x0 + r * sin, y0 + r * cos];
tar1 = [x1 - r * cos, y1 - r * sin];
angle = tan;
}
if (x0 > x1 && y0 < y1) {
tar0 = [x0 - r * cos, y0 + r * sin];
tar1 = [x1 + r * cos, y1 - r * sin];
angle = Math.PI - tan
}
if (x0 > x1 && y0 > y1) {
tar0 = [x0 - r * cos, y0 - r * sin];
tar1 = [x1 + r * cos, y1 + r * sin];
angle = tan + Math.PI
}
return {
begin: [x0, y0],
end: tar1,
angle: angle,
}
}
function direction(source, target, r) {
var x0 = source.x;
var y0 = source.y;
var x1 = target.x;
var y1 = target.y;
var tar0 = [];
var tar1 = [];
var angle = 0;
if (x0 === x1 && y0 < y1) {
tar0 = [x0, y0 - r];
tar1 = [x1, y1 + r];
angle = -Math.PI / 2;
}
if (x0 < x1 && y0 === y1) {
tar0 = [x0 + r, y0];
tar1 = [x1 - r, y1];
angle = 0;
}
if (x0 === x1 && y0 > y1) {
tar0 = [x0, y0 + r];
tar1 = [x1, y1 - r];
angle = Math.PI / 2;
}
if (x0 > x1 && y0 === y1) {
tar0 = [x0 - r, y0];
tar1 = [x1 + r, y1];
angle = 0;
}
var tan = Math.atan(Math.abs((y1 - y0) / (x1 - x0)));
var R = Math.sqrt(Math.pow((y1 - y0), 2) + Math.pow((x1 - x0), 2));
var sin = Math.abs((y1 - y0)) / R;
var cos = Math.abs((x1 - x0)) / R;
if (x0 < x1 && y0 > y1) {
tar0 = [x0 + r * cos, y0 - r * sin];
tar1 = [x1 - r * cos, y1 + r * sin];
angle = -tan;
}
if (x0 < x1 && y0 < y1) {
tar0 = [x0 + r * sin, y0 + r * cos];
tar1 = [x1 - r * cos, y1 - r * sin];
angle = tan;
}
if (x0 > x1 && y0 < y1) {
tar0 = [x0 - r * cos, y0 + r * sin];
tar1 = [x1 + r * cos, y1 - r * sin];
angle = -tan
}
if (x0 > x1 && y0 > y1) {
tar0 = [x0 - r * cos, y0 - r * sin];
tar1 = [x1 + r * cos, y1 + r * sin];
angle = tan
}
return {
begin: [x0, y0],
end: tar1,
angle: angle,
}
}
function moreSector(r, s, w) {
var Mstr = 'M0 -' + (r + s) + ' ';
var Lstr = 'L0 -' + (r + s + w) + ' ';
var Astr = 'A' + (r + s + w) + ' ' + (r + s + w) + ' 0 0 1 0 ' + (r + s + w) + ' ';
var Lstr1 = 'L0 ' + (r + s) + ' ';
var Astr1 = 'A' + (r + s) + ' ' + (r + s) + ' 0 0 0 0 -' + (r + s) + 'Z';
return Mstr + Lstr + Astr + Lstr1 + Astr1;
}
function lessSector(r, s, w) {
var Mstr = 'M0 -' + (r + s) + ' ';
var Lstr = 'L0 -' + (r + s + w) + ' ';
var Astr = 'A' + (r + s + w) + ' ' + (r + s + w) + ' 0 0 0 0 ' + (r + s + w) + ' ';
var Lstr1 = 'L0 ' + (r + s) + ' ';
var Astr1 = 'A' + (r + s) + ' ' + (r + s) + ' 0 0 1 0 -' + (r + s) + 'Z';
return Mstr + Lstr + Astr + Lstr1 + Astr1;
}
function findRoots(originNodes, indicators) {
var tar = [];
$.each(originNodes, function (i, v) {
$.each(indicators, function (k, val) {
if (val == v.node_id) {
v.label = 'firstIndex'
tar.push(v)
}
})
})
return tar;
}
function findAllRelIds(seed) {
var tar = [];
$.each(seed.rel, function (i, v) {
tar.push(v)
})
return tar;
}
function findNodesByIdAry(ids, originNodes) {
var tar = [];
$.each(ids, function (i, v) {
$.each(originNodes, function (k, val) {
if (v == val.node_id) {
tar.push(val)
}
})
})
return tar;
}
var graphSvg = new Grap({
svg: '#fullMap',
originNodes: grap.kg.nodes,
originLinks: grap.kg.links,
indicators: ['JHCW_100001', 'JHCW_100002', 'JHCW_100003']
})
graphSvg.init();
</script>
</html>
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论