mirror of
https://github.com/jackyzha0/hugo-obsidian.git
synced 2024-06-14 11:42:35 +03:00
add graph partial
This commit is contained in:
parent
6155ce71dc
commit
b25dcb718b
17
main.go
17
main.go
@ -92,25 +92,18 @@ func index(links []Link) (index Index) {
|
||||
linkMap := make(map[string][]Link)
|
||||
backlinkMap := make(map[string][]Link)
|
||||
for _, l := range links {
|
||||
bl := Link{
|
||||
Source: l.Target,
|
||||
Target: l.Source,
|
||||
Text: l.Text,
|
||||
}
|
||||
|
||||
// backlink (only if internal)
|
||||
|
||||
if _, ok := backlinkMap[l.Target]; ok {
|
||||
backlinkMap[l.Target] = append(backlinkMap[l.Target], bl)
|
||||
backlinkMap[l.Target] = append(backlinkMap[l.Target], l)
|
||||
} else {
|
||||
backlinkMap[l.Target] = []Link{bl}
|
||||
backlinkMap[l.Target] = []Link{l}
|
||||
}
|
||||
|
||||
// regular link
|
||||
if val, ok := linkMap[l.Source]; ok {
|
||||
val = append(val, l)
|
||||
if _, ok := linkMap[l.Source]; ok {
|
||||
linkMap[l.Source] = append(linkMap[l.Source], l)
|
||||
} else {
|
||||
linkMap[l.Target] = []Link{l}
|
||||
linkMap[l.Source] = []Link{l}
|
||||
}
|
||||
}
|
||||
index.Links = linkMap
|
||||
|
@ -1,7 +1,197 @@
|
||||
<script src="https://cdn.jsdelivr.net/npm/d3@6"></script>
|
||||
<div id="graph-container"></div>
|
||||
<script>
|
||||
const links = [
|
||||
{{- range $.Site.Data.linkIndex.links -}}
|
||||
{}
|
||||
{{- end -}}
|
||||
const index = {{$.Site.Data.linkIndex.index}}
|
||||
const links = {{$.Site.Data.linkIndex.links}}
|
||||
const curPage = {{ strings.TrimRight "/" .Page.RelPermalink }}
|
||||
const pathColors = {{$.Site.Data.graphColors.paths}}
|
||||
|
||||
const parseIdsFromLinks = (links) => [...(new Set(links.flatMap(link => ([link.source, link.target]))))]
|
||||
|
||||
const data = {
|
||||
nodes: parseIdsFromLinks(links).map(id => ({id})),
|
||||
links,
|
||||
}
|
||||
|
||||
const color = (d) => {
|
||||
if (d.id === curPage) {
|
||||
return "{{.Site.Data.graphColors.base.activeNode}}"
|
||||
}
|
||||
|
||||
for (const pathColor of pathColors) {
|
||||
const path = Object.keys(pathColor)[0]
|
||||
const colour = pathColor[path]
|
||||
if (d.id.startsWith(path)) {
|
||||
return colour
|
||||
}
|
||||
}
|
||||
|
||||
return "{{.Site.Data.graphColors.base.node}}"
|
||||
}
|
||||
|
||||
const drag = simulation => {
|
||||
function dragstarted(event, d) {
|
||||
if (!event.active) simulation.alphaTarget(1).restart();
|
||||
d.fx = d.x;
|
||||
d.fy = d.y;
|
||||
}
|
||||
|
||||
function dragged(event,d) {
|
||||
d.fx = event.x;
|
||||
d.fy = event.y;
|
||||
}
|
||||
|
||||
function dragended(event,d) {
|
||||
if (!event.active) simulation.alphaTarget(0);
|
||||
d.fx = null;
|
||||
d.fy = null;
|
||||
}
|
||||
|
||||
return d3.drag()
|
||||
.on("start", dragstarted)
|
||||
.on("drag", dragged)
|
||||
.on("end", dragended);
|
||||
}
|
||||
|
||||
const height = 400
|
||||
const width = document.getElementById("graph-container").offsetWidth
|
||||
|
||||
const simulation = d3.forceSimulation(data.nodes)
|
||||
.force("charge", d3.forceManyBody().strength(-20))
|
||||
.force("link", d3.forceLink(data.links)
|
||||
.id(d => d.id)
|
||||
)
|
||||
.force("center", d3.forceCenter());
|
||||
|
||||
const svg = d3.select('#graph-container')
|
||||
.append('svg')
|
||||
.attr('width', width)
|
||||
.attr('height', height)
|
||||
.attr("viewBox", [-width / 2, -height / 2, width, height]);
|
||||
|
||||
// legend
|
||||
const legend = [
|
||||
{"Current": "{{.Site.Data.graphColors.base.activeNode}}"},
|
||||
{"Note": "{{.Site.Data.graphColors.base.node}}"},
|
||||
...pathColors
|
||||
]
|
||||
legend.forEach((legendEntry, i) => {
|
||||
const key = Object.keys(legendEntry)[0]
|
||||
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.append("text").attr("x", -width/2 + 40).attr("y", height/2 - 30 * (i+1)).text(key).style("font-size", "15px").attr("alignment-baseline","middle")
|
||||
})
|
||||
|
||||
const link = svg.append("g")
|
||||
.selectAll("line")
|
||||
.data(data.links)
|
||||
.join("line")
|
||||
.attr("class", "link")
|
||||
.attr("stroke", "{{.Site.Data.graphColors.base.link}}")
|
||||
.attr("stroke-width", 2)
|
||||
.attr("data-source", d => d.source.id)
|
||||
.attr("data-target", d => d.target.id)
|
||||
|
||||
const graphNode = svg.append("g")
|
||||
.selectAll("g")
|
||||
.data(data.nodes)
|
||||
.enter().append("g")
|
||||
|
||||
const node = graphNode.append("circle")
|
||||
.attr("class", "node")
|
||||
.attr("id", (d) => d.id)
|
||||
.attr("r", (d) => {
|
||||
const numOut = index.links[d.id]?.length || 0
|
||||
const numIn = index.backlinks[d.id]?.length || 0
|
||||
return 3 + (numOut + numIn) / 4
|
||||
})
|
||||
.attr("fill", color)
|
||||
.style("cursor", "pointer")
|
||||
.on("click", (_, d) => {
|
||||
window.location.href = d.id;
|
||||
})
|
||||
.on("mouseover", function (_, d) {
|
||||
d3.selectAll(".node")
|
||||
.transition()
|
||||
.duration(100)
|
||||
.attr("fill", "{{$.Site.Data.graphColors.base.inactiveNode}}")
|
||||
|
||||
const neighbours = parseIdsFromLinks([...(index.links[d.id] || []), ...(index.backlinks[d.id] || [])])
|
||||
const neighbourNodes = d3.selectAll(".node").filter(d => neighbours.includes(d.id))
|
||||
const currentId = d.id
|
||||
const links = d3.selectAll(".link").filter(d => d.source.id === currentId || d.target.id === currentId)
|
||||
|
||||
// highlight neighbour nodes
|
||||
neighbourNodes
|
||||
.transition()
|
||||
.duration(200)
|
||||
.attr("fill", color)
|
||||
|
||||
// highlight links
|
||||
links
|
||||
.transition()
|
||||
.duration(200)
|
||||
.attr("stroke", "{{$.Site.Data.graphColors.base.activeLink}}")
|
||||
|
||||
// show text for self
|
||||
d3.select(this.parentNode)
|
||||
.select("text")
|
||||
.raise()
|
||||
.transition()
|
||||
.duration(200)
|
||||
.style("opacity", 0.8)
|
||||
}).on("mouseleave", function (_,d) {
|
||||
d3.selectAll(".node")
|
||||
.transition()
|
||||
.duration(200)
|
||||
.attr("fill", color)
|
||||
|
||||
const currentId = d.id
|
||||
const links = d3.selectAll(".link").filter(d => d.source.id === currentId || d.target.id === currentId)
|
||||
|
||||
links
|
||||
.transition()
|
||||
.duration(200)
|
||||
.attr("stroke", "{{$.Site.Data.graphColors.base.link}}")
|
||||
|
||||
d3.select(this.parentNode)
|
||||
.select("text")
|
||||
.transition()
|
||||
.duration(200)
|
||||
.style("opacity", 0)
|
||||
})
|
||||
.call(drag(simulation));
|
||||
|
||||
const labels = graphNode.append("text")
|
||||
.attr("dx", 12)
|
||||
.attr("dy", ".35em")
|
||||
.text((d) => d.id)
|
||||
.style("opacity", 0)
|
||||
.style("pointer-events", "none")
|
||||
.call(drag(simulation));
|
||||
|
||||
svg.call(d3.zoom()
|
||||
.extent([[0, 0], [width, height]])
|
||||
.scaleExtent([0.25, 4])
|
||||
.on("zoom", ({transform}) => {
|
||||
link.attr("transform", transform);
|
||||
node.attr("transform", transform);
|
||||
labels.attr("transform", transform);
|
||||
}));
|
||||
|
||||
simulation.on("tick", () => {
|
||||
link
|
||||
.attr("x1", d => d.source.x)
|
||||
.attr("y1", d => d.source.y)
|
||||
.attr("x2", d => d.target.x)
|
||||
.attr("y2", d => d.target.y);
|
||||
|
||||
node
|
||||
.attr("cx", d => d.x)
|
||||
.attr("cy", d => d.y);
|
||||
|
||||
labels
|
||||
.attr("x", d => d.x)
|
||||
.attr("y", d => d.y);
|
||||
});
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user