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

src/utils/Draw.ts   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 77
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 66
mnd 3
bc 3
fnc 0
dl 0
loc 77
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
    handleDrag,
10
} from './helpers/GraphHelpers';
11
12
export const draw = (network: Network, container: HTMLDivElement) => {
13
    const { nodes, links } = network;
14
15
    const width = container.clientWidth + 500;
16
    const height = container.clientHeight + 500;
17
18
    const simulation = createSimulation(nodes, links, width, height);
19
    const svgContainer = createSVGContainer(container, width, height);
20
21
    const zoomLayer = createZoom(svgContainer);
22
23
    generateMarkers(svgContainer);
24
25
    const linkElements = zoomLayer
26
        .append('g')
27
        .attr('id', 'links')
28
        .selectAll<Element, DependencyLink>('line.link')
29
        .data(links)
30
        .enter()
31
        .append<Element>('svg:path')
32
        .attr('class', 'link')
33
        .attr('marker-end', 'url(#provider)')
34
        .style('stroke-width', 1)
35
        .attr('stroke-opacity', 0.12);
36
37
    const labelNodes = zoomLayer.append('g').attr('id', 'labels');
38
39
    labelNodes
40
        .selectAll<Element, DependencyNode>('g#labels')
41
        .data(nodes)
42
        .enter()
43
        .append<SVGGElement>('g')
44
        .call(handleDrag(simulation))
45
        .append('text')
46
        .attr('fill', '#5E6063')
47
        .attr('cursor', 'default')
48
        .text(d => d.name);
49
50
    labelNodes
51
        .selectAll('g')
52
        .data(nodes)
53
        .append<SVGPathElement>('svg:path')
54
        .attr('class', 'label')
55
        .attr('fill', '#dcdee0')
56
        .attr('d', generateLabelPath);
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('transform', (node: DependencyNode) =>
70
                node.x && node.y ? 'translate(' + (node.x - 30) + ',' + (node.y - 55) + ')' : null
71
            )
72
            .lower();
73
    });
74
75
    return svgContainer.node();
76
};
77