Skip to content

Instantly share code, notes, and snippets.

@pinpox

pinpox/graph.js Secret

Created June 30, 2021 14:29
Show Gist options
  • Save pinpox/17496ff61ad4012dccb238d8f3deca81 to your computer and use it in GitHub Desktop.
Save pinpox/17496ff61ad4012dccb238d8f3deca81 to your computer and use it in GitHub Desktop.
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.path; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
/*
* Input file generated with
* zk graph --format json > data/notes2.json
*/
d3.json("data/notes.json", function(error, json) {
if (error) throw error;
// Create list of existing paths/IDs
var validPaths = json.notes.map(n => n.path);
// Filter broken links and format notes so that d3 can untderstand them
var edges = json.links
.filter( edge => ( validPaths.includes(edge.targetPath) && (validPaths.includes(edge.sourcePath) )))
.map(function(e){
ne = {};
ne.source = e.sourcePath;
ne.target = e.targetPath;
return ne;
});
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(edges)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(json.notes)
.enter().append("g")
var circles = node.append("circle")
.attr("r", 5)
.attr("fill", function(d) { return color(d.group); })
// We could use wordCount here, to denote the length of a note with it's color
// .attr("fill", function(d) { return color(d.wordCount); })
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var lables = node.append("text")
.text(function(d) {
return d.title;
})
.attr('x', 6)
.attr('y', 3);
// Add title to nodes
node.append("title")
.text(function(d) { return d.title; });
simulation
.nodes(json.notes)
.on("tick", ticked);
simulation.force("link")
.links(edges);
function ticked() {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
text {
font-family: sans-serif;
font-size: 10px;
}
</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="graph.js"></script>
<script>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment