Published
Edited
Jun 24, 2018
18 forks
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function chart(root) {
const svg = d3.select(DOM.svg(width, height));
var format = d3.format(",d"),
color = d3.scaleOrdinal(d3.schemeSet3)

var tooltip = d3.select("body").append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("color", "white")
.style("padding", "8px")
.style("background-color", "rgba(0, 0, 0, 0.75)")
.style("border-radius", "6px")
.style("font", "12px sans-serif")
.text("tooltip");
var bubble = d3.pack()
.size([width, height])
.padding(1.5);

bubble(root);
var node = svg.selectAll(".node")
.data(root.children)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) {
return color(d.data.group);
})
.on("mouseover", function(d) {
tooltip.text(d.data.name + ": " + format(d.value));
tooltip.style("visibility", "visible");
d3.select(this).style("stroke", "black");
})
.on("mousemove", function() {
return tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");
})
.on("mouseout", function() {
d3.select(this).style("stroke", "none");
return tooltip.style("visibility", "hidden");
});

node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.style("font", "10px sans-serif")
.style("pointer-events", "none")
.text(function(d) { return d.data.name.substring(0, d.r / 3); });

return svg.node();
}
Insert cell
Insert cell
Insert cell
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