Published
Edited
Oct 20, 2018
Insert cell
Insert cell
Insert cell
chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
const simulation = forceSimulation(nodes, links).on("tick", ticked);
const width = 600
const svg = d3.select('#my-canvas')
.attr("viewBox", [-width / 2, -height / 2, width, height]);
// Create Arrow head SVG definition
svg.append("svg:defs").selectAll("marker")
.data(["end"]) // Different link/path types can be defined here
.enter().append("svg:marker") // This section adds in the arrows
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 12)
.attr("markerHeight", 12)
.attr("stroke", "red")
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");

const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll(".link-group")
.data(links)
.enter().append("line")
.attr("class", "link-group")
.attr("stroke-width", 1)
.attr("marker-end", "url(#end)");

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
node.append("circle")
.attr("r", 10)
.attr("fill", color)
.call(drag(simulation));
node.append("text")
.text(d => d.__proto__.id)
.style('fill', '#0000')

// node.append("title")
// .text(d => d.id);

function ticked() {
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("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}

return svg.node();
}
Insert cell
function forceSimulation(nodes, links) {
return d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-600))
.force("center", d3.forceCenter());
}
Insert cell
_data = (await fetch("https://gist.githubusercontent.com"
+ "/mbostock/4062045"
+ "/raw/5916d145c8c048a6e3086915a6be464467391c62"
+ "/miserables.json")).json()
Insert cell
data =
d3.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vQQpkq7QyDWoXiT8GyZlIOho3tIY5fAT-CfI7bfXo1qEZ_vWXPg76hsHW8ZYEXuoOloSad4qE-pZj6z/pub?gid=0&single=true&output=csv")
.then(resp => {
const nodes = {}
const links = []
resp.forEach(row => {
nodes[row.name] = { id: row.name, group: 1 }
row.teammates.split(", ").forEach(teammate => {
links.push({source: row.name, target: teammate, value: 1})
nodes[row.name] = { id: row.name, group: 1 }
})
})
console.log({nodes: Object.values(nodes), links: links})
return {nodes: Object.values(nodes), links: links};
})
Insert cell
height = 600
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
Insert cell
drag = simulation => {
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;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more