-
Notifications
You must be signed in to change notification settings - Fork 11
/
tables-er-d3.html
582 lines (548 loc) · 16.1 KB
/
tables-er-d3.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript" src="http://d3js.org/d3.v5.min.js"></script>
<script>
let tables = new Array(3).fill(1).map((v1,i1)=>{
return {
tableName:'sda_fdsf'+i1,
id:'table'+i1,
cols:new Array(2).fill(1).map((v2,i2)=>{
return {
id:'table'+i1+'cols'+i2,
itemName:'table'+i1+'colsName'+i2,
}
}),
}
})
let links = [
{
source:'table'+0,
target:'table'+1,
relation:'line1',
sourceIndex:1,
targetIndex:1,
value:1,
},
{
source:'table'+0,
target:'table'+1,
relation:'line1',
sourceIndex:2,
targetIndex:2,
value:1,
},
{
source:'table'+1,
target:'table'+2,
relation:'line1',
sourceIndex:2,
targetIndex:2,
value:1,
},
]
</script>
<style>
.table-foot-btn{
fill:blue;
font-size: 12px;
text-decoration: underline;
cursor: pointer;
dominant-baseline: middle;
/*text-anchor: middle;*/
}
</style>
</head>
<body>
<div>
<button onclick="toImg()">toImg</button>
<button onclick="downloadSVG()">toSvg</button>
</div>
<div id="wrap">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" style="border:1px dashed #000;background-color: #efefef;"></svg>
</div>
<script>
let width = 1200
let height = 800
let svg = d3.select('svg')
.attr('width',width)
.attr('height',height)
// .attr('viewBox',`-${width/2} -${height/2} ${width} ${height}`)
let colsHeight = 30;//字段高度
let tableTitleHeight = 30;//字段高度
let tableFootHeight = 30;//字段高度
let colsWidth = 200;
let offLine = 20;//连线的节点偏移
let btnText = {
leftOpen:'字段展开',
leftClose:'字段收起',
rightOpen:'关系扩展',
rightClose:'关系收起',
}
//样式定义
let lineStyle = {
strokeDefault:'#80848f',
strokeWidthDefault:'2',
strokeLight:'#19be6b',
strokeWidthLight:'3',
}
let colsStyle = {
fillDefalt:'#ccc',
fillLight:'#19be6b',
}
let initObj = initData(svg);
initObj.svgAddNode()
window.setTimeout(initObj.changePort,100)
function initData(svgEle){
//初始化
let g, gPosition;
//初始化力导向布局
let forceSimulation = d3.forceSimulation()
.force('charge',d3.forceManyBody().strength(-100))//电荷力
.force('forceCollide',d3.forceCollide().radius(colsWidth/2+50))//检测碰撞
.force('link',d3.forceLink().id(function(d){return d.id}))//link
initSVG()
//首次执行
function initSVG(){
let zoom = d3.zoom()
.scaleExtent([0.1,10])
.on("zoom",function(d){
g.attr("transform",function(){//初始化
return `translate(${d3.event.transform.x+gPosition.x},${d3.event.transform.y+gPosition.y}) scale(${d3.event.transform.k})`
})
})
svgEle.call(zoom).on('dblclick.zoom',null);
g = svgEle.append('g')
g.append("g").attr('class','links')
}
//初始化位置
function changePort(){
gPosition = svgEle.select('g').node().getBBox()
gPosition.x = -gPosition.x+20
gPosition.y = -gPosition.y+20
// svg.transition().delay(500).attr('viewBox',`${gPosition.x-20} ${gPosition.y-20} ${width} ${height}`)
g.transition().delay(0).attr('transform',function(){
return `translate(${gPosition.x},${gPosition.y}) scale(1)`
})
}
//绘制
let allTables, addTablesData, allLinks, addLinksData, allColsGs, addColsGsData,allFooter, addFooterData;
function svgAddNode(){
forceInit(tables,links)
addTables()
addLinks()
initDrag()
}
function tick(){//force 迭代执行函数
allTables.attr("transform", function(d){
return `translate(${d.x},${d.y})`
})
allLinks.attr('points',linkFn)
}
function end(){//force end函数
}
function forceInit(nodes,links){//在nodes、links原对象上添加位置属性
forceSimulation.nodes(nodes)
.on('end',end)
.on('tick',tick);
forceSimulation.force('link')
.links(links)
.distance(function (d) {
return d.value * colsWidth + 100
})
}
function addTables(){
addTablesData = g.selectAll('.tbClass').data(tables)
.enter()
.append('g')
.attr('class','tbClass')
.attr('id',function(d){return d.id})
.attr("transform", function(d){
return `translate(${d.x},${d.y})`
})
allTables = g.selectAll('.tbClass').data(tables)
addTitle()
addColsWrap()
addFooter()
}
function delTables(){
// allTables = g.selectAll('.tbClass').data(tables)
allTables.exit().remove()
// delLinks()
}
function initDrag(){
allTables.call(d3.drag()//添加拖动事件
.on('start',startFn)
.on('drag',dragFn)
.on('end',endFn)
)
//添加鼠标经过事件
allTables.on('mouseenter',function(d){
hoverLight.call(this,d,1)
})
.on('mouseleave',function(d){
hoverLight.call(this,d,0)
})
}
//左右连线计算
function linkFn(d){
let res = []
if(d.source.x < d.target.x){
res[2] = [d.target.x - offLine, d.target.y]
res[3] = [d.target.x, d.target.y]
if((d.source.x + colsWidth + offLine*0) < d.target.x ){
res[0] = [d.source.x + colsWidth, d.source.y]
res[1] = [d.source.x + colsWidth + offLine, d.source.y]
}else{
res[0] = [d.source.x, d.source.y]
res[1] = [d.source.x - offLine, d.source.y]
}
}else{
res[1] = [d.source.x - offLine, d.source.y]
res[0] = [d.source.x, d.source.y]
if((d.target.x + colsWidth + offLine*0) < d.source.x ){
res[3] = [d.target.x + colsWidth, d.target.y]
res[2] = [d.target.x + colsWidth + offLine, d.target.y]
}else{
res[3] = [d.target.x, d.target.y]
res[2] = [d.target.x - offLine, d.target.y]
}
}
res[0][1] += d.sourceIndex*colsHeight + colsHeight/2
res[1][1] += d.sourceIndex*colsHeight + colsHeight/2
res[2][1] += d.targetIndex*colsHeight + colsHeight/2
res[3][1] += d.targetIndex*colsHeight + colsHeight/2
return res.map(v=>v.join(',')).join(' ')
}
function startFn(d){//拖动开始
if(!d3.event.active){
forceSimulation.alphaTarget(0.8).restart()//[0,1]
}
d.fx = d.x
d.fy = d.y
}
function dragFn(d){//拖动中
d.fx = d3.event.x
d.fy = d3.event.y
}
function endFn(d){//拖动结束
if(!d3.event.active){
forceSimulation.alphaTarget(0)
}
// d.fx = null
// d.fy = null
}
//添加连线
function addLinks(){
addLinksData = d3.select('.links')
.selectAll("polyline")
.data(links)
.enter()
.append("polyline")
.attr("fill",'none')
.attr("stroke",'#000')
.attr("stroke-width",2)
.attr('class','link-polyline')
allLinks = d3.select('.links')
.selectAll('polyline')
.data(links)
}
function delLinks(){
// allLinks = d3.select('.links')
// .selectAll('polyline')
// .data(links)
allLinks.exit().remove()
}
//添加边框
function gsAddBorder(){
addTablesData.append('rect')
.attr('height',function(d){
return colsHeight*d.cols.length + tableTitleHeight
})
.attr('width',function(d){
return colsWidth
})
.attr('rx',5)
.attr('stroke','#5cadff')
.attr('stroke-width','1px')
.attr('fill-opacity','0')
}
//添加表头
function addTitle() {
let tTitle = addTablesData.append('g')
.attr('class','table-title')
tTitle.append('rect')
// .attr('rx',5)
.attr('height',function(d){
return tableTitleHeight
})
.attr('width',function(d){
return colsWidth
})
.attr('fill','#5cadff')
.style('cursor','pointer')
// .attr("transform", "translate(" + 0 + ", " + 10 + ")")
tTitle.append('text')
.text(function(d){
return d.tableName
})
.attr('dx',function(){
return colsWidth/2
})
// .attr('dy',tableTitleHeight/2)
.attr('class','svg-text')
.attr('text-anchor','middle')
.attr('dominant-baseline','middle')
.attr('fill','#000')
}
//创建字段g
function addColsWrap(){
addTablesData.append('g')
.attr('class','table-cols')
.attr('transform',"translate(" + 0 + ", " + tableTitleHeight + ")")
addColsGs()
}
function addColsGs(){
addColsGsData = addTablesData.select('.table-cols')
.selectAll('g')
.data(function(d){
return d.cols
})
.enter()
.append('g')
.attr("transform",(d,i)=>`translate(${0},${i*colsHeight})`)
.attr('id',function(d){ return d.id })
allColsGs = addTablesData.select('.table-cols')
.selectAll('g')
.data(function(d){
return d.cols
})
colsAdd()
}
function addExtColsGs(obj){
addColsGsData = d3.select('#'+obj.id).select('.table-cols')
.selectAll('g')
.data(function(d){
return d.cols
})
.enter()
.append('g')
.attr("transform",(d,i)=>`translate(${0},${i*colsHeight})`)
.attr('id',function(d){ return d.id })
colsAdd()
}
function delColsGs(){
allColsGs = allTables.select('.table-cols')
.selectAll('g')
.data(function(d){
return d.cols
})
allColsGs.exit().remove()
}
//添加字段信息
function colsAdd(){
addColsGsData.append('rect')
.attr('height',function(d){
return colsHeight
})
.attr('width',function(d){
return colsWidth
})
.attr('fill','#ccc')
addColsGsData.append('text')
.attr('class','svg-text')
.text(function(d){
return d.itemName
})
.attr('dx',function(){
// return colsWidth/2
return 10
})
.attr('dy',colsHeight/2)
.attr('dominant-baseline','middle')
.attr('fill','#000')
//添加高亮
addColsGsData.on('mouseenter',function(d){
colsLight.call(this,d,1)
})
.on('mouseleave',function(d){
colsLight.call(this,d,0)
})
}
//节点高亮
function colsLight(data,isEnter){
lineLight(data,isEnter)
d3.select(this)
.select('rect')
.transition()
// .delay(50)
.attr('fill',isEnter ? colsStyle.fillLight : colsStyle.fillDefalt)
}
//线高亮
function lineLight(data,isEnter){
d3.selectAll('.link-polyline')
.attr('stroke',function(d){
let source = d.source.cols[d.sourceIndex-1]
let target = d.target.cols[d.targetIndex-1]
if(data.id === source.id || data.id === target.id ){
return isEnter ? lineStyle.strokeLight : lineStyle.strokeDefault
}else{
return lineStyle.strokeDefault
}
})
.attr('stroke-width',function(d){
let source = d.source.cols[d.sourceIndex-1]
let target = d.target.cols[d.targetIndex-1]
if(data.id === source.id || data.id === target.id ){
return isEnter ? lineStyle.strokeWidthLight : lineStyle.strokeWidthDefault
}else{
return lineStyle.strokeWidthDefault
}
})
}
//提升节点层次
function hoverLight(data,isEnter){
// let preData = Object.assign(data)
// if(isEnter){
// d3.select(this).raise()
// }else{
// d3.select(this).lower()
// }
}
//添加按钮
function addFooter(){
addFooterData = addTablesData.append('g')
.attr('class','table-foot')
addFooterData.append('rect')
.attr('width',colsWidth)
.attr('height',tableFootHeight)
.attr('fill','#f8f8f9')
addFooterData.append('text')
.attr('class','table-foot-btn table-foot-btn-left')
.attr('dy',tableFootHeight/2)
.attr('dx',10)
.on('click',addCols)
addFooterData.append('text')
.attr('class','table-foot-btn table-foot-btn-right')
.attr('dy',tableFootHeight/2)
.attr('dx',colsWidth-60)
.on('click',addNode)
allFooter = allTables.selectAll('.table-foot')
updateFoot()
}
function updateFoot(){
allFooter = allTables.selectAll('.table-foot')
allFooter.attr('transform',function(d){
return `translate(${0},${tableTitleHeight + colsHeight*d.cols.length})`
})
allFooter.selectAll('.table-foot-btn-left').text(function(d){
return d.text ? d.text[0] || btnText.leftOpen : btnText.leftOpen
})
allFooter.selectAll('.table-foot-btn-right').text(function(d){
return d.text ? d.text[1] || btnText.rightOpen : btnText.rightOpen
})
}
return {
changePort,
svgAddNode,
addExtColsGs,
updateFoot,
delColsGs,
delTables,
}
}
function addNode(data){
let temp = tables.length
let random = Math.floor(Math.random()*6)+1
if(data.isOpen){ //节点是否是打开状态,目前功能未完成
data.isOpen = false
// tables = tables.filter(v=>v.id != data.id)
console.table(tables)
// links = links.filter(v=>v.source.id != data.id && v.target.id != data.id)
// initObj.delTables()
initObj.svgAddNode()
}else{
tables.push({
tableName:'sda_fdsf'+temp,
id:'table'+temp,
cols:new Array(random).fill(1).map((v2,i2)=>{
return {
id:'table'+temp+'cols'+i2,
itemName:'table'+temp+'colsName'+i2,
}
}),
})
links.push({
source:data.id,
target:'table'+temp,
relation:'line1',
sourceIndex:1,
targetIndex:Math.floor(Math.random()*random)+1,
value:1,
})
data.isOpen = true
initObj.svgAddNode()
}
console.log(tables)
console.log(links)
}
function addCols(data){
let len = data.cols.length
let col = new Array(2).fill(1).map((v,i2)=>{
return {
id: data.id+'cols'+(i2+len),
itemName:data.id+'colsName'+(i2+len),
}
})
if(data.isAddCols){//收起
data.isAddCols = false
data.text = [btnText.leftOpen,btnText.rightOpen]
data.allCols = data.cols
data.cols = data.linkCols
initObj.delColsGs()
}else{//展开
data.isAddCols = true
data.text = [btnText.leftClose,btnText.rightClose]
data.linkCols = data.cols.concat()
data.cols = data.cols.concat(col)
initObj.addExtColsGs(data)
}
initObj.updateFoot()
}
function toImg(){
var serializer = new XMLSerializer();
var source = '<?xml version="1.0" standalone="no"?>\r\n' + serializer.serializeToString(svg.node());
var image = new Image;
image.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source);
var canvas = document.createElement("canvas");
canvas.width = 1000;
canvas.height = 800;
var context = canvas.getContext("2d");
context.fillStyle = '#fff';//#fff设置保存后的PNG 是白色的
context.fillRect(0, 0, 10000, 10000);
image.onload = function() {
context.drawImage(image, 0, 0);
var a = document.createElement("a");
a.download = "name.png";
a.href = canvas.toDataURL("image/png");
a.click();
};
}
function downloadSVG(){
// console.log()
let fileName = 'svg';
let content = document.getElementById('wrap').innerHTML;
let aTag = document.createElement('a');
let blob = new Blob([content]);
aTag.download = fileName;
aTag.href = URL.createObjectURL(blob);
aTag.click();
URL.revokeObjectURL(blob);
}
</script>
</body>
</html>