-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.vue
597 lines (569 loc) · 19.3 KB
/
index.vue
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
<template>
<div class="d3-tree-container" :class="{'empty-data-classifty':!emptyFlag}">
<div id="product_tree" />
<div v-if="!emptyFlag" class="empty-card-s">
<el-row>
<el-col :span="24"><div class="grid-content-flex">
<img src="@/assets/layout/empy1.png" alt="">
<span>暂无数据</span>
</div></el-col>
</el-row>
</div>
</div>
</template>
<script>
// import { mockData } from './GetCompanyEquityShareMap.js'
import { getCompanyEquityShareMap } from '@/api/ei-web/business'
var container
var zoom
var depthInfo
var _data
export default {
name: 'CompanyGuquan',
data() {
return {
directions: ['upward', 'downward'],
treeData: {},
d3: null,
rootData: null,
forUpward: true,
rootRectWidth: 0, // 根节点rect的宽度
rootName: '', // 根节点名称
projObject: this.$route.query.projObject || '53363',
emptyFlag: true
}
},
async mounted() {
var child = document.getElementById('product_tree')
child.innerHTML = ''
const projObject = this.projObject
const resultData = await this.getTreeMap(projObject)
const result = resultData.Result
console.log('result:', resultData)
if (!resultData.Result) {
this.emptyFlag = false
}
this.init(result)
},
methods: {
getTreeMap(projObject) {
return new Promise((resolve, reject) => {
getCompanyEquityShareMap(projObject).then(res => {
if (res.status === '0') {
const data = JSON.parse(res.data.companyEquityShareMap)
resolve(data)
this.treeData = data.Result
}
})
})
},
getData(data) {
// 测试内容
const _children = data.children
const _name = data.name
this.rootData = {
'downward': {
'direction': 'downward',
'name': 'origin',
'children': []
},
'upward': {
'direction': 'upward',
'name': 'origin',
'children': _children
}
}
this.rootName = _name
},
getTreeConfig() {
var treeConfig = {
'margin': {
'top': 10,
'right': 5,
'bottom': 0,
'left': 30
}
}
const clientWidth = document.documentElement.clientWidth - 197 // 减掉左边侧边菜单
const clientHeight = document.documentElement.clientHeight - 102 // 上面头部
treeConfig.chartWidth = (clientWidth - treeConfig.margin.right - treeConfig.margin.left)
treeConfig.chartHeight = (clientHeight - treeConfig.margin.top - treeConfig.margin.bottom)
treeConfig.centralHeight = treeConfig.chartHeight / 2
treeConfig.centralWidth = treeConfig.chartWidth / 2
treeConfig.linkLength = 120
treeConfig.duration = 500 // 动画时间
return treeConfig
},
graphTree(config) {
var _self = this
var d3 = this.d3
var linkLength = config.linkLength
var duration = config.duration
var hasChildNodeArr = []
var id = 0
let forUpward = this.forUpward
const rootRectWidth = this.rootRectWidth
var diagonal = d3.svg.diagonal()
.source(function(d) {
return {
'x': d.source.x,
'y': d.source.name === 'origin' ? (forUpward ? d.source.y - 20 : d.source.y + 20) : (forUpward ? d.source.y - 60 : d.source.y + 60)
}
})
.target(function(d) {
return {
'x': d.target.x,
'y': d.target.y
}
})
.projection(function(d) {
return [d.x, d.y]
})
// d3.behavior.zoom() 加载图像时设置初始化缩放倍数跟移动大小后,再次用鼠标滚轮进行缩放时
var zoom = d3.behavior.zoom()
.scaleExtent([0.3, 2])
.on('zoom', redraw)
var svg = d3.select('#product_tree')
.append('svg')
.attr('width', config.chartWidth + config.margin.right + config.margin.left)
.attr('height', config.chartHeight + config.margin.top + config.margin.bottom)
.attr('xmlns', 'http://www.w3.org/2000/svg')
.on('mousedown', disableRightClick)
.call(zoom)
.on('dblclick.zoom', null)
var treeG = svg.append('g')
.attr('class', 'gbox')
.attr('transform', 'translate(' + config.margin.left + ',' + config.margin.top + ')')
/*
* 绘制箭头
* @param {string} markerUnits [设置为strokeWidth箭头会随着线的粗细发生变化]
* @param {string} viewBox 坐标系的区域
* @param {number} markerWidth,markerHeight 标识的大小
* @param {string} orient 绘制方向,可设定为:auto(自动确认方向)和 角度值
* @param {number} stroke-width 箭头宽度
* @param {string} d 箭头的路径
* @param {string} fill 箭头颜色
* @param {string} id resolved0表示公司 resolved1表示个人
* 直接用一个marker达不到两种颜色都展示的效果
*/
// 箭头(下半部分)
var markerDown = svg.append('marker')
.attr('id', 'resolvedDown')
.attr('markerUnits', 'strokeWidth') // 设置为strokeWidth箭头会随着线的粗细发生变化
.attr('markerUnits', 'userSpaceOnUse')
.attr('viewBox', '0 -5 10 10') // 坐标系的区域
.attr('markerWidth', 12) // 标识的大小
.attr('markerHeight', 12)
.attr('orient', '90') // 绘制方向,可设定为:auto(自动确认方向)和 角度值
.attr('refX', 0) // 箭头坐标
.attr('refY', 15)
.attr('stroke-width', 2) // 箭头宽度
.append('path')
.attr('d', 'M0,-5L10,0L0,5') // 箭头的路径
.attr('fill', '#000') // 箭头颜色
// 箭头(上半部分)
var markerUp = svg.append('marker')
.attr('id', 'resolvedUp')
.attr('markerUnits', 'strokeWidth') // 设置为strokeWidth箭头会随着线的粗细发生变化
.attr('markerUnits', 'userSpaceOnUse')
.attr('viewBox', '0 -5 10 10') // 坐标系的区域
.attr('markerWidth', 4) // 标识的大小
.attr('markerHeight', 4)
.attr('refX', -3) // 箭头坐标
// .attr('refY', 15)
.attr('orient', '90') // 绘制方向,可设定为:auto(自动确认方向)和 角度值
.attr('stroke-width', 2) // 箭头宽度
.append('path')
.attr('d', 'M0,-5L10,0L0,5') // 箭头的路径
.attr('fill', '#b40005') // 箭头颜色
// Initialize the tree nodes and update chart.
for (var d in this.directions) {
var direction = this.directions[d]
var data = _self.treeData[direction]
data.x0 = config.centralWidth
data.y0 = config.centralHeight
data.children.forEach(collapse)
update(data, data, treeG)
}
function update(source, originalData, g) {
var direction = originalData['direction']
forUpward = direction === 'upward'
const nodeClass = direction + 'Node'
const linkClass = direction + 'Link'
var downwardSign = (forUpward) ? -1 : 1
var nodeColor = (forUpward) ? '#37592b' : '#8b4513'
var isExpand = false
var statusUp = true
var statusDown = true
var nodeSpace = 130
var tree = d3.layout.tree().sort(sortByDate).nodeSize([nodeSpace, 0])
var nodes = tree.nodes(originalData)
var links = tree.links(nodes)
var offsetX = -config.centralWidth
nodes.forEach(function(d) {
d.y = downwardSign * (d.depth * linkLength) + config.centralHeight
d.x = d.x - offsetX
if (d.name == 'origin') {
d.x = config.centralWidth
d.y += downwardSign * 0 // 上下两树图根节点之间的距离
console.log('origin:', d)
}
console.log('d:', d)
})
// Update the node.
var node = g.selectAll('g.' + nodeClass)
.data(nodes, function(d) {
return d.id || (d.id = ++id)
})
var nodeEnter = node.enter().append('g')
.attr('class', nodeClass)
.attr('transform', function(d) {
return 'translate(' + source.x0 + ',' + source.y0 + ')'
})
.style('cursor', function(d) {
return (d.name == 'origin') ? '' : (d.children || d._children) ? 'pointer' : ''
})
// .on('click', click);
nodeEnter.append('svg:rect')
.attr('x', function(d) {
return (d.name === 'origin') ? -(rootRectWidth / 2) : -60
})
.attr('y', function(d) {
// return (d.name === 'origin') ? -20 : forUpward ? -52 : 12
return (d.name === 'origin') ? -20 : forUpward ? -62 : 12
})
.attr('width', function(d) {
return (d.name === 'origin') ? rootRectWidth : 120
})
.attr('height', function(d) {
return (d.name === 'origin') ? 38 : 62
})
.attr('rx', 0)
.style('stroke', function(d) {
return (d.name === 'origin') ? '#128bed' : '#CCC'
})
.style('fill', '#fff')
nodeEnter.append('circle')
.attr('r', 1e-6)
nodeEnter.append('text')
.attr('class', 'text-style-name')
.attr('x', function(d) {
return (d.name === 'origin') ? '0' : '0'
})
.attr('dy', function(d) {
return (d.name === 'origin') ? '.35em' : forUpward ? '-40' : '24'
})
.attr('text-anchor', function(d) {
return (d.name === 'origin') ? 'middle' : 'middle'
})
.attr('fill', '#000')
.text((d) => {
if (d.name === 'origin') {
return _self.rootName
}
if (d.repeated) {
return '[Recurring] ' + d.name
}
return (d.name.length > 10) ? d.name.substr(0, 10) : d.name
})
.style({
'fill-opacity': 1e-6,
'fill': function(d) {
if (d.name === 'origin') {
return '#fff'
}
},
'font-size': function(d) {
return (d.name === 'origin') ? 14 : 11
},
'cursor': 'pointer'
})
.on('click', ChangeModal)
nodeEnter.append('text')
.attr('class', 'text-style-name')
.attr('x', '0')
.attr('dy', function(d) {
return (d.name === 'origin') ? '.35em' : forUpward ? '-24' : '35'
})
.attr('text-anchor', function() {
return (d.name === 'origin') ? 'middle' : 'middle'
})
.text(function(d) {
const str = d.name.substr(10, d.name.length)
const name = str.length > 10 ? d.name.substring(10, 19) + '...' : str
return name
})
.style({
'fill': '#337ab7',
'font-size': function(d) {
return (d.name === 'origin') ? 14 : 11
},
'cursor': 'pointer'
})
nodeEnter.append('text')
.attr('x', '0')
.attr('dy', function(d) {
return (d.name === 'origin') ? '.35em' : forUpward ? '-10' : '48'
})
.attr('text-anchor', 'middle')
.attr('class', 'linkname')
.style('fill', '#666666')
.style('font-size', 10)
.text((d) => {
// 大于16
const str = (d.name === 'origin') ? '' : d.SubConAmt ? '认缴金额:' + d.SubConAmt : ''
const strReturn = str.length > 16 ? str.substring(0, 16) + '...' : str
return strReturn
})
nodeEnter.append('text')
.attr('x', '5')
.attr('dy', function(d) {
return (d.name === 'origin') ? '.35em' : forUpward ? '10' : '10'
})
.attr('text-anchor', 'start')
.attr('class', 'linkname')
.style('fill', '#128bed')
.style('font-size', 10)
.text((d) => {
return (d.name === 'origin') ? '' : d.FundedRatio
})
// Transition nodes to their new position.原有节点更新到新位置
var nodeUpdate = node.transition()
.duration(duration)
.attr('transform', function(d) {
return 'translate(' + d.x + ',' + d.y + ')'
})
// +- 圆 控制
nodeUpdate.select('circle')
.attr('r', function(d) {
return (d.name == 'origin') ? 0 : (hasChildNodeArr.indexOf(d) == -1) ? 0 : 6
})
.attr('cy', function(d) {
return (d.name == 'origin') ? -20 : (forUpward) ? -68 : 59
})
.style('fill', function(d) {
return hasChildNodeArr.indexOf(d) != -1 ? '#fff' : ''
// if (d._children || d.children) { return "#fff"; } else { return "rgba(0,0,0,0)"; }
})
.style('stroke', function(d) {
return hasChildNodeArr.indexOf(d) != -1 ? '#128bed' : ''
// if (d._children || d.children) { return "#8b4513"; } else { return "rgba(0,0,0,0)"; }
})
.style('fill-opacity', function(d) {
if (d.children) {
return 0.35
}
})
// Setting summary node style as class as mass style setting is
// not compatible to circles.
.style('stroke-width', function(d) {
if (d.repeated) {
return 5
}
})
// 代表是否展开的+-号
nodeEnter.append('svg:text')
.attr('class', 'isExpand')
.attr('x', '0')
.attr('dy', function(d) {
return forUpward ? -64 : 62
})
.attr('text-anchor', 'middle')
.style('fill', '#128bed')
.style('font-size', 14)
.text(function(d) {
if (d.name === 'origin') {
return ''
}
return hasChildNodeArr.indexOf(d) !== -1 ? '+' : ''
})
.on('click', click)
nodeUpdate.select('text').style('fill-opacity', 1)
var nodeExit = node.exit().transition()
.duration(duration)
.attr('transform', function(d) {
return 'translate(' + source.x + ',' + source.y + ')'
})
.remove()
nodeExit.select('circle')
.attr('r', 1e-6)
.attr('stroke', function(d) {
return '#128bed'
})
nodeExit.select('text')
.style('fill-opacity', 1e-6)
// 数据连接,根据目标节点的id绑定数据
var link = g.selectAll('path.' + linkClass)
.data(links, function(d) {
return d.target.id
})
// 增加新连接
link.enter().insert('path', 'g')
.attr('class', linkClass)
.attr('stroke', function(d) {
return '#7a7a7a'
})
.attr('fill', 'none')
.attr('stroke-width', '1px')
.attr('opacity', 0.5)
.attr('d', function(d) {
var o = {
x: source.x0,
y: source.y0
}
return diagonal({
source: o,
target: o
})
})
.attr('marker-end', function(d) {
return forUpward ? 'url(#resolvedUp)' : 'url(#resolvedDown)'
}) // 根据箭头标记的id号标记箭头;
.attr('id', function(d, i) {
return 'mypath' + i
})
// 原有连接更新位置
link.transition()
.duration(duration)
.attr('d', diagonal)
// 折叠的链接,收缩到源节点处
link.exit().transition()
.duration(duration)
.attr('d', function(d) {
var o = {
x: source.x,
y: source.y
}
return diagonal({
source: o,
target: o
})
})
.remove()
// 把旧位置存下来,用以过渡
nodes.forEach(function(d) {
d.x0 = d.x
d.y0 = d.y
})
function ChangeModal() {
_self.Modal = true
}
// 切换折叠与否
function click(d) {
if (forUpward) {} else {
if (d._children) {
console.log('对外投资--ok')
} else {
console.log('对外投资--no')
}
}
isExpand = !isExpand
if (d.name == 'origin') {
return
}
if (d.children) {
d._children = d.children
d.children = null
d3.select(this).text('+')
} else {
d.children = d._children
d._children = null
// expand all if it's the first node
if (d.name == 'origin') {
d.children.forEach(expand)
}
d3.select(this).text('-')
}
update(d, originalData, g)
}
}
function expand(d) {
if (d._children) {
d.children = d._children
d.children.forEach(expand)
d._children = null
}
}
function collapse(d) {
if (d.children && d.children.length != 0) {
d._children = d.children
d._children.forEach(collapse)
d.children = null
hasChildNodeArr.push(d)
}
}
function redraw() {
treeG.attr('transform', 'translate(' + d3.event.translate + ')' +
' scale(' + d3.event.scale + ')')
}
function disableRightClick() {
// stop zoom
if (d3.event.button == 2) {
console.log('No right click allowed')
d3.event.stopImmediatePropagation()
}
}
function sortByDate(a, b) {
var aNum = a.name.substr(a.name.lastIndexOf('(') + 1, 4)
var bNum = b.name.substr(b.name.lastIndexOf('(') + 1, 4)
return d3.ascending(aNum, bNum) ||
d3.ascending(a.name, b.name) ||
d3.ascending(a.id, b.id)
}
},
drawing() {
const _this = this
let downwardLength = 0
let upwardLength = 0
const TreeChart = (d3Object) => {
_this.d3 = d3Object
}
TreeChart.prototype.drawChart = () => {
// First get tree data for both directions.
_this.directions.forEach((direction) => {
_this.treeData[direction] = _this.rootData[direction]
})
// rootName = '上海冰鉴信息科技有限公司';
this.rootRectWidth = _this.rootName.length * 15
// 获得upward第一级节点的个数
upwardLength = _this.rootData.upward.children.length
// 获得downward第一级节点的个数
downwardLength = _this.rootData.downward.children.length
_this.graphTree(_this.getTreeConfig())
}
var d3GenerationChart = new TreeChart(d3)
d3GenerationChart.drawChart()
},
init(data) {
this.getData(data)
this.drawing()
}
}
}
</script>
<style lang="scss">
.empty-data-classifty{
display: flex;
justify-content: center;
align-items: center;
}
.d3-tree-container{
background-color: #fff;
min-height: calc(100vh - 84px);
.text-style-name{
fill: #333!important;
}
.empty-card-s{
.grid-content-flex{
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
img{
width: 193px;
height: 188px;
}
}
}
}
</style>