mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 05:14:06 -06:00
Navigate on click for graph
This commit is contained in:
parent
e87cab0707
commit
6ad4ac318d
@ -1,51 +1,67 @@
|
|||||||
async function drawGraph(url, baseUrl, pathColors, depth, enableDrag, enableLegend, enableZoom) {
|
async function drawGraph(
|
||||||
const { index, links, content } = await fetchData
|
url,
|
||||||
const curPage = url.replace(baseUrl, "")
|
baseUrl,
|
||||||
|
pathColors,
|
||||||
|
depth,
|
||||||
|
enableDrag,
|
||||||
|
enableLegend,
|
||||||
|
enableZoom
|
||||||
|
) {
|
||||||
|
const { index, links, content } = await fetchData;
|
||||||
|
const curPage = url.replace(baseUrl, '');
|
||||||
|
|
||||||
const parseIdsFromLinks = (links) => [...(new Set(links.flatMap(link => ([link.source, link.target]))))]
|
const parseIdsFromLinks = (links) => [
|
||||||
|
...new Set(links.flatMap((link) => [link.source, link.target])),
|
||||||
|
];
|
||||||
|
const copyLinks = JSON.parse(JSON.stringify(links));
|
||||||
|
|
||||||
const neighbours = new Set()
|
const neighbours = new Set();
|
||||||
const wl = [curPage || "/", "__SENTINEL"]
|
const wl = [curPage || '/', '__SENTINEL'];
|
||||||
if (depth >= 0) {
|
if (depth >= 0) {
|
||||||
while (depth >= 0 && wl.length > 0) {
|
while (depth >= 0 && wl.length > 0) {
|
||||||
// compute neighbours
|
// compute neighbours
|
||||||
const cur = wl.shift()
|
const cur = wl.shift();
|
||||||
if (cur === "__SENTINEL") {
|
if (cur === '__SENTINEL') {
|
||||||
depth--
|
depth--;
|
||||||
wl.push("__SENTINEL")
|
wl.push('__SENTINEL');
|
||||||
} else {
|
} else {
|
||||||
neighbours.add(cur)
|
neighbours.add(cur);
|
||||||
const outgoing = index.links[cur] || []
|
const outgoing = index.links[cur] || [];
|
||||||
const incoming = index.backlinks[cur] || []
|
const incoming = index.backlinks[cur] || [];
|
||||||
wl.push(...outgoing.map(l => l.target), ...incoming.map(l => l.source))
|
wl.push(
|
||||||
|
...outgoing.map((l) => l.target),
|
||||||
|
...incoming.map((l) => l.source)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
parseIdsFromLinks(links).forEach(id => neighbours.add(id))
|
parseIdsFromLinks(copyLinks).forEach((id) => neighbours.add(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
nodes: [...neighbours].map(id => ({ id })),
|
nodes: [...neighbours].map((id) => ({ id })),
|
||||||
links: links.filter(l => neighbours.has(l.source) && neighbours.has(l.target)),
|
links: copyLinks.filter(
|
||||||
}
|
(l) => neighbours.has(l.source) && neighbours.has(l.target)
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
const color = (d) => {
|
const color = (d) => {
|
||||||
if (d.id === curPage || (d.id === "/" && curPage === "")) {
|
if (d.id === curPage || (d.id === '/' && curPage === '')) {
|
||||||
return "var(--g-node-active)"
|
return 'var(--g-node-active)';
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const pathColor of pathColors) {
|
for (const pathColor of pathColors) {
|
||||||
const path = Object.keys(pathColor)[0]
|
const path = Object.keys(pathColor)[0];
|
||||||
const colour = pathColor[path]
|
const colour = pathColor[path];
|
||||||
if (d.id.startsWith(path)) {
|
if (d.id.startsWith(path)) {
|
||||||
return colour
|
return colour;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "var(--g-node)"
|
return 'var(--g-node)';
|
||||||
}
|
};
|
||||||
|
|
||||||
const drag = simulation => {
|
const drag = (simulation) => {
|
||||||
function dragstarted(event, d) {
|
function dragstarted(event, d) {
|
||||||
if (!event.active) simulation.alphaTarget(1).restart();
|
if (!event.active) simulation.alphaTarget(1).restart();
|
||||||
d.fx = d.x;
|
d.fx = d.x;
|
||||||
@ -63,158 +79,186 @@ async function drawGraph(url, baseUrl, pathColors, depth, enableDrag, enableLege
|
|||||||
d.fy = null;
|
d.fy = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const noop = () => { }
|
const noop = () => {};
|
||||||
return d3.drag()
|
return d3
|
||||||
.on("start", enableDrag ? dragstarted : noop)
|
.drag()
|
||||||
.on("drag", enableDrag ? dragged : noop)
|
.on('start', enableDrag ? dragstarted : noop)
|
||||||
.on("end", enableDrag ? dragended : noop);
|
.on('drag', enableDrag ? dragged : noop)
|
||||||
}
|
.on('end', enableDrag ? dragended : noop);
|
||||||
|
};
|
||||||
|
|
||||||
const height = 250
|
const height = 250;
|
||||||
const width = document.getElementById("graph-container").offsetWidth
|
const width = document.getElementById('graph-container').offsetWidth;
|
||||||
|
|
||||||
const simulation = d3.forceSimulation(data.nodes)
|
const simulation = d3
|
||||||
.force("charge", d3.forceManyBody().strength(-30))
|
.forceSimulation(data.nodes)
|
||||||
.force("link", d3.forceLink(data.links).id(d => d.id))
|
.force('charge', d3.forceManyBody().strength(-30))
|
||||||
.force("center", d3.forceCenter());
|
.force(
|
||||||
|
'link',
|
||||||
|
d3.forceLink(data.links).id((d) => d.id)
|
||||||
|
)
|
||||||
|
.force('center', d3.forceCenter());
|
||||||
|
|
||||||
const svg = d3.select('#graph-container')
|
const svg = d3
|
||||||
|
.select('#graph-container')
|
||||||
.append('svg')
|
.append('svg')
|
||||||
.attr('width', width)
|
.attr('width', width)
|
||||||
.attr('height', height)
|
.attr('height', height)
|
||||||
.attr("viewBox", [-width / 2, -height / 2, width, height]);
|
.attr('viewBox', [-width / 2, -height / 2, width, height]);
|
||||||
|
|
||||||
if (enableLegend) {
|
if (enableLegend) {
|
||||||
const legend = [
|
const legend = [
|
||||||
{ "Current": "var(--g-node-active)" },
|
{ Current: 'var(--g-node-active)' },
|
||||||
{ "Note": "var(--g-node)" },
|
{ Note: 'var(--g-node)' },
|
||||||
...pathColors
|
...pathColors,
|
||||||
]
|
];
|
||||||
legend.forEach((legendEntry, i) => {
|
legend.forEach((legendEntry, i) => {
|
||||||
const key = Object.keys(legendEntry)[0]
|
const key = Object.keys(legendEntry)[0];
|
||||||
const colour = legendEntry[key]
|
const colour = legendEntry[key];
|
||||||
svg.append("circle").attr("cx", -width / 2 + 20).attr("cy", height / 2 - 30 * (i + 1)).attr("r", 6).style("fill", colour)
|
svg
|
||||||
svg.append("text").attr("x", -width / 2 + 40).attr("y", height / 2 - 30 * (i + 1)).text(key).style("font-size", "15px").attr("alignment-baseline", "middle")
|
.append('circle')
|
||||||
})
|
.attr('cx', -width / 2 + 20)
|
||||||
|
.attr('cy', height / 2 - 30 * (i + 1))
|
||||||
|
.attr('r', 6)
|
||||||
|
.style('fill', colour);
|
||||||
|
svg
|
||||||
|
.append('text')
|
||||||
|
.attr('x', -width / 2 + 40)
|
||||||
|
.attr('y', height / 2 - 30 * (i + 1))
|
||||||
|
.text(key)
|
||||||
|
.style('font-size', '15px')
|
||||||
|
.attr('alignment-baseline', 'middle');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw links between nodes
|
// draw links between nodes
|
||||||
const link = svg.append("g")
|
const link = svg
|
||||||
.selectAll("line")
|
.append('g')
|
||||||
|
.selectAll('line')
|
||||||
.data(data.links)
|
.data(data.links)
|
||||||
.join("line")
|
.join('line')
|
||||||
.attr("class", "link")
|
.attr('class', 'link')
|
||||||
.attr("stroke", "var(--g-link)")
|
.attr('stroke', 'var(--g-link)')
|
||||||
.attr("stroke-width", 2)
|
.attr('stroke-width', 2)
|
||||||
.attr("data-source", d => d.source.id)
|
.attr('data-source', (d) => d.source.id)
|
||||||
.attr("data-target", d => d.target.id)
|
.attr('data-target', (d) => d.target.id);
|
||||||
|
|
||||||
// svg groups
|
// svg groups
|
||||||
const graphNode = svg.append("g")
|
const graphNode = svg
|
||||||
.selectAll("g")
|
.append('g')
|
||||||
|
.selectAll('g')
|
||||||
.data(data.nodes)
|
.data(data.nodes)
|
||||||
.enter().append("g")
|
.enter()
|
||||||
|
.append('g');
|
||||||
|
|
||||||
// draw individual nodes
|
// draw individual nodes
|
||||||
const node = graphNode.append("circle")
|
const node = graphNode
|
||||||
.attr("class", "node")
|
.append('circle')
|
||||||
.attr("id", (d) => d.id)
|
.attr('class', 'node')
|
||||||
.attr("r", (d) => {
|
.attr('id', (d) => d.id)
|
||||||
const numOut = index.links[d.id]?.length || 0
|
.attr('r', (d) => {
|
||||||
const numIn = index.backlinks[d.id]?.length || 0
|
const numOut = index.links[d.id]?.length || 0;
|
||||||
return 3 + (numOut + numIn) / 4
|
const numIn = index.backlinks[d.id]?.length || 0;
|
||||||
|
return 3 + (numOut + numIn) / 4;
|
||||||
})
|
})
|
||||||
.attr("fill", color)
|
.attr('fill', color)
|
||||||
.style("cursor", "pointer")
|
.style('cursor', 'pointer')
|
||||||
.on("click", (_, d) => {
|
.on('click', (_, d) => {
|
||||||
window.location.href = baseUrl + '/' + decodeURI(d.id).replace(/\s+/g, '-')
|
const url = baseUrl + decodeURI(d.id).replace(/\s+/g, '-');
|
||||||
|
navigate(new URL(url), '.singlePage');
|
||||||
})
|
})
|
||||||
.on("mouseover", function(_, d) {
|
.on('mouseover', function (_, d) {
|
||||||
d3.selectAll(".node")
|
d3.selectAll('.node')
|
||||||
.transition()
|
.transition()
|
||||||
.duration(100)
|
.duration(100)
|
||||||
.attr("fill", "var(--g-node-inactive)")
|
.attr('fill', 'var(--g-node-inactive)');
|
||||||
|
|
||||||
const neighbours = parseIdsFromLinks([...(index.links[d.id] || []), ...(index.backlinks[d.id] || [])])
|
const neighbours = parseIdsFromLinks([
|
||||||
const neighbourNodes = d3.selectAll(".node").filter(d => neighbours.includes(d.id))
|
...(index.links[d.id] || []),
|
||||||
const currentId = d.id
|
...(index.backlinks[d.id] || []),
|
||||||
const linkNodes = d3.selectAll(".link").filter(d => d.source.id === currentId || d.target.id === currentId)
|
]);
|
||||||
|
const neighbourNodes = d3
|
||||||
|
.selectAll('.node')
|
||||||
|
.filter((d) => neighbours.includes(d.id));
|
||||||
|
const currentId = d.id;
|
||||||
|
const linkNodes = d3
|
||||||
|
.selectAll('.link')
|
||||||
|
.filter((d) => d.source.id === currentId || d.target.id === currentId);
|
||||||
|
|
||||||
// highlight neighbour nodes
|
// highlight neighbour nodes
|
||||||
neighbourNodes
|
neighbourNodes.transition().duration(200).attr('fill', color);
|
||||||
.transition()
|
|
||||||
.duration(200)
|
|
||||||
.attr("fill", color)
|
|
||||||
|
|
||||||
// highlight links
|
// highlight links
|
||||||
linkNodes
|
linkNodes
|
||||||
.transition()
|
.transition()
|
||||||
.duration(200)
|
.duration(200)
|
||||||
.attr("stroke", "var(--g-link-active)")
|
.attr('stroke', 'var(--g-link-active)');
|
||||||
|
|
||||||
// show text for self
|
// show text for self
|
||||||
d3.select(this.parentNode)
|
d3.select(this.parentNode)
|
||||||
.select("text")
|
.select('text')
|
||||||
.raise()
|
.raise()
|
||||||
.transition()
|
.transition()
|
||||||
.duration(200)
|
.duration(200)
|
||||||
.style("opacity", 1)
|
.style('opacity', 1);
|
||||||
}).on("mouseleave", function(_, d) {
|
})
|
||||||
d3.selectAll(".node")
|
.on('mouseleave', function (_, d) {
|
||||||
.transition()
|
d3.selectAll('.node').transition().duration(200).attr('fill', color);
|
||||||
.duration(200)
|
|
||||||
.attr("fill", color)
|
|
||||||
|
|
||||||
const currentId = d.id
|
const currentId = d.id;
|
||||||
const linkNodes = d3.selectAll(".link").filter(d => d.source.id === currentId || d.target.id === currentId)
|
const linkNodes = d3
|
||||||
|
.selectAll('.link')
|
||||||
|
.filter((d) => d.source.id === currentId || d.target.id === currentId);
|
||||||
|
|
||||||
linkNodes
|
linkNodes.transition().duration(200).attr('stroke', 'var(--g-link)');
|
||||||
.transition()
|
|
||||||
.duration(200)
|
|
||||||
.attr("stroke", "var(--g-link)")
|
|
||||||
|
|
||||||
d3.select(this.parentNode)
|
d3.select(this.parentNode)
|
||||||
.select("text")
|
.select('text')
|
||||||
.transition()
|
.transition()
|
||||||
.duration(200)
|
.duration(200)
|
||||||
.style("opacity", 0)
|
.style('opacity', 0);
|
||||||
})
|
})
|
||||||
.call(drag(simulation));
|
.call(drag(simulation));
|
||||||
|
|
||||||
// draw labels
|
// draw labels
|
||||||
const labels = graphNode.append("text")
|
const labels = graphNode
|
||||||
.attr("dx", 12)
|
.append('text')
|
||||||
.attr("dy", ".35em")
|
.attr('dx', 12)
|
||||||
.text((d) => content[d.id]?.title || d.id.replace("-", " "))
|
.attr('dy', '.35em')
|
||||||
.style("opacity", 0)
|
.text((d) => content[d.id]?.title || d.id.replace('-', ' '))
|
||||||
.style("pointer-events", "none")
|
.style('opacity', 0)
|
||||||
|
.style('pointer-events', 'none')
|
||||||
.call(drag(simulation));
|
.call(drag(simulation));
|
||||||
|
|
||||||
// set panning
|
// set panning
|
||||||
|
|
||||||
if (enableZoom) {
|
if (enableZoom) {
|
||||||
svg.call(d3.zoom()
|
svg.call(
|
||||||
.extent([[0, 0], [width, height]])
|
d3
|
||||||
|
.zoom()
|
||||||
|
.extent([
|
||||||
|
[0, 0],
|
||||||
|
[width, height],
|
||||||
|
])
|
||||||
.scaleExtent([0.25, 4])
|
.scaleExtent([0.25, 4])
|
||||||
.on("zoom", ({ transform }) => {
|
.on('zoom', ({ transform }) => {
|
||||||
link.attr("transform", transform);
|
link.attr('transform', transform);
|
||||||
node.attr("transform", transform);
|
node.attr('transform', transform);
|
||||||
labels.attr("transform", transform);
|
labels.attr('transform', transform);
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// progress the simulation
|
// progress the simulation
|
||||||
simulation.on("tick", () => {
|
simulation.on('tick', () => {
|
||||||
link
|
link
|
||||||
.attr("x1", d => d.source.x)
|
.attr('x1', (d) => d.source.x)
|
||||||
.attr("y1", d => d.source.y)
|
.attr('y1', (d) => d.source.y)
|
||||||
.attr("x2", d => d.target.x)
|
.attr('x2', (d) => d.target.x)
|
||||||
.attr("y2", d => d.target.y)
|
.attr('y2', (d) => d.target.y);
|
||||||
node
|
node.attr('cx', (d) => d.x).attr('cy', (d) => d.y);
|
||||||
.attr("cx", d => d.x)
|
labels.attr('x', (d) => d.x).attr('y', (d) => d.y);
|
||||||
.attr("cy", d => d.y)
|
|
||||||
labels
|
|
||||||
.attr("x", d => d.x)
|
|
||||||
.attr("y", d => d.y)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(parseIdsFromLinks(links));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,8 @@
|
|||||||
<script src="https://cdn.jsdelivr.net/npm/d3@6.7.0/dist/d3.min.js" integrity="sha256-+7jaYCp29O1JusNWHaYtgUn6EhuP0VaFuswhNV06MyI=" crossorigin="anonymous"></script>
|
<script
|
||||||
|
src="https://cdn.jsdelivr.net/npm/d3@6.7.0/dist/d3.min.js"
|
||||||
|
integrity="sha256-+7jaYCp29O1JusNWHaYtgUn6EhuP0VaFuswhNV06MyI="
|
||||||
|
crossorigin="anonymous"
|
||||||
|
></script>
|
||||||
<h3>Interactive Graph</h3>
|
<h3>Interactive Graph</h3>
|
||||||
<div id="graph-container"></div>
|
<div id="graph-container"></div>
|
||||||
<style>
|
<style>
|
||||||
|
|||||||
@ -56,8 +56,12 @@
|
|||||||
}))
|
}))
|
||||||
</script>
|
</script>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import { router } from 'https://unpkg.com/million/dist/router.mjs';
|
import {
|
||||||
router();
|
router,
|
||||||
|
navigate,
|
||||||
|
} from 'https://unpkg.com/million/dist/router.mjs';
|
||||||
|
window.navigate = navigate;
|
||||||
|
router({}, '.singlePage');
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
{{ template "_internal/google_analytics.html" . }}
|
{{ template "_internal/google_analytics.html" . }}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user