-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrheingold.html
More file actions
222 lines (182 loc) · 6.28 KB
/
rheingold.html
File metadata and controls
222 lines (182 loc) · 6.28 KB
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
<body>
<link rel="stylesheet"
href="rheingold.css"
text="type/css" >
</link>
<div class="title" align="center"style="font-family:'Brush Script MT', cursive;font-weight: bold;font-size: 40px;text-shadow: 1.5px 1.5px orange;color:black">
Reigngold Tilford Layout
</div></body>
<!-- reigngoldtilford correct -->
<!--
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reingold-Tilford Tree Layout</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<input type="file" id="csvInput" accept=".csv">
<script>
// Function to handle user input
document.getElementById('csvInput').addEventListener('change', handleFileSelect);
function handleFileSelect(event) {
const file = event.target.files[0];
if (file) {
// Read the CSV file
const reader = new FileReader();
reader.onload = function(e) {
const csvData = d3.csvParse(e.target.result);
const adjacencyMatrix = convertCSVToMatrix(csvData);
createReingoldTilfordLayout(adjacencyMatrix);
};
reader.readAsText(file);
}
}
function convertCSVToMatrix(csvData) {
const matrix = [];
csvData.forEach((row) => {
const rowData = Object.values(row).map(Number);
matrix.push(rowData);
});
return matrix;
}
function createReingoldTilfordLayout(matrix) {
// Convert the adjacency matrix to hierarchical data
var hierarchicalData = convertToHierarchical(matrix);
// Set the size of the canvas
var width = 1000;
var height = 600;
// Create a tree layout
var treeLayout = d3.tree().size([width, height]);
// Apply the Reingold-Tilford layout to the data
var treeData = treeLayout(d3.hierarchy(hierarchicalData));
// Create an SVG element
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(50, 20)"); // Adjust the translation as needed
// Draw links
var links = svg.selectAll(".link")
.data(treeData.links())
.enter().append("path")
.attr("class", "link")
.attr("d", d3.linkHorizontal()
.x(function(d) { return d.y; })
.y(function(d) { return d.x; }));
// Draw nodes
var nodes = svg.selectAll(".node")
.data(treeData.descendants())
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodes.append("circle")
.attr("r", 5);
nodes.append("text")
.attr("dy", ".35em")
.attr("x", function(d) { return d.children ? -13 : 13; })
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.data.name; });
function convertToHierarchical(matrix) {
const root = { name: "root", children: [] };
for (let i = 0; i < matrix.length; i++) {
const node = { name: "Node " + i, children: [] };
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] === 1) {
node.children.push({ name: "Node " + j });
}
}
root.children.push(node);
}
return root;
}
}
</script>
</body>
</html> -->
<!-- reingoldtilfordd treee -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Reingold-Tilford Layout</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<input type="file" id="csvInput" accept=".csv">
<svg width="1000" height="600"></svg>
<script>
const fileInput = document.getElementById('csvInput');
fileInput.addEventListener('change', handleFile);
function handleFile(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(event) {
const csvData = event.target.result;
visualizeCSV(csvData);
}
reader.readAsText(file);
}
function visualizeCSV(csvData) {
// Parse CSV data
const rows = d3.csvParseRows(csvData);
const columns = rows[0].length;
// Create an adjacency matrix
const adjacencyMatrix = [];
for (let i = 0; i < columns; i++) {
const columnData = [];
for (let j = 1; j < rows.length; j++) {
columnData.push(parseInt(rows[j][i]));
}
adjacencyMatrix.push(columnData);
}
// Create the tree layout manually
const treeData = { name: 'Root', children: [] };
for (let i = 0; i < columns; i++) {
const child = { name: String.fromCharCode(97 + i), children: [] };
for (let j = 0; j < adjacencyMatrix[i].length; j++) {
if (adjacencyMatrix[i][j] === 1) {
const grandchild = { name: String.fromCharCode(97 + j) };
child.children.push(grandchild);
}
}
treeData.children.push(child);
}
const root = d3.hierarchy(treeData, d => d.children);
const treeLayout = d3.tree().size([700, 500]);
treeLayout(root);
const svg = d3.select("svg").html("");
const links = svg.selectAll("line.link")
.data(root.links())
.enter().append("line")
.attr("class", "link")
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y)
.attr("stroke", "white");
const nodes = svg.selectAll("circle.node")
.data(root.descendants())
.enter().append("circle")
.attr("class", "node")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 10)
.attr("fill", "orange");
const labels = svg.selectAll("text.label")
.data(root.descendants())
.enter().append("text")
.attr("class", "label")
.attr("x", d => d.x)
.attr("y", d => d.y - 15)
.attr("text-anchor", "middle")
.text(d => d.data.name)
.attr("fill", "blue");
}
</script>
</body>
</html>