-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.html
46 lines (39 loc) · 1.89 KB
/
filter.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
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<input type="range" min="0" max="10" step="0.1" value="6" id="link-filter" style="width: 600px; display: block" />
<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 fetch("country-religion.json").then((r) => r.json());
const { nodes, links } = kpartite(
data,
{ country: "Country", religion: "Religion" },
{ count: 1, Population: "Value" },
);
function draw() {
// Filter the data
const minPopulation = Math.pow(10, document.querySelector("#link-filter").value);
const filteredLinks = links.filter((d) => d.Population >= minPopulation);
const filteredNodes = nodes.filter((d) => filteredLinks.some((l) => l.source == d || l.target == d));
// Create the network
const graph = network("#network", { nodes: filteredNodes, links: filteredLinks, d3 });
// Style the network
const rScale = d3
.scaleLinear()
.domain(d3.extent(nodes, (d) => d.Population))
.range([3, 20]);
graph.nodes
.attr("fill", (d) => (d.key == "country" ? "rgba(255,0,0,0.5)" : "rgba(0,0,255,0.5)"))
.attr("r", (d) => rScale(d.Population))
.attr("data-bs-toggle", "tooltip")
.attr("title", (d) => d.value);
graph.links.attr("stroke", "rgba(0,0,0,0.2)");
}
draw();
document.querySelector("#link-filter").addEventListener("input", draw);
new bootstrap.Tooltip("#network", { selector: '[data-bs-toggle="tooltip"]' });
</script>