-
Notifications
You must be signed in to change notification settings - Fork 9
/
denote-explore-network.html
executable file
·776 lines (669 loc) · 25.6 KB
/
denote-explore-network.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Denote Explore {{graph-type}} Network</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f8f8f8;
}
svg {
border: 0px solid #ccc;
z-index: 1;
}
.tooltip {
position: absolute;
background-color: white;
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
pointer-events: none;
line-height: 1.4;
max-width: 600px;
z-index: 1000;
}
.lighter {
color: #888;
}
.tooltip .title {
font-weight: bold;
margin-bottom: 10px;
font-size: 28px;
}
.tooltip .keywords {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-top: 5px;
}
.tooltip .keywords span {
background-color: #e0e7ff;
color: #3b82f6;
padding: 2px 8px;
border-radius: 4px;
font-size: 18px;
}
.tooltip .metadata {
margin-top: 10px;
font-size: 18px;
color: #333;
}
.hidden {
display: none;
}
.info-button {
position: fixed;
top: 10px;
left: 10px;
width: 50px;
height: 50px;
background-color: #007BFF;
color: white;
font-size: 24px;
font-weight: bold;
text-align: center;
line-height: 50px;
border-radius: 25px;
cursor: pointer;
z-index: 1000;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
transition: transform 0.2s, box-shadow 0.2s;
}
.info-button:hover {
transform: scale(1.1); /* Slightly enlarges on hover */
box-shadow: 0px 6px 8px rgba(0, 0, 0, 0.3); /* More pronounced shadow */
}
.info-button:active {
transform: scale(0.95); /* Slightly shrinks when clicked */
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.3); /* Subtle shadow on click */
}
#info-button:hover .info-tooltip {
display: block; /* Show on hover */
}
.info-tooltip {
position: absolute;
top: 60px;
left: 10px;
background-color: white;
color: black;
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
max-width: 600px; /* Prevents it from growing too wide */
overflow: auto;
display: none;
z-index: 99999;
}
.info-tooltip .title {
font-weight: bold;
margin-bottom: 10px;
font-size: 24px;
}
.info.tooltip .metadata {
margin-top: 10px;
font-size: 22px;
color: #555;
}
#keyword-chart svg {
font-family: Arial, sans-serif;
}
.legend-container .chart-container {
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #f8f8f8;
}
.metadata-container {
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #f8f8f8; /* Matches the existing tooltip and legend background */
font-size: 18px; /* Matches the metadata text size */
}
.metadata {
margin-bottom: 10px; /* Adjust spacing as needed */
font-size: 18px; /* Optional: Adjust font size */
}
</style>
</head>
<body>
<!-- Info Button -->
<div id="info-button" class="info-button" title="Click for more information">ℹ</div>
<!-- Tooltip Container -->
<div class="info-tooltip"></div>
</div>
<script src="{{d3-js}}"></script>
<script>
const data = {{json-content}}
const width = 1440;
const height = 900;
const fontSize = 18;
const color = d3.scaleOrdinal(d3.{{d3-colourscheme}});
const links = data.edges; // Array of edges
const nodes = data.nodes; // Array of nodes
// Variable to hold the current minimum degree tag value
let minDegreeTag = 2;
// Create a tooltip div
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip hidden");
// Create a zoom behaviour
const zoom = d3.zoom()
.scaleExtent([0.1, 4]) // Zoom scale limits
.on("zoom", (event) => {
svgGroup.attr("transform", event.transform); // Apply zoom
});
// Create the SVG container with zoom functionality
const svg = d3.select('body').append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;")
.call(zoom);
const svgGroup = svg.append("g"); // Group to apply zoom transformations
const radiusScale = d3.scaleLinear()
.domain(d3.extent(nodes, d => d.degree)) // Node degree range
.range([7, 30]); // Radius range for nodes
// Define a weight scale for edge stroke width (Keywords only)
const isKeywordType = data.meta.type === "Keywords";
const maxWeight = d3.max(links, d => d.weight || 1);
// Define the weight scale with interpolation
const weightScale = d3.scaleLinear()
.domain([1, maxWeight]) // Map the range of weights
.range(isKeywordType ? [2, 30] : [1, 1]); // Adjust the range
const arrowMarkers = svg.append("defs");
// Pre-calculate refX for edges
links.forEach((d, i) => {
const targetNode = nodes.find(node => node.id === d.target);
const targetRadius = isKeywordType ? 15 : radiusScale(targetNode.degree); // Use constant radius if isKeywordType is true
d.refX = targetRadius + 2; // Dynamically calculate refX for alignment, with padding
});
// Define arrow markers dynamically based on line weight
links.forEach((d, i) => {
arrowMarkers.append("marker")
.attr("id", `arrow-${i}`)
.attr("viewBox", "0 -5 10 10")
.attr("refX", d.refX) // Use the pre-calculated refX
.attr("refY", 0)
.attr("markerWidth", 10) // Adjust arrow size
.attr("markerHeight", 5) // Adjust arrow size
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5") // Arrow shape
.attr("fill", "#999")
.attr("stroke", "none");
});
// Add links and use dynamically created markers
const link = svgGroup.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
// Binding and joining data
.selectAll("line")
.data(links)
.join("line")
// Styling the links
.attr("stroke-width", d => weightScale(d.weight || 1))
// Add arrow markers
.attr("marker-end", (d, i) => data.meta.directed ? `url(#arrow-${i})` : null)
// Add hover interaction for edges
.on("mouseover", (event, d) => {
if (data.meta.type === "Keywords") {
// Generate content for the tooltip
const tooltipContent = `
<div class="metadata">From: ${d.source.name}</div>
<div class="metadata">To: ${d.target.name}</div>
<div class="metadata">Weight: ${d.weight || 'undefined'}</div>
`;
tooltip
.classed("hidden", false)
.html(tooltipContent)
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY + 10) + "px");
}
})
.on("mouseout", () => {
tooltip.classed("hidden", true);
});
// Add nodes
const node = svgGroup.append("g")
.attr("stroke", "none")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => isKeywordType ? 15 : radiusScale(d.degree))
.attr("fill", d => color(d.type))
.on("click", (event, d) => {
if (!isKeywordType) { // Only open hyperlink if isKeywordType is false
window.open(d.filename, "_blank");
}
})
.on("mouseover", (event, d) => {
// Check if the filename is an image or a PDF
const isImage = /\.(jpg|jpeg|png|gif|svg)$/i.test(d.filename);
const isPDF = /\.pdf$/i.test(d.filename);
// Generate tooltip content based on meta.type
const tooltipContent = data.meta.type === "Keywords"
? `
<div class="title">${d.name}</div>
<div class="metadata">Degree: ${d.degree}</div>
`:
// Tooltip for other types
`
<div class="title">${d.name}</div>
<div class="metadata">ID: ${d.id.replace('T', '<span class="lighter">T</span>')}</div>
<div class="metadata">Degree: ${d.degree}, Backlinks: ${d.backlinks}</div>
<div class="metadata">Type: ${d.type}</div>
${d.signature ? `<div class="metadata">Signature: ${d.signature}</div>` : ''}
<div class="keywords">${d.keywords && d.keywords.length > 0 ? d.keywords.map(keyword =>
`<span>${keyword}</span>`).join('') :
'<div class="metadata">No keywords</div>'}</div>
${isImage
? `<img src="${d.filename}" alt="${d.name}"
onload="if(this.naturalHeight > this.naturalWidth) this.style.maxHeight='500px';"
style="max-width: 100%; height: auto; margin-top: 10px;">`
: isPDF
? `<iframe src="${d.filename}?toolbar=0&navpanes=0&scrollbar=0#view=FitH"
style="width: 100%; height: 500px; margin-top: 10px; border: none;"></iframe>`
: ''
}
`;
// Display the tooltip and set its initial content
tooltip
.classed("hidden", false)
.html(tooltipContent);
// Calculate tooltip dimensions
const tooltipNode = tooltip.node();
const tooltipWidth = tooltipNode.offsetWidth;
const tooltipHeight = tooltipNode.offsetHeight;
// Get viewport dimensions
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// Calculate mouse position
let tooltipX = event.pageX + 10; // Default offset
let tooltipY = event.pageY + 10;
// Adjust position to fit within the viewport
if (tooltipX + tooltipWidth > viewportWidth) {
tooltipX = viewportWidth - tooltipWidth - 10; // Align with the right edge
}
if (tooltipY + tooltipHeight > viewportHeight) {
tooltipY = viewportHeight - tooltipHeight - 10; // Align with the bottom edge
}
// Set tooltip position
tooltip
.style("left", `${tooltipX}px`)
.style("top", `${tooltipY}px`);
})
.on("mouseout", () => {
tooltip.classed("hidden", true);
});
// Add drag behavior
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
// Add labels for nodes with degree > minDegreeTag
const labels = svgGroup.append("g")
.selectAll("text")
.data(nodes.filter(d => d.degree > minDegreeTag)) // Filter nodes based on degree
.join("text")
.attr("dx", 10)
.attr("dy", ".35em")
.style("font-size", `${fontSize}px`)
.style("font-family", "Arial, sans-serif")
.style("fill", "#333")
.text(d => d.name);
// Set up force simulation
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(20)) // Link force
.force("charge", d3.forceManyBody().strength(-20)) // Repel nodes
.force("center", d3.forceCenter(width / 2, height / 2)) // Centering force
.force("collide", d3.forceCollide(50)) // Spread out nodes
.force("x", d3.forceX(width / 2).strength(0.05)) // Keep nodes near center
.force("y", d3.forceY(height / 2).strength(0.05))
.on("tick", ticked);
// Update positions on each tick
function ticked() {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => {
const dx = d.target.x - d.source.x;
const dy = d.target.y - d.source.y;
const distance = Math.sqrt(dx * dx + dy * dy) || 1; // Avoid division by zero
const targetRadius = isKeywordType ? 15 : radiusScale(d.target.degree);
const offsetX = (dx / distance) * targetRadius;
return d.target.x - offsetX;
})
.attr("y2", d => {
const dx = d.target.x - d.source.x;
const dy = d.target.y - d.source.y;
const distance = Math.sqrt(dx * dx + dy * dy) || 1; // Avoid division by zero
const targetRadius = radiusScale(d.target.degree); // Exact node radius
const offsetY = (dy / distance) * targetRadius;
return d.target.y - offsetY;
});
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
labels
.attr("x", d => d.x)
.attr("y", d => d.y);
}
// Drag event handlers
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
const infoButton = document.getElementById("info-button");
const infoTooltip = document.querySelector(".info-tooltip");
// Generate Tooltip Content
function generateTooltipContent() {
infoTooltip.innerHTML = "";
// Add title
const title = document.createElement("div");
title.className = "title";
title.textContent = `Denote ${data.meta.type} Network`;
infoTooltip.appendChild(title);
// Add metadata container
const metadataContainer = document.createElement("div");
metadataContainer.className = "metadata-container";
if (data.meta.type === "Community") {
const graphParameter = document.createElement("div");
graphParameter.className = "metadata";
graphParameter.textContent = `Regular expression: "${data.meta.parameters[0]}"`;
metadataContainer.appendChild(graphParameter);
}
if (data.meta.type === "Keywords") {
const graphParameter = document.createElement("div");
graphParameter.className = "metadata";
graphParameter.textContent = `Minimum links: ${data.meta.parameters}`;
metadataContainer.appendChild(graphParameter);
}
if (data.meta.type === "Neighbourhood") {
const graphParameter = document.createElement("div");
graphParameter.className = "metadata";
const rootLine = document.createElement("div");
rootLine.textContent = `Root: ${data.meta.parameters[0]}`;
rootLine.style.marginBottom = "5px";
graphParameter.appendChild(rootLine);
const depthLine = document.createElement("div");
depthLine.textContent = `Depth: ${data.meta.parameters[1]}`;
graphParameter.appendChild(depthLine);
metadataContainer.appendChild(graphParameter);
}
// Number of nodes
const nodeCount = document.createElement("div");
nodeCount.className = "metadata";
nodeCount.textContent = `${data.nodes.length} nodes`;
metadataContainer.appendChild(nodeCount);
// Number of edges
const edgeCount = document.createElement("div");
edgeCount.className = "metadata";
edgeCount.textContent = `${data.edges.length} ${data.edges.length === 1 ? "edge" : "edges"}`;
metadataContainer.appendChild(edgeCount);
// Network Density
const density = document.createElement("div");
density.className = "metadata";
const edges = data.edges.length;
const nodes = data.nodes.length;
if (nodes > 1) {
// Number of actual edges divided by the possible edges
const calculatedDensity = (2 * edges) / (nodes * (nodes - 1));
density.textContent = `Density: ${calculatedDensity.toFixed(3)}`;
} else {
density.textContent = "Density: N/A (insufficient nodes)";
}
metadataContainer.appendChild(density);
infoTooltip.appendChild(metadataContainer);
// Calculate the number of singular nodes
const singularNodeCount = data.nodes.filter(node => node.degree === 0).length;
// Add a toggle button
if (data.meta.type === "Community" && singularNodeCount !== 0) {
const buttonContainer = document.createElement("div");
buttonContainer.className = "metadata-container";
const toggleButton = document.createElement("button");
toggleButton.textContent = "Hide isolated nodes";
toggleButton.style.marginTop = "10px";
toggleButton.style.padding = "10px 20px";
toggleButton.style.borderRadius = "5px";
toggleButton.style.border = "1px solid #darkgrey";
toggleButton.style.backgroundColor = "darkgrey";
toggleButton.style.color = "white";
toggleButton.style.cursor = "pointer";
toggleButton.style.fontSize = "18px";
// Toggle button click event
toggleButton.addEventListener("click", () => {
// Find all nodes with degree = 0
const isolatedNodes = d3.selectAll("circle")
.filter(d => d.degree === 0);
// Determine if they are currently visible
const isVisible = isolatedNodes.style("display") !== "none";
// Toggle their visibility
isolatedNodes.style("display", isVisible ? "none" : "block");
// Update the button text
toggleButton.textContent = isVisible ? "Show Isolated Nodes" : "Hide Isolated Nodes";
});
// Create a comment element to display the count
const singularNodeComment = document.createElement("div");
singularNodeComment.className = "metadata";
singularNodeComment.textContent = `${singularNodeCount} isolated notes`;
// Append this comment above the toggle button
buttonContainer.appendChild(singularNodeComment);
buttonContainer.appendChild(toggleButton);
infoTooltip.appendChild(buttonContainer);
}
// Add legend container
const legendContainer = document.createElement("div");
legendContainer.className = "metadata-container";
legendContainer.style.marginTop = "10px";
const legendTitle = document.createElement("div");
legendTitle.style.fontWeight = "bold";
legendTitle.style.marginBottom = "10px";
legendTitle.textContent = "File Types";
legendContainer.appendChild(legendTitle);
// Generate legend and bar chart dynamically for non-keyword types
if (!isKeywordType) {
const typeCounts = data.nodes.reduce((acc, node) => {
acc[node.type] = (acc[node.type] || 0) + 1;
return acc;
}, {});
const legendData = Object.entries(typeCounts).map(([type, count]) => ({
type,
count,
}));
legendData.forEach((entry) => {
const legendItem = document.createElement("div");
legendItem.style.display = "flex";
legendItem.style.alignItems = "center";
legendItem.style.marginBottom = "5px";
const colorBox = document.createElement("div");
colorBox.style.width = "15px";
colorBox.style.height = "15px";
colorBox.style.marginRight = "10px";
colorBox.style.backgroundColor = d3.{{d3-colourscheme}}[legendData.indexOf(entry)];
legendItem.appendChild(colorBox);
const text = document.createElement("span");
text.style.fontSize = `${fontSize}px`;
text.textContent = `${entry.type} (${entry.count})`;
legendItem.appendChild(text);
legendContainer.appendChild(legendItem);
});
infoTooltip.appendChild(legendContainer);
// Add keyword frequency chart container
const chartContainer = document.createElement("div");
chartContainer.className = "metadata-container"; // Match the metadata background style
chartContainer.style.marginTop = "10px"; // Add spacing between sections
const chartTitle = document.createElement("div");
chartTitle.style.fontWeight = "bold";
chartTitle.style.marginBottom = "10px";
chartTitle.textContent = "Keywords";
chartContainer.appendChild(chartTitle);
// Bar chart
const keywordCounts = data.nodes.reduce((acc, node) => {
(node.keywords || []).forEach((keyword) => {
acc[keyword] = (acc[keyword] || 0) + 1;
});
return acc;
}, {});
const keywordData = Object.entries(keywordCounts).map(([keyword, count]) => ({
keyword,
count,
}));
// Sort keywordData by count in descending order
keywordData.sort((a, b) => b.count - a.count);
// Define constants
const barHeight = 20; // Height of each bar
const barSpacing = 5; // Space between bars
const chartWidth = 400;
const margin = { top: 20, right: 20, bottom: 30, left: 20 }; // Initial margins
// Create a temporary SVG to measure label dimensions
const tempSVG = d3.select("body").append("svg").attr("class", "temp-svg");
// Calculate max label width for y-axis
const maxYLabelWidth = d3.max(keywordData, (d) => {
const textElement = tempSVG
.append("text")
.attr("class", "temp-text")
.style("font-size", `${fontSize}px`)
.text(d.keyword);
const textWidth = textElement.node().getBBox().width;
textElement.remove();
return textWidth;
});
// Calculate max label height for x-axis
const maxXLabelHeight = d3.max(keywordData, (d) => {
const textElement = tempSVG
.append("text")
.attr("class", "temp-text")
.style("font-size", `${fontSize}px`)
.text(d.count * 2);
const textHeight = textElement.node().getBBox().height;
textElement.remove();
return textHeight;
});
tempSVG.remove(); // Clean up the temporary SVG
// Update margins dynamically
margin.left = maxYLabelWidth + 20; // Add padding
margin.bottom = maxXLabelHeight + 10; // Add padding
// Compute chart height based on number of bars
const chartHeight = keywordData.length * (barHeight + barSpacing) + margin.top + margin.bottom;
// Update scales
const x = d3
.scaleLinear()
.domain([0, d3.max(keywordData, (d) => d.count)])
.range([0, chartWidth - margin.left - margin.right]);
const y = d3
.scaleBand()
.domain(keywordData.map((d) => d.keyword))
.range([0, keywordData.length * (barHeight + barSpacing)])
.padding(0.1); // Padding within the band
// Create SVG
const chartSVG = d3
.select(chartContainer)
.append("svg")
.attr("width", chartWidth)
.attr("height", chartHeight);
const chartGroup = chartSVG
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
// Add bars
chartGroup
.selectAll(".bar")
.data(keywordData)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", 0)
.attr("y", (d, i) => i * (barHeight + barSpacing)) // Use index to calculate position
.attr("width", (d) => x(d.count))
.attr("height", barHeight)
.attr("fill", "lightgrey");
// Add y-axis
chartGroup
.append("g")
.call(d3.axisLeft(y))
.selectAll("text")
.style("font-size", `${fontSize}px`);
// Add x-axis
const maxValue = d3.max(keywordData, d => d.count); // Get the maximum value
chartGroup
.append("g")
.attr("transform", `translate(0, ${chartHeight - margin.top - margin.bottom})`)
.call(d3.axisBottom(x).tickValues([0, maxValue])) // Set tick values to only 0 and max
.selectAll("text")
.style("font-size", `${fontSize}px`);
// Append the chart container to the tooltip or desired location
infoTooltip.appendChild(chartContainer);
}
}
// Toggle tooltip visibility on button click
infoButton.addEventListener("click", () => {
if (infoTooltip.style.display === "block") {
infoTooltip.style.display = "none";
} else {
generateTooltipContent();
infoTooltip.style.display = "block";
}
});
// Label density
// Add a slider element to the top-right corner
const sliderContainer = d3.select('body').append('div')
.style('position', 'fixed')
.style('top', '10px')
.style('right', '10px')
.style('z-index', '1000');
sliderContainer.append('label')
.text('Label density: ')
.style('margin-right', '10px');
const slider = sliderContainer.append('input')
.attr('type', 'range')
.attr('min', 0)
.attr('max', d3.max(nodes, d => d.degree))
.attr('value', minDegreeTag)
.style('width', '150px')
.on('input', function () {
minDegreeTag = d3.max(nodes, d => d.degree) - +this.value;
updateLabels();
});
// Function to update labels based on the slider value
function updateLabels() {
labels = svgGroup.selectAll('text')
.data(nodes.filter(d => d.degree > minDegreeTag), d => d.id)
.join(
enter => enter.append('text')
.attr('x', d => d.x)
.attr('y', d => d.y)
.attr('dx', 10)
.attr('dy', '.35em')
.style('font-size', `${fontSize}px`)
.style('font-family', 'Arial, sans-serif')
.style('fill', '#333')
.text(d => d.name),
update => update.attr('x', d => d.x).attr('y', d => d.y),
exit => exit.remove()
);
simulation.alpha(1).restart();
}
</script>
</body>
</html>