Passed
Pull Request — master (#6)
by Pawel
03:03
created

src/utils/Draw.ts   A

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 85
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 73
mnd 5
bc 5
fnc 0
dl 0
loc 85
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { DependencyLink, DependencyNode, Network } from '../components/types';
2
import {
3
    createSimulation,
4
    createSVGContainer,
5
    createZoom,
6
    generateLabelPath,
7
    generateMarkers,
8
    generateLinkPath,
9
    getLabelTextHeight,
10
    handleDrag,
11
} from './helpers/GraphHelpers';
12
13
export const draw = (network: Network, container: HTMLDivElement) => {
14
    const { nodes, links } = network;
15
16
    const width = container.clientWidth + 500;
17
    const height = container.clientHeight + 500;
18
19
    const simulation = createSimulation(nodes, links, width, height);
20
    const svgContainer = createSVGContainer(container, width, height);
21
22
    const zoomLayer = createZoom(svgContainer);
23
24
    generateMarkers(svgContainer);
25
26
    const linkElements = zoomLayer
27
        .append('g')
28
        .attr('id', 'links')
29
        .selectAll<Element, DependencyLink>('line.link')
30
        .data(links)
31
        .enter()
32
        .append<Element>('svg:path')
33
        .attr('class', 'link')
34
        .attr('marker-end', 'url(#provider)')
35
        .style('stroke-width', 1)
36
        .attr('stroke-opacity', 0.12);
37
38
    const labelNodes = zoomLayer.append('g').attr('id', 'labels');
39
40
    labelNodes
41
        .selectAll('g#labels')
42
        .data(nodes)
43
        .enter()
44
        .append('g')
45
        .call(handleDrag(simulation))
46
        .append('svg:path')
47
        .attr('class', 'label')
48
        .attr('fill', '#dcdee0');
49
50
    labelNodes
51
        .selectAll('g')
52
        .data(nodes)
53
        .append('text')
54
        .attr('fill', '#5E6063')
55
        .attr('cursor', 'default')
56
        .text(d => d.name);
57
58
    simulation.on('tick', () => {
59
        linkElements.each(generateLinkPath);
60
61
        labelNodes
62
            .selectAll('text')
63
            .data(nodes)
64
            .attr('x', (node: DependencyNode) => (node.x ? node.x : null))
65
            .attr('y', (node: DependencyNode) => (node.y ? node.y : null));
66
67
        labelNodes
68
            .selectAll<Element, DependencyNode>('path')
69
            .attr('d', generateLabelPath)
70
            .attr('transform', (node: DependencyNode) =>
71
                node.x && node.y ? 'translate(' + (node.x - 30) + ',' + (node.y - 55) + ')' : null
72
            )
73
            .attr('width', function() {
74
                const labelTextWidth = getLabelTextHeight.call(this, 'width');
75
                return getLabelTextHeight.call(this, 'width') > 0 ? labelTextWidth + 10 : '';
76
            })
77
            .attr('height', function() {
78
                const labelTextHeight = getLabelTextHeight.call(this, 'height');
79
                return labelTextHeight > 0 ? labelTextHeight + 10 : '';
80
            });
81
    });
82
83
    return svgContainer.node();
84
};
85