-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends.html
39 lines (33 loc) · 1.66 KB
/
friends.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
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<svg id="network" width="600" height="380"></svg>
<script type="module">
/* globals bootstrap */
import { network } from "https://cdn.jsdelivr.net/npm/@gramex/network@2";
import { kpartite } from "https://cdn.jsdelivr.net/npm/@gramex/network@2/dist/kpartite.js";
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm";
// Load the data
const data = await d3.csv("friends-sexual-partners.csv");
// Identify the main characters separately using the "Character" column
const characters = new Set(data.map((d) => d.Character));
// Create nodes and links, mapping the "name" key with both "Character" and "Partner" columns
const { nodes, links } = kpartite(
data,
[["name", "Character"], ["name", "Partner"]],
{ count: 1 },
);
// Scale the radius by count of sexual partners
const radius = d3.scaleSqrt().range([4, 40]).domain(d3.extent(nodes, d => d.count));
// Create the network ensuring nodes don't collide
const forces = { collide: () => d3.forceCollide().radius(d => radius(d.count) + 2) };
const graph = network("#network", { nodes, links, forces, d3 });
// Style the network
graph.nodes
.attr("fill", d => characters.has(d.value) ? "red" : "blue")
.attr("stroke", "white")
.attr("r", d => radius(d.count))
.attr("data-bs-toggle", "tooltip")
.attr("title", (d) => d.value);
graph.links.attr("stroke", "rgba(0,0,0,0.2)");
new bootstrap.Tooltip("#network", { selector: '[data-bs-toggle="tooltip"]' });
</script>