more config

This commit is contained in:
jackyzha0 2021-07-17 14:14:35 -04:00
parent b25dcb718b
commit cd575a12d2

View File

@ -4,7 +4,7 @@
const index = {{$.Site.Data.linkIndex.index}} const index = {{$.Site.Data.linkIndex.index}}
const links = {{$.Site.Data.linkIndex.links}} const links = {{$.Site.Data.linkIndex.links}}
const curPage = {{ strings.TrimRight "/" .Page.RelPermalink }} const curPage = {{ strings.TrimRight "/" .Page.RelPermalink }}
const pathColors = {{$.Site.Data.graphColors.paths}} const pathColors = {{$.Site.Data.graphConfig.paths}}
const parseIdsFromLinks = (links) => [...(new Set(links.flatMap(link => ([link.source, link.target]))))] const parseIdsFromLinks = (links) => [...(new Set(links.flatMap(link => ([link.source, link.target]))))]
@ -15,7 +15,7 @@
const color = (d) => { const color = (d) => {
if (d.id === curPage) { if (d.id === curPage) {
return "{{.Site.Data.graphColors.base.activeNode}}" return "{{.Site.Data.graphConfig.base.activeNode}}"
} }
for (const pathColor of pathColors) { for (const pathColor of pathColors) {
@ -26,7 +26,7 @@
} }
} }
return "{{.Site.Data.graphColors.base.node}}" return "{{.Site.Data.graphConfig.base.node}}"
} }
const drag = simulation => { const drag = simulation => {
@ -47,10 +47,12 @@
d.fy = null; d.fy = null;
} }
const enableDrag = {{$.Site.Data.graphConfig.enableDrag}}
const noop = () => {}
return d3.drag() return d3.drag()
.on("start", dragstarted) .on("start", enableDrag ? dragstarted : noop)
.on("drag", dragged) .on("drag", enableDrag ? dragged : noop)
.on("end", dragended); .on("end", enableDrag ? dragended : noop);
} }
const height = 400 const height = 400
@ -70,33 +72,39 @@
.attr("viewBox", [-width / 2, -height / 2, width, height]); .attr("viewBox", [-width / 2, -height / 2, width, height]);
// legend // legend
const legend = [ const enableLegend = {{$.Site.Data.graphConfig.enableLegend}}
{"Current": "{{.Site.Data.graphColors.base.activeNode}}"}, if (enableLegend) {
{"Note": "{{.Site.Data.graphColors.base.node}}"}, const legend = [
...pathColors {"Current": "{{.Site.Data.graphConfig.base.activeNode}}"},
] {"Note": "{{.Site.Data.graphConfig.base.node}}"},
legend.forEach((legendEntry, i) => { ...pathColors
const key = Object.keys(legendEntry)[0] ]
const colour = legendEntry[key] legend.forEach((legendEntry, i) => {
svg.append("circle").attr("cx", -width/2 + 20).attr("cy", height/2 - 30 * (i+1)).attr("r", 6).style("fill", colour) const key = Object.keys(legendEntry)[0]
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 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")
})
}
// draw links between nodes
const link = svg.append("g") const link = svg.append("g")
.selectAll("line") .selectAll("line")
.data(data.links) .data(data.links)
.join("line") .join("line")
.attr("class", "link") .attr("class", "link")
.attr("stroke", "{{.Site.Data.graphColors.base.link}}") .attr("stroke", "{{.Site.Data.graphConfig.base.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
const graphNode = svg.append("g") const graphNode = svg.append("g")
.selectAll("g") .selectAll("g")
.data(data.nodes) .data(data.nodes)
.enter().append("g") .enter().append("g")
// draw individual nodes
const node = graphNode.append("circle") const node = graphNode.append("circle")
.attr("class", "node") .attr("class", "node")
.attr("id", (d) => d.id) .attr("id", (d) => d.id)
@ -114,7 +122,7 @@
d3.selectAll(".node") d3.selectAll(".node")
.transition() .transition()
.duration(100) .duration(100)
.attr("fill", "{{$.Site.Data.graphColors.base.inactiveNode}}") .attr("fill", "{{$.Site.Data.graphConfig.base.inactiveNode}}")
const neighbours = parseIdsFromLinks([...(index.links[d.id] || []), ...(index.backlinks[d.id] || [])]) const neighbours = parseIdsFromLinks([...(index.links[d.id] || []), ...(index.backlinks[d.id] || [])])
const neighbourNodes = d3.selectAll(".node").filter(d => neighbours.includes(d.id)) const neighbourNodes = d3.selectAll(".node").filter(d => neighbours.includes(d.id))
@ -131,7 +139,7 @@
links links
.transition() .transition()
.duration(200) .duration(200)
.attr("stroke", "{{$.Site.Data.graphColors.base.activeLink}}") .attr("stroke", "{{$.Site.Data.graphConfig.base.activeLink}}")
// show text for self // show text for self
d3.select(this.parentNode) d3.select(this.parentNode)
@ -152,7 +160,7 @@
links links
.transition() .transition()
.duration(200) .duration(200)
.attr("stroke", "{{$.Site.Data.graphColors.base.link}}") .attr("stroke", "{{$.Site.Data.graphConfig.base.link}}")
d3.select(this.parentNode) d3.select(this.parentNode)
.select("text") .select("text")
@ -162,6 +170,7 @@
}) })
.call(drag(simulation)); .call(drag(simulation));
// draw labels
const labels = graphNode.append("text") const labels = graphNode.append("text")
.attr("dx", 12) .attr("dx", 12)
.attr("dy", ".35em") .attr("dy", ".35em")
@ -170,26 +179,29 @@
.style("pointer-events", "none") .style("pointer-events", "none")
.call(drag(simulation)); .call(drag(simulation));
svg.call(d3.zoom() // set panning
.extent([[0, 0], [width, height]]) const enableZoom = {{$.Site.Data.graphConfig.enableZoom}}
.scaleExtent([0.25, 4]) if (enableZoom) {
.on("zoom", ({transform}) => { svg.call(d3.zoom()
link.attr("transform", transform); .extent([[0, 0], [width, height]])
node.attr("transform", transform); .scaleExtent([0.25, 4])
labels.attr("transform", transform); .on("zoom", ({transform}) => {
})); link.attr("transform", transform);
node.attr("transform", transform);
labels.attr("transform", transform);
}));
}
// 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("cx", d => d.x)
.attr("cy", d => d.y); .attr("cy", d => d.y);
labels labels
.attr("x", d => d.x) .attr("x", d => d.x)
.attr("y", d => d.y); .attr("y", d => d.y);