Fix graph not rendering

This commit is contained in:
Aiden Bai 2022-04-29 11:04:58 -07:00
parent 2127249b6c
commit 6df774eb51
No known key found for this signature in database
GPG Key ID: D37584388675FF3A
4 changed files with 85 additions and 62 deletions

View File

@ -7,6 +7,9 @@ async function drawGraph(
enableLegend, enableLegend,
enableZoom enableZoom
) { ) {
if (!document.getElementById('graph-container')) return;
document.getElementById('graph-container').textContent = '';
const { index, links, content } = await fetchData; const { index, links, content } = await fetchData;
const curPage = url.replace(baseUrl, ''); const curPage = url.replace(baseUrl, '');
@ -87,13 +90,23 @@ async function drawGraph(
.on('end', enableDrag ? dragended : noop); .on('end', enableDrag ? dragended : noop);
}; };
const height = Math.max(document.getElementById("graph-container").offsetHeight, 250) const height = Math.max(
const width = document.getElementById("graph-container").offsetWidth document.getElementById('graph-container').offsetHeight,
250
);
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).distance(40)) .force('charge', d3.forceManyBody().strength(-30))
.force("center", d3.forceCenter()); .force(
'link',
d3
.forceLink(data.links)
.id((d) => d.id)
.distance(40)
)
.force('center', d3.forceCenter());
const svg = d3 const svg = d3
.select('#graph-container') .select('#graph-container')
@ -149,20 +162,24 @@ async function drawGraph(
// calculate radius // calculate radius
const nodeRadius = (d) => { const nodeRadius = (d) => {
const numOut = index.links[d.id]?.length || 0 const numOut = index.links[d.id]?.length || 0;
const numIn = index.backlinks[d.id]?.length || 0 const numIn = index.backlinks[d.id]?.length || 0;
return 3 + (numOut + numIn) / 4 return 3 + (numOut + numIn) / 4;
} };
// 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", nodeRadius) .attr('id', (d) => d.id)
.attr("fill", color) .attr('r', nodeRadius)
.style("cursor", "pointer") .attr('fill', color)
.on("click", (_, d) => { .style('cursor', 'pointer')
window.location.href = `${baseUrl}/${decodeURI(d.id).replace(/\s+/g, '-')}/` .on('click', (_, d) => {
window.navigate(
new URL(`${baseUrl}${decodeURI(d.id).replace(/\s+/g, '-')}/`),
'.singlePage'
);
}) })
.on('mouseover', function (_, d) { .on('mouseover', function (_, d) {
d3.selectAll('.node') d3.selectAll('.node')
@ -194,16 +211,13 @@ async function drawGraph(
// show text for self // show text for self
d3.select(this.parentNode) d3.select(this.parentNode)
.raise() .raise()
.select("text") .select('text')
.transition() .transition()
.duration(200) .duration(200)
.style("opacity", 1) .style('opacity', 1);
.raise() })
}).on("mouseleave", function(_, d) { .on('mouseleave', function (_, d) {
d3.selectAll(".node") d3.selectAll('.node').transition().duration(200).attr('fill', color);
.transition()
.duration(200)
.attr("fill", color)
const currentId = d.id; const currentId = d.id;
const linkNodes = d3 const linkNodes = d3
@ -221,32 +235,37 @@ async function drawGraph(
.call(drag(simulation)); .call(drag(simulation));
// draw labels // draw labels
const labels = graphNode.append("text") const labels = graphNode
.attr("dx", 0) .append('text')
.attr("dy", d => nodeRadius(d) + 8 + "px") .attr('dx', 0)
.attr("text-anchor", "middle") .attr('dy', (d) => nodeRadius(d) + 8 + 'px')
.text((d) => content[d.id]?.title || d.id.replace("-", " ")) .attr('text-anchor', 'middle')
.style("opacity", 0) .text((d) => content[d.id]?.title || d.id.replace('-', ' '))
.style("pointer-events", "none") .style('opacity', 0)
.style("font-size", "0.4em") .style('pointer-events', 'none')
.style('font-size', '0.4em')
.raise() .raise()
.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);
const scale = transform.k const scale = transform.k;
const scaledOpacity = Math.max((scale - 1) / 3.75, 0) const scaledOpacity = Math.max((scale - 1) / 3.75, 0);
labels labels.attr('transform', transform).style('opacity', scaledOpacity);
.attr("transform", transform) })
.style("opacity", scaledOpacity) );
}));
} }
// progress the simulation // progress the simulation
@ -259,6 +278,4 @@ async function drawGraph(
node.attr('cx', (d) => d.x).attr('cy', (d) => d.y); node.attr('cx', (d) => d.x).attr('cy', (d) => d.y);
labels.attr('x', (d) => d.x).attr('y', (d) => d.y); labels.attr('x', (d) => d.x).attr('y', (d) => d.y);
}); });
console.log(parseIdsFromLinks(links));
} }

View File

@ -145,11 +145,13 @@ const removeMarkdown = (
}; };
const redir = (id, term) => { const redir = (id, term) => {
navigate( window.navigate(
new URL( new URL(
`${BASE_URL.slice(0, -1)}${id}#:~:text=${encodeURIComponent(term)}/` `${BASE_URL.slice(0, -1)}${id}#:~:text=${encodeURIComponent(term)}/`
) ),
'.singlePage'
); );
closeSearch();
}; };
const formatForDisplay = (id) => ({ const formatForDisplay = (id) => ({

View File

@ -16,14 +16,3 @@
</style> </style>
{{ $js := resources.Get "js/graph.js" | resources.Fingerprint "md5" }} {{ $js := resources.Get "js/graph.js" | resources.Fingerprint "md5" }}
<script src="{{ $js.Permalink }}"></script> <script src="{{ $js.Permalink }}"></script>
<script>
drawGraph(
{{strings.TrimRight "/" .Page.Permalink}},
{{strings.TrimRight "/" .Site.BaseURL}},
{{$.Site.Data.graphConfig.paths}},
{{$.Site.Data.graphConfig.depth}},
{{$.Site.Data.graphConfig.enableDrag}},
{{$.Site.Data.graphConfig.enableLegend}},
{{$.Site.Data.graphConfig.enableZoom}}
)
</script>

View File

@ -61,7 +61,22 @@
navigate, navigate,
} from 'https://unpkg.com/million/dist/router.mjs'; } from 'https://unpkg.com/million/dist/router.mjs';
window.navigate = navigate; window.navigate = navigate;
router({}, '.singlePage'); router('.singlePage');
const callback = () => {
requestAnimationFrame(() => {
drawGraph(
{{strings.TrimRight "/" .Page.Permalink}},
{{strings.TrimRight "/" .Site.BaseURL}},
{{$.Site.Data.graphConfig.paths}},
{{$.Site.Data.graphConfig.depth}},
{{$.Site.Data.graphConfig.enableDrag}},
{{$.Site.Data.graphConfig.enableLegend}},
{{$.Site.Data.graphConfig.enableZoom}}
)
})
}
window.addEventListener('million:navigate', callback);
window.addEventListener('DOMContentLoaded', callback);
</script> </script>
</head> </head>
{{ template "_internal/google_analytics.html" . }} {{ template "_internal/google_analytics.html" . }}