From f31ed7d781fb36d9b4debe7287b11badd8d7f75a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 24 Aug 2021 19:07:19 +0200 Subject: add legend, filter visible nodes --- src/graph.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/graph.js') diff --git a/src/graph.js b/src/graph.js index 4ed953a..640b42e 100644 --- a/src/graph.js +++ b/src/graph.js @@ -102,6 +102,34 @@ export default function buildGraph(db, handlers) { }) .onNodeClick(handlers.click); + const handleSelect = (category) => { + if (!category) { + graph.graphData(data); + return; + } + const { nodes, links } = data; + const visible = new Set(); + + const selectedData = {}; + selectedData.nodes = nodes.filter((node) => { + for (let tagIndex = 0; tagIndex < 8; tagIndex += 1) { + const group = node.data["tag_" + tagIndex]; + if (!group) continue; + if (group === category) { + visible.add(node.id); + return true; + } + } + return false; + }); + selectedData.links = links.filter((link) => { + const { source, target } = link; + return visible.has(source.id) && visible.has(target.id); + }); + console.log(selectedData); + graph.graphData(selectedData); + }; + // graph.d3Force("charge").strength(-150); // camera orbit @@ -112,6 +140,9 @@ export default function buildGraph(db, handlers) { z: distance * Math.cos(angle), }); + return { + onSelect: handleSelect, + }; // stars(); } -- cgit v1.2.3-70-g09d2