-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiEntity.html
300 lines (293 loc) · 10.8 KB
/
multiEntity.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link href="http://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script src="static/js/d3.min.js" type="text/javascript"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
padding: 0;
font-weight: 500;
font-family: "Microsoft YaHei","宋体","Segoe UI", "Lucida Grande", Helvetica, Arial,sans-serif, FreeSans, Arimo;
}
#container {
width: 100%;
height: 500px;
margin: 0 auto;
}
.tooltip{
position: absolute;
width: 150;
height: auto;
font-family:simsun;
font-size: 15px;
padding:3px;
text-align:center;
border-style: solid;
border-width: 1px;
background-color:#F5F5F5;
border-radius: 5px;
text-align:left;
}
div.search {padding: 5px 0;}
div.form {
position: relative;
width: 500px;
margin: 0 auto;
padding-top:10px;
}
.i2{
margin:10px auto;
position: relative;
}
.i1{
margin:10px auto;
position: relative;
}
div.btn{
position:relative;
width:500px;
margin:0 auto;
}
input, button {
border: none;
outline: none;
}
input {
width: 100%;
height: 42px;
padding-left: 13px;
}
button {
height: 42px;
width: 100px;
cursor: pointer;
position: absolute;
margin:10px;
}
.bar1 input {
border: 2px solid #7BA7AB;
border-radius: 5px;
background: #F0F8FF;
color: #9E9C9C;
}
.bar1 button {
top: 0;
right: 0;
background: #7BA7AB;
border-radius: 5px 5px 5px 5px;
}
.bar1 button:before {
content: "\f002";
font-family: FontAwesome;
font-size: 16px;
color: #F9F0DA;
}
#visual{
position:absolute;width:100%;height:800px;margin:auto;top:220px;left:0;right:0;
}
</style>
<script>
function idIndex(a,id)
{
for (var i=0;i<a.length;i++)
{
if (a[i].id == id) return i;
}
return null;
}
var cnt=0;
function MyD3(nodes,links) {
if(cnt!=0){d3.select('svg').remove();}
cnt++;
var width = document.body.scrollWidth, height = 800;
var svg=d3.select("#visual").append("svg").attr("width",width).attr("height",height);
var circleRad=new Array();
var zoom = d3.behavior.zoom().scaleExtent([0.4, 2]).on("zoom", zoomed);
var tooltip = d3.select("body").append("div").attr("class","tooltip").style("opacity",0.0);
var force = d3.layout.force().nodes(d3.values(nodes)).links(links).size([width, height]).linkDistance(250).charge(-800).on("tick", tick).start();
svg.call(zoom).on("dblclick.zoom", null);
var edges_line = svg.append("g").selectAll(".edgepath").data(force.links()).enter().append("path")
.attr({'d': function(d) {return 'M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y},'class':'edgepath','id':function(d,i) {return 'edgepath'+i;}})
.style("stroke",function(d){var lineColor;lineColor="#9370DB";return lineColor;}).style("pointer-events", "none").style("stroke-width",1);
var edges_text = svg.append("g").selectAll(".edgelabel").data(force.links()).enter().append("text").style("pointer-events", "none")
.attr({ 'class':'edgelabel','id':function(d,i){return 'edgepath'+i;},'dx':80,'dy':0}).style("font-size",12);
edges_text.append('textPath').attr('xlink:href',function(d,i) {return '#edgepath'+i}).style("pointer-events", "none").text(function(d){return d.type;});
var circle = svg.append("g").selectAll("circle").data(force.nodes()).enter().append("circle")
.style("fill",function(node){var color;color="#00BFFF";return color;})
.style('stroke',function(node){var color;color="#000080";return color;}).style("stroke-width",1).attr("r", function(node,i){
var r;
r = 20+set_r(node.id);
circleRad[node.id]=r;
return r;
})
.call(drag()).on("mouseover",function(d){var str="";
for(key in d){ if(key=="label"){str+=key+":"+d[key]+"<br/>";}else{
if(/^[\u4e00-\u9fa5]/.test(key)){str+=key+":"+d[key]+"<br/>";}
}}
tooltip.html(str).style("left", (d3.event.pageX) + "px").style("top", (d3.event.pageY + 20) + "px").style("top", (d3.event.pageY + 20) + "px").style("opacity",1.0);})
.on("mouseout",function(d){tooltip.style("opacity",0.0);})
.on("mousedown",function(){d3.select(this).attr("r",function(node){return circleRad[node.id]+2;})})
.on("mouseup",function(){d3.select(this).attr("r",function(node){return circleRad[node.id];})})
.on("dblclick.zoom", null)
.on("click",function(node){
edges_line.style("stroke-width",function(line){
if(line.source.id==node.id || line.target.id==node.id){//当与连接点连接时变粗
return 3;
}else{
return 1;
}
});
circle.style('stroke-width',1);
circle.style('stroke-width',function(d){
if(d.id!=node.id&&isOne(d.id,node.id)){
return 4;
}
});
d3.select(this).style('stroke-width',4);
});
var text = svg.append("g").selectAll("text").data(force.nodes()).enter().append("text").attr("dy", ".35em").attr("text-anchor", "middle")
.style('fill',function(node){var color; color="#F5F5F5";return color;}).attr('x',function(d){
name=d.name;
name=name.toString();
if(name.length<=4){d3.select(this).append('tspan').attr('x',0).attr('y',2).text(function(){return name;});
}else{ var top=name.substring(0,4);var bot=name.substring(4,name.length);d3.select(this).text(function(){return '';});
d3.select(this).append('tspan').attr('x',0).attr('y',-7).text(function(){return top;});
d3.select(this).append('tspan').attr('x',0).attr('y',10).text(function(){return bot;});}}).style("font-size",13)
.on("mouseover",function(d){var str="";
for(key in d){ if(key=="label"){str+=key+":"+d[key]+"<br/>";}else{
if(/^[\u4e00-\u9fa5]/.test(key)){str+=key+":"+d[key]+"<br/>";}
}}tooltip.html(str).style("left", (d3.event.pageX) + "px").style("top", (d3.event.pageY + 20) + "px").style("top", (d3.event.pageY + 20) + "px").style("opacity",1.0);})
.on("mouseout",function(d){tooltip.style("opacity",0.0);}).call(drag())
.on("mousedown",function(){d3.select(this).attr("r",function(node){return circleRad[node.id]+2;})})
.on("mouseup",function(){d3.select(this).attr("r",function(node){return circleRad[node.id];})})
.on("dblclick.zoom", null)
.on("click",function(node){
edges_line.style("stroke-width",function(line){
if(line.source.id==node.id || line.target.id==node.id){//当与连接点连接时变粗
return 3;
}else{
return 1;
}
});
circle.style('stroke-width',1);
circle.style('stroke-width',function(d){
if(d.id!=node.id&&isOne(d.id,node.id)){
return 4;
}
});
d3.select(this).style('stroke-width',4);
});
function zoomed(){
svg.selectAll("g").attr("transform",//svg下的g标签移动大小
"translate(" +d3.event.translate + ")scale(" +d3.event.scale + ")");
}
function drag(){//拖拽函数
return force.drag()
.on("dragstart",function(d){
d3.event.sourceEvent.stopPropagation(); //取消默认事件
d.fixed = true; //拖拽开始后设定被拖拽对象为固定
});
}
function tick(){
circle.attr("transform", transform1);text.attr("transform", transform2);
edges_line.attr('d', function(d) { var path='M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y;return path;});
edges_text.attr('transform',function(d,i){if (d.target.x<d.source.x){bbox = this.getBBox();rx = bbox.x+bbox.width/2;ry = bbox.y+bbox.height/2;
return 'rotate(180 '+rx+' '+ry+')';}else {return 'rotate(0)';}});}
function zoomed(){svg.selectAll("g").attr("transform","translate(" +d3.event.translate + ")scale(" +d3.event.scale + ")");}
function set_r(id){
var r=0;
for(var i=0;i<links.length;i++){
if(id == links[i].source.id || id == links[i].target.id){
r++;
}
}
return r;
}
function isOne(num1,num2){
for(var i=0;i<links.length;i++){
if((num1==links[i].source.id&&num2==links[i].target.id)||(num2==links[i].source.id&&num1==links[i].target.id)){
return true;
}
}
return false;
}
}
function transform1(d) {return "translate(" + d.x + "," + d.y + ")";}
function transform2(d) {return "translate(" + (d.x) + "," + d.y + ")";}
function isObjectValueEqual(a, b) {
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
}
function isInArray(arr,value){
for(var i = 0; i < arr.length; i++){
if(isObjectValueEqual(value,arr[i])){
return true;
}
}
return false;
}
function mysend(){
var name1=document.getElementById("i1").value;
var name2=document.getElementById("i2").value;
var nodes=[], links=[];
$(document).ready(function() {
$.ajax({
url : "http://localhost:8080/Salarymanage/MultiEntity",
dataType : "json",
type : "post",
async : true,
data:{"name1":name1,"name2":name2},
success : function(str) {
var count=0;
str.results[0].data.forEach(function (row) {
row.graph.nodes.forEach(function (n) {
var obj=new Object();
obj["id"]=n.id;
obj["label"]=n.labels[0];
for (key in n.properties){
obj[key]=n.properties[key];
}
if(!isInArray(nodes,obj)){
nodes.push(obj);
}
});
links = links.concat( row.graph.relationships.map(function(r) {
return {source:idIndex(nodes,r.startNode),target:idIndex(nodes,r.endNode),type:r.type,relatype:r.relationtype};
}));
});
MyD3(nodes,links);
}
})
})
}
</script>
</head>
<body>
<div id="container">
<div class="search bar1">
<div class="form">
<input type="text" id="i2" class="i1" placeholder="实体1">
<input type="text" id="i1" class="i2" placeholder="实体2">
</div>
<div class="btn">
<button type="button" onclick="mysend()" ></button>
</div>
<div id="visual"></div>
</div>
</body>