|
1 | 1 | <script>
|
2 |
| - import { onMount } from 'svelte'; |
3 |
| - import * as d3 from 'd3'; |
| 2 | + import { onMount } from "svelte"; |
| 3 | + import * as d3 from "d3"; |
4 | 4 |
|
5 | 5 | export let componentStructure;
|
| 6 | + // let containerHeight = 0; |
| 7 | + // let containerWidth = 0; |
| 8 | + // document.addEventListener("DOMContentLoaded", () => { |
| 9 | + // containerWidth = document.getElementById("tree-container").offsetWidth; |
| 10 | + // containerHeight = document.getElementById("tree-container").offsetHeight; |
| 11 | + // console.log("container: ", containerWidth); |
| 12 | + // }); |
6 | 13 |
|
7 | 14 | onMount(() => {
|
8 | 15 | let height = 2000;
|
9 | 16 | let width = 2000;
|
10 | 17 |
|
| 18 | + let containerWidth = document.getElementById("tree-container").offsetWidth; |
| 19 | + let containerHeight = |
| 20 | + document.getElementById("tree-container").offsetHeight; |
| 21 | +
|
11 | 22 | let window = d3
|
12 |
| - .select('#tree-container') |
13 |
| - .append('svg') |
14 |
| - .attr('height', height) |
15 |
| - .attr('width', width) |
16 |
| - .append('g') |
17 |
| - .attr('transform', 'translate(50,50)'); |
| 23 | + .select("#tree-container") |
| 24 | + .append("svg") |
| 25 | + .attr("height", containerHeight) |
| 26 | + .attr("width", containerWidth) |
| 27 | + .append("g") |
| 28 | + .attr("transform", "translate(50,50)"); |
18 | 29 |
|
19 | 30 | const root = d3.hierarchy(componentStructure[0]);
|
20 |
| - //const roots = d3.hierarchy(root); |
21 |
| - // console.log(componentStructure[0]); |
22 |
| - // console.log(root); |
23 | 31 |
|
24 | 32 | // console.log('root test', root.data);
|
25 |
| - const tree = d3.tree().size([height / 4, width / 4]); |
26 |
| -
|
27 |
| - //console.log('hello'); |
28 |
| - // console.log(componentStructure); |
29 |
| - // console.log(componentStructure[0]); |
| 33 | + const tree = d3.tree().size([containerHeight / 4, containerWidth / 4]); |
30 | 34 |
|
31 | 35 | const treeDataTransformed = tree(root);
|
32 | 36 |
|
33 | 37 | let nodes = treeDataTransformed.descendants();
|
34 |
| - console.log('nodes', nodes); |
35 |
| - //console.log(nodes); |
36 | 38 | let links = treeDataTransformed.links();
|
37 |
| - //console.log(links); |
38 | 39 |
|
39 | 40 | let node = window
|
40 |
| - .selectAll('.node') |
| 41 | + .selectAll(".node") |
41 | 42 | .data(nodes)
|
42 | 43 | .enter()
|
43 |
| - .append('g') |
44 |
| - .attr('class', 'node') |
45 |
| - .attr('transform', function (d) { |
| 44 | + .append("g") |
| 45 | + .attr("class", "node") |
| 46 | + .attr("transform", function (d) { |
46 | 47 | //console.log('d', d);
|
47 | 48 | // console.log(typeof d);
|
48 |
| - return 'translate(' + d.x + ',' + d.y + ')'; |
| 49 | + return "translate(" + d.x + "," + d.y + ")"; |
49 | 50 | });
|
50 | 51 |
|
51 |
| - node.append('circle').attr('r', 15).attr('fill', '#ff0000'); |
| 52 | + node.append("circle").attr("r", 15).attr("fill", "#e3ae52"); |
52 | 53 |
|
53 | 54 | // console.log(root);
|
54 | 55 |
|
55 |
| - node.append('text').text(function (d) { |
56 |
| - //console.log(d.data.name); |
57 |
| - return d.data.name; |
58 |
| - }); |
| 56 | + node |
| 57 | + .append("text") |
| 58 | + .text(function (d) { |
| 59 | + //console.log(d.data.name); |
| 60 | + return d.data.name; |
| 61 | + }) |
| 62 | + .style("fill", "antiquewhite") |
| 63 | + .attr("dy", "2em") // Move text down |
| 64 | + .attr("text-anchor", "middle"); |
59 | 65 |
|
60 | 66 | let diagonal = d3
|
61 | 67 | .linkVertical()
|
62 | 68 | .x((d) => d.x)
|
63 | 69 | .y((d) => d.y);
|
64 | 70 |
|
65 | 71 | window
|
66 |
| - .selectAll('.link') |
| 72 | + .selectAll(".link") |
67 | 73 | .data(links)
|
68 | 74 | .enter()
|
69 |
| - .append('path') |
70 |
| - .attr('class', 'link') |
71 |
| - .attr('fill', 'none') |
72 |
| - .attr('stroke', '#fff') |
73 |
| - .attr('stroke-width', 1) |
74 |
| - .attr('d', diagonal); |
| 75 | + .append("path") |
| 76 | + .attr("class", "link") |
| 77 | + .attr("fill", "none") |
| 78 | + .attr("stroke", "#fff") |
| 79 | + .attr("stroke-width", 1) |
| 80 | + .attr("d", diagonal); |
75 | 81 | });
|
76 | 82 | </script>
|
77 | 83 |
|
78 | 84 | <div id="tree-container"></div>
|
79 | 85 |
|
| 86 | +<style> |
| 87 | + #tree-container { |
| 88 | + width: 100%; |
| 89 | + height: 100%; |
| 90 | + } |
| 91 | +</style> |
0 commit comments