Published
Edited
Jul 17, 2020
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 = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-100))
.force("collide", d3.forceCollide(function (d) { return nodeRadiusScale(d.value) }))
.force("center", d3.forceCenter(width / 2, height / 2));

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

const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));

const nodeG = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation))
nodeG.append('circle')
.attr("r", d => nodeRadiusScale(d.value))
.attr("fill", color)

nodeG.append('g')
.each(function (d) {
drawHexagons(
d3.select(this),
[{ key: d.id, values: d.value }],
{
width: nodeRadiusScale(d.value),
height: nodeRadiusScale(d.value),
nodeColor: 'white',
borderColor: 'black',
nodeTextColor: 'black',
}
)
})

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);

nodeG.attr("transform", d => `translate(${d.x}, ${d.y})`)
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
data = FileAttachment("miserables.json").json()
Insert cell
data['nodes'].forEach((d,i)=>{
d.value = Math.floor(Math.random() * 5)
})
Insert cell
height = 600
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
Insert cell
nodeRadiusScale = d3.scaleSqrt().domain([0, 50]).range([10, 50])
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
function drawHexagons(nodeElement, parentIds, options) {

const nodeColor = options.nodeColor
const borderColor = options.borderColor
const nodeTextColor = options.nodeTextColor
const width = options.width
const height = options.height
const data = getData(parentIds, width * 2, height * 2)

const SQRT3 = Math.sqrt(3)
const hexagonPoly = [
[0, -1],
[SQRT3 / 2, 0.5],
[0, 1],
[-SQRT3 / 2, 0.5],
[-SQRT3 / 2, -0.5],
[0, -1],
[SQRT3 / 2, -0.5],
]

function generateHexagon(hexRadius) {
const hexagonPath =
"m" +
hexagonPoly
.map(function (p) {
return [p[0] * hexRadius, p[1] * hexRadius].join(",")
})
.join("l") +
"z"
return hexagonPath
}
const node = nodeElement.insert("g")

const nodeData = node.selectAll("g").data(data)

const nodesEnter = nodeData
.enter()
.append("g")
.attr("id", (d, i) => "node-group-" + i)
.attr("transform", (d) => `translate(${d.x - width},${d.y - height})`)

nodesEnter
.filter((d) => d.height === 0)
.append("path")
.attr("class", "node pie")
.attr("d", (d) => generateHexagon(d.r + 2))
.attr("stroke", borderColor)
.attr("stroke-width", 1)
.attr("fill", "white")

//nodesEnter
//.filter((d) => d.height === 0)
//.append("text")
//.attr("fill", nodeTextColor)
//.attr("font-size", "0.6em")
//.attr("text-anchor", "middle")
//.attr("alignment-baseline", "middle")
//.text('Label')

}
Insert cell
function getData(parentIDs, width, height) {

var rawData = []
rawData.push({ id: "root" })
parentIDs.forEach((d) => {
rawData.push({ id: d.key, parentId: "root", size: d.values })
d3.range(0, d.values).forEach((el) => {
rawData.push({
id: el,
parentId: d.key,
size: 1
})
})
})
const vData = d3.stratify()(rawData)
const vLayout = d3.pack().size([width, height]).padding(10)
const vRoot = d3.hierarchy(vData).sum(function (d) {
return d.data.size
})
const vNodes = vLayout(vRoot)
const data = vNodes.descendants().slice(1)

return data
}
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more