Published
Edited
Apr 24, 2020
1 star
Insert cell
Insert cell
Insert cell
chart = {
const links = data.links;
const nodes = data.nodes;
const zoom = d3.zoom()
.scaleExtent([0.5, 32])
.on("zoom", zoomed);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const gGrid = svg.append("g");

const link = svg.append("g")
.attr("stroke", "#ccc")
.attr("stroke-opacity", 0.3)
.selectAll("line")
.data(links)
.enter().append("line")
.attr("x1", d => x(combined[d.source].x))
.attr("y1", d => y(combined[d.source].y))
.attr("x2", d => x(combined[d.target].x))
.attr("y2", d => y(combined[d.target].y));
const defs = svg.append("defs")
.selectAll("pattern")
.data(nodes)
.enter()
.append("pattern")
.attr("id", d => d.id)
.attr("width", "100%")
.attr("height", "100%")
.attr("patternContentUnits", "objectBoundingBox")
.append("image")
.attr("width", 1)
.attr("height", 1)
.attr("preserveAspectRatio", "none")
.attr("xlink:href", d => d.attributes.imageurl);
const gDot = svg.append("g")
.attr("fill", "none")
.attr("stroke-linecap", "round")
.selectAll(".node")
.data(nodes)
.enter()
.append('g')

const circles = gDot
.append("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("fill", d => "url(#"+d.id+")")

const gx = svg.append("g");
const gy = svg.append("g");

svg.call(zoom).call(zoom.transform, d3.zoomIdentity);

function zoomed() {
const transform = d3.event.transform;
const zx = transform.rescaleX(x).interpolate(d3.interpolateRound);
const zy = transform.rescaleY(y).interpolate(d3.interpolateRound);
circles.attr("transform", transform).attr("r", d => d.size*3 / transform.k);
link.attr("transform", transform).attr("stroke-width", 2.5 / transform.k);
gx.call(xAxis, zx);
gy.call(yAxis, zy);
gGrid.call(grid, zx, zy);
}

return Object.assign(svg.node(), {
reset() {
svg.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity);
}
});
}
Insert cell
reset, chart.reset()
Insert cell
data = FileAttachment("graph@2.json").json()
Insert cell
nodes_pos = data.nodes.map(d => [d.x, d.y, d["attributes"]["Modularity Class"]])
Insert cell
combined = {
const position_list = data.nodes.map(d => ({"x": d.x, "y": d.y}))
const id_list = data.nodes.map(d => d.id)
let combined = Object.create({})
id_list.map((d, i) => combined[d] = position_list[i])
return combined
}
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(nodes_pos, d => d[0])).nice()
.rangeRound([0, width])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(nodes_pos, d => d[1])).nice()
.rangeRound([height, 0])
Insert cell
z = d3.scaleOrdinal()
.domain(nodes_pos.map(d => d[2]))
.range(d3.schemeCategory10)
Insert cell
xAxis = (g, x) => g
.attr("transform", `translate(0,${height})`)
.call(d3.axisTop(x).ticks(12))
.call(g => g.select(".domain").attr("display", "none"))
Insert cell
yAxis = (g, y) => g
.call(d3.axisRight(y).ticks(12 * k))
.call(g => g.select(".domain").attr("display", "none"))
Insert cell
grid = (g, x, y) => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g
.selectAll(".x")
.data(x.ticks(12))
.join(
enter => enter.append("line").attr("class", "x").attr("y2", height),
update => update,
exit => exit.remove()
)
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d)))
.call(g => g
.selectAll(".y")
.data(y.ticks(12 * k))
.join(
enter => enter.append("line").attr("class", "y").attr("x2", width),
update => update,
exit => exit.remove()
)
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d)));
Insert cell
k = height / width
Insert cell
height = 600
Insert cell
d3 = require("d3@5", "d3-array@2")
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