-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfPage.html
180 lines (150 loc) · 4.59 KB
/
rfPage.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
svg {
display: block;
margin: auto;
}
.node circle {
fill: #660066;
/* Dark purple */
stroke: #E0B0FF;
/* White border */
stroke-width: 2.5px;
cursor: pointer;
/* Add cursor pointer for interactivity */
}
.node:hover circle {
fill: #FAE6FA;
/* change fill color to pink on hover */
}
.node text {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #cc99ff;
/* Light lilac purple */
stroke-width: 1.7px;
}
button {
outline: none;
color: #f2f2f2;
font-size: 14px;
font-weight: 500;
border-radius: 8px;
padding: 10px 16px; /* Adjust the padding as needed */
border: none;
margin: 8px 12px; /* Adjust the margin between buttons */
cursor: pointer;
transition: all 0.3s ease;
background-image: linear-gradient(135deg, #b4aee8 10%, #9370DB 100%);
}
button:hover {
transform: scale(0.95);
background-image: linear-gradient(135deg, #9370DB 10%, #b4aee8 100%);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<input type="file" id="fileInput" />
<button onclick="readFile()">Create RT</button>
<div id="visualization"></div>
<script>
function generateRT(adjMatrix) {
// Create a tree structure from the adjacency matrix
const treeData = createTreeStructure(adjMatrix);
// Set up the D3 tree layout with vertical orientation
const treeLayout = d3.tree().size([500, 500]);
// Create a root node for the tree
const rootNode = d3.hierarchy(treeData);
// Assign new y-coordinates based on the depth for layers and levels
treeLayout(rootNode);
// Create an SVG container
const svg = d3.select('#visualization').append('svg').attr('width', 550).attr('height', 550);
// Draw links between nodes
svg.selectAll('path.link')
.data(rootNode.links())
.enter().append('path')
.attr('class', 'link')
.attr('d', d3.linkVertical()
.x(d => d.x)
.y(d => d.y));
// Draw nodes
const nodes = svg.selectAll('g.node')
.data(rootNode.descendants())
.enter().append('g')
.attr('class', 'node')
.attr('transform', d => `translate(${d.x},${d.y})`);
nodes.append('circle')
.attr('r', 5);
// Add labels to nodes
nodes.append('text')
.attr('dy', '0.35em')
.attr('x', d => (d.children ? -1 : 1) * 8)
.style('text-anchor', d => d.children ? 'end' : 'start')
.text(d => d.data.name);
// Add numbers to nodes
nodes.append('text')
.attr('dy', '1.5em')
.attr('x', 0)
.style('text-anchor', 'middle')
.style('font-weight', 'bold')
.text(d => d.data.name.split(' ')[1]);
}
function createTreeStructure(adjMatrix) {
const numNodes = adjMatrix.length;
const treeData = {
name: 'Root',
children: []
};
for (let i = 0; i < numNodes; i++) {
const node = {
name: `${i + 1}`,
children: []
};
for (let j = 0; j < numNodes; j++) {
if (adjMatrix[i][j] === 1) {
node.children.push({
name: ` ${j + 1}`
});
}
}
treeData.children.push(node);
}
return treeData;
}
function readFile() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const csvText = e.target.result;
const adjMatrix = parseCSV(csvText);
generateRT(adjMatrix);
};
reader.readAsText(file);
} else {
alert('Please select a file.');
}
}
function parseCSV(csvText) {
// Assuming CSV format with rows and columns separated by commas
const rows = csvText.split('\n');
const adjMatrix = [];
for (let i = 0; i < rows.length; i++) {
const cols = rows[i].split(',');
const rowValues = cols.map(value => parseInt(value.trim(), 10));
adjMatrix.push(rowValues);
}
return adjMatrix;
}
</script>
</body>
</html>