Repository navigation
Expand file tree
/
Copy pathscript_graph.js
More file actions
429 lines (376 loc) · 15.9 KB
/
Copy pathscript_graph.js
File metadata and controls
429 lines (376 loc) · 15.9 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
const nodeToBoxMap = {
"Leadership": "professional-item",
"Collaboration": "professional-item",
"Networking": "contact-item", // Assuming networking links to contact
"Computer Science": "education-item",
"Data Science": "education-item",
"Machine Learning": "education-item",
"Artificial Intelligence": "research-item",
"Natural Language Processing": "research-item",
"Computer Vision": "research-item",
"Python": "technologies-item",
"JavaScript": "technologies-item",
"SQL": "technologies-item"
};
class SkillsNetwork {
constructor(containerId) {
this.container = d3.select(containerId);
// Force a reflow to ensure we get correct dimensions
this.container.node().offsetHeight;
const bounds = this.container.node().getBoundingClientRect();
this.width = bounds.width;
this.height = bounds.height;
this.svg = this.container.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.attr("preserveAspectRatio", "xMidYMid meet")
.attr("viewBox", `0 0 ${this.width} ${this.height}`);
this.g = this.svg.append("g");
this.simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(d => d.id).distance(80))
.force("charge", d3.forceManyBody().strength(-98))
.force("center", d3.forceCenter(this.width / 2, this.height / 2))
.alphaTarget(0)
.alphaDecay(0.1)
.alphaMin(0.001);
this.zoom = d3.zoom()
.scaleExtent([0.1, 4])
.on("zoom", (event) => {
this.g.attr("transform", event.transform);
});
this.svg.call(this.zoom);
// Add a resize handler
window.addEventListener('resize', () => {
const bounds = this.container.node().getBoundingClientRect();
this.width = bounds.width;
this.height = bounds.height;
this.svg
.attr("viewBox", `0 0 ${this.width} ${this.height}`);
// Update force simulation center
this.simulation.force("center", d3.forceCenter(this.width / 2, this.height / 2));
this.simulation.alpha(0.3).restart();
});
}
update(data) {
console.log("Data received:", data);
this.g.selectAll("*").remove();
const links = this.g.append("g")
.selectAll("line")
.data(data.links)
.enter().append("line")
.attr("class", "link");
const nodes = this.g.append("g")
.selectAll("g")
.data(data.nodes)
.enter().append("g")
.attr("class", "node")
.attr("id", d => d.name.toLowerCase().replace(/\s+/g, '-'))
.call(d3.drag()
.on("start", (event) => this.dragstarted(event))
.on("drag", (event) => this.dragged(event))
.on("end", (event) => this.dragended(event)))
.on("mouseover", (event, d) => {
this.showTooltip(event, d);
// Add movement effect on hover
this.simulation
.force("charge")
.strength(d => {
const isHovered = d === event.target.__data__;
return isHovered ? -150 : -98;
});
// Add random movement on hover
this.simulation.force("random", () => {
this.simulation.nodes().forEach(node => {
node.x += (Math.random() - 0.5) * 0.4;
node.y += (Math.random() - 0.5) * 0.4;
});
});
this.simulation.alphaTarget(0.3).restart();
})
.on("mouseout", () => {
this.hideTooltip();
// Remove random force and reset charge
this.simulation.force("random", null);
this.simulation
.force("charge")
.strength(-98);
this.simulation.alphaTarget(0).restart();
});
nodes.append("circle")
.attr("r", 4);
nodes.append("text")
.attr("dx", 8)
.attr("dy", ".35em")
.style("font-weight", 400)
.style("fill", "white")
.style("font-size", "10px")
.text(d => d.name)
.attr("text-anchor", "start");
this.simulation
.nodes(data.nodes)
.on("tick", () => {
links
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
nodes
.attr("transform", d => `translate(${d.x},${d.y})`);
});
this.simulation.force("link")
.links(data.links);
}
showTooltip(event, d) {
console.log("Tooltip data:", d);
const tooltip = d3.select("#tooltip");
tooltip.style("visibility", "visible")
.text(d.name)
.style("left", (event.pageX + 5) + "px")
.style("top", (event.pageY + 5) + "px");
}
hideTooltip() {
d3.select("#tooltip").style("visibility", "hidden");
}
dragstarted(event) {
if (!event.active) this.simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
dragended(event) {
if (!event.active) this.simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
nodeClicked(d) {
const relatedBoxId = nodeToBoxMap[d.name];
if (relatedBoxId) {
const relatedBox = document.getElementById(relatedBoxId);
if (relatedBox) {
relatedBox.scrollIntoView({ behavior: 'smooth' });
}
}
}
}
function parseGraphData(text) {
const nodes = new Set();
const links = new Map();
const skillRegex = /\[(.*?)\]/g;
text.split('\n').forEach(line => {
const matches = line.match(skillRegex);
if (matches) {
matches.forEach(match => {
const skill = match.slice(1, -1);
nodes.add(skill);
});
for (let i = 0; i < matches.length - 1; i++) {
const source = matches[i].slice(1, -1);
const target = matches[i + 1].slice(1, -1);
const linkKey = `${source}-${target}`;
links.set(linkKey, {
source: source,
target: target
});
}
}
});
return {
nodes: Array.from(nodes).map((name, index) => ({ id: String(index), name: name })),
links: Array.from(links.values()).map(link => ({
source: String(Array.from(nodes).indexOf(link.source)),
target: String(Array.from(nodes).indexOf(link.target))
}))
};
}
// --- Defer Initialization ---
function initializeGraph() {
// Initialize the network
const network = new SkillsNetwork("#skills-network");
// Load and parse the graph_words.txt file
fetch('graph_words.txt')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.text();
})
.then(text => {
if (text.trim() === "") {
console.warn("graph_words.txt is empty. No graph data to load.");
return; // Don't try to parse empty data
}
const skillsData = parseGraphData(text);
if (skillsData && skillsData.nodes && skillsData.links) {
console.log("Updating graph with data:", skillsData);
network.update(skillsData);
} else {
console.error("Parsed skills data is invalid:", skillsData);
}
})
.catch(error => console.error('Error loading or parsing graph data:', error));
}
// Wait for the DOM to be ready, then wait for the next animation frame
document.addEventListener('DOMContentLoaded', function() {
requestAnimationFrame(initializeGraph);
// --- Keep other DOMContentLoaded logic separate ---
// const boxes = document.querySelectorAll('.grid-container .bento-box'); // Commented out as .bento-box class is not used
// boxes.forEach(box => {
// box.addEventListener('click', function() {
// const link = this.querySelector('.view-link');
// // If this box is already expanded, collapse it and change text back to "view"
// if (this.classList.contains('expanded')) {
// this.classList.remove('expanded');
// if (link) {
// link.textContent = "view"; // Change text back to "View"
// link.style.color = "var(--color-link)"; // Set color to link color
// }
// return;
// }
// // Collapse any other expanded box and change their text back to "view"
// boxes.forEach(b => {
// b.classList.remove('expanded');
// const otherLink = b.querySelector('.view-link');
// if (otherLink) {
// otherLink.textContent = "view"; // Reset other boxes to "View"
// otherLink.style.color = "var(--color-link)"; // Set color to link color
// }
// });
// // Expand this box and change text to "Go Back"
// this.classList.add('expanded');
// if (link) {
// link.textContent = "Go Back"; // Change text to "Go Back"
// link.style.color = "var(--color-text)"; // Set color to white
// }
// });
// });
// initializeExpandableBoxes(); // Commented out as this seems to be for a different layout
});
// Example distribution of words into categories
// const wordDistribution = { // Commented out as it's not used
// professional: ["Leadership", "Collaboration", "Networking"],
// education: ["Computer Science", "Data Science", "Machine Learning"],
// research: ["Artificial Intelligence", "Natural Language Processing", "Computer Vision"],
// technologies: ["Python", "JavaScript", "SQL"]
// };
// Update the content dynamically based on the distribution
// function updateContent() {
// Object.entries(wordDistribution).forEach(([category, words]) => {
// const section = document.getElementById(category);
// if (section) { // Check if section exists
// const ul = section.querySelector('ul');
// if (ul) { // Check if ul exists
// ul.innerHTML = words.map(word => `<li>${word}</li>`).join('');
// }
// }
// });
// }
// Call this function after loading the PDF and processing the words
// document.addEventListener('DOMContentLoaded', () => {
// updateContent(); // Call your function here
// });
// console.log(this.wordCloud); // Check if this is null // Removed as wordCloud is not defined
document.addEventListener('DOMContentLoaded', () => {
const professionalItemBox = document.getElementById('professional-item'); // Corrected ID
const pdfIframe = document.getElementById('professional-pdf');
const link = document.getElementById('professional-link'); // Get the link element
if (professionalItemBox && pdfIframe && link) { // Check if all elements exist
// Set the title for the Professional box
const titleElement = professionalItemBox.querySelector('h3');
if (titleElement) {
titleElement.textContent = "Professional"; // Set title to "Professional"
}
// Initially hide the PDF iframe
pdfIframe.style.display = "none";
professionalItemBox.addEventListener('click', (event) => { // Use corrected variable name
// Prevent click on link from toggling PDF
if (event.target.tagName === 'A' || event.target.id === 'professional-link') {
return;
}
// Check if the PDF iframe is currently displayed
if (pdfIframe.style.display === "none" || pdfIframe.style.display === "") {
pdfIframe.style.display = "block"; // Show the PDF
link.textContent = "Go Back"; // Change link text to "Go Back"
} else {
pdfIframe.style.display = "none"; // Hide the PDF
link.textContent = "View"; // Reset link text to "View"
}
});
}
});
// function updateWordCloud(words) { // Commented out as 'word-cloud' element doesn't exist
// const wordCloud = document.getElementById('word-cloud');
// if (wordCloud) {
// wordCloud.innerHTML = ""; // Clear existing words
//
// Object.entries(words).forEach(([word, count]) => {
// const wordElement = document.createElement('span');
// wordElement.textContent = word;
// wordElement.className = 'word';
// wordElement.style.fontSize = `${calculateFontSize(count)}px`; // Adjust font size
//
// wordCloud.appendChild(wordElement); // Append the word to the container
// });
// }
// }
// Example function to calculate font size
// function calculateFontSize(count) { // Commented out as it's part of updateWordCloud
// return Math.max(10, 20 - count); // Example logic for font size
// }
// document.querySelectorAll('.professional-box').forEach(box => { // Commented out as '.professional-box' class doesn't exist
// box.addEventListener('click', function() {
// this.classList.toggle('expanded');
// console.log('Class toggled:', this.classList.contains('expanded')); // This will log true or false
// });
// });
// document.querySelectorAll('.box').forEach(box => { // Commented out as '.box' class doesn't exist
// box.addEventListener('click', function() {
// // Remove active class from all boxes
// document.querySelectorAll('.box').forEach(b => b.classList.remove('active'));
//
// // Add active class to the clicked box
// this.classList.add('active');
// });
// });
// function initializeExpandableBoxes() { // Commented out as this seems to be for a different layout
// // Create overlay
// const overlay = document.createElement('div');
// overlay.className = 'overlay';
// document.body.appendChild(overlay);
//
// // Get all grid item titles
// const titles = document.querySelectorAll('.grid-item h1, .grid-item h3');
//
// titles.forEach(title => {
// title.addEventListener('click', (e) => {
// e.stopPropagation(); // Prevent event bubbling
// const box = title.closest('.grid-item');
//
// // If already expanded, collapse
// if (box.classList.contains('expanded')) {
// box.classList.remove('expanded');
// overlay.classList.remove('active');
// return;
// }
//
// // Collapse any other expanded boxes
// document.querySelectorAll('.grid-item.expanded').forEach(expandedBox => {
// expandedBox.classList.remove('expanded');
// });
//
// // Expand this box
// box.classList.add('expanded');
// overlay.classList.add('active');
// });
// });
//
// // Close on overlay click
// overlay.addEventListener('click', () => {
// document.querySelectorAll('.grid-item.expanded').forEach(box => {
// box.classList.remove('expanded');
// });
// overlay.classList.remove('active');
// });
// }