Public
Edited
May 3
Insert cell
data = FileAttachment("myYelpData_transformed.json").json()



Insert cell
tree = d3.cluster().size([2 * Math.PI, 500])

Insert cell
toppicData = data.children.map(d => d.name)
Insert cell
color = d3.scaleOrdinal()
.domain(toppicData)
.range(d3.schemeCategory10)

Insert cell
CirSize = value => 2 + Math.sqrt(value)

Insert cell
function autoBox() {
document.body.appendChild(this);
const {x, y, width, height} = this.getBBox();
document.body.removeChild(this);
return [x, y, width, height];
}
Insert cell
function setColor(d) {
var name = d.data.name;
d.color = color.domain().indexOf(name) >= 0 ? color(name) : d.parent ? d.parent.color : null;
if (d.children) d.children.forEach(setColor);
}

Insert cell
function getMinMaxvalues() {
let mappedData = data.children.map(d => d.children)
var allthedata = []
for (let i = 0; i < mappedData.length; i++) {
let j = i * 2;
allthedata[j] = d3.min(mappedData[i].map(v => v.value));
allthedata[j+1] = d3.max(mappedData[i].map(v => v.value));
}
return allthedata;
}
Insert cell
chart = {
const root = tree(d3.hierarchy(data)
.sum(d => d.value || 1)
.sort((a, b) => d3.descending(a.value, b.value))
);

setColor(root);
const svg = d3.create("svg");

svg.append("g")
.attr("fill", "none")
.attr("stroke", "grey")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y))
.each(function(d) { d.target.linkNode = this; })
.attr("stroke", d => d.target.color);

svg.append("g")
.selectAll("circle")
.data(root.descendants())
.join("circle")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`)
.attr("fill", d => d.children ? color(d.data.name) : color(d.data.group))
.attr("r", d => d.children ? 4 : CirSize(d.data.value || 1));

svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 12)
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("reviews")
.data(root.descendants())
.join("text")
.attr("transform", d => `
rotate(${d.x * 180 / Math.PI - 90})
translate(${d.y},0)
rotate(${d.x >= Math.PI ? 180 : 0})
`)
.attr("dy", "0.31em")
.attr("x", d => d.x < Math.PI === !d.children ? 6 : -6)
.attr("text-anchor", d => d.x < Math.PI === !d.children ? "start" : "end")
.text(d => d.data.name)
.clone(true).lower()
.attr("stroke", "white");

return svg.attr("viewBox", autoBox).attr("width", 1000).attr("height", 1000).node();
}

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