Published
Edited
Jun 19, 2019
1 fork
4 stars
Insert cell
Insert cell
chart = {
const root = pack(data);
let focus = root;
let view;

const svg = d3.create("svg")
.attr("viewBox", `-${width / 2} -${height / 2} ${width} ${height}`)
.style("display", "block")
.style("margin", "0 -14px")
.style("background", "none")
.style("cursor", "pointer")
.on("click", () => zoom(root));

const node = svg.append("g")
.selectAll("circle")
.data(root.descendants().slice(1))
.join("circle")
.attr("fill", d => d.children ? color(d.depth) : "green")
.attr("pointer-events", d => !d.children ? "none" : null)
.on("mouseover", function() {
d3.select(this).attr("stroke", "orange");
})
.on("mouseout", function() { d3.select(this).attr("stroke", null); })
.on("click", d => focus !== d && (zoom(d), d3.event.stopPropagation()));

const formatter = new Intl.NumberFormat('it-IT', {
style: 'currency',
currency: 'EUR'
});
const label = svg.append("g")
.style("font", "12px sans-serif")
.style("fill", "white")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.selectAll("text")
.data(root.descendants())
.join("g")
.classed("labels",true)
.style("fill-opacity", d => d.parent === root ? 1 : 0)
.style("display", d => d.parent === root ? "inline" : "none");
label.append("text")
.text(d => d.data.name)
.attr("dy",-6);
label.append("text")
.text(d => formatter.format(d.data.value))
.attr("dy",+6);


zoomTo([root.x, root.y, root.r * 2]);

function zoomTo(v) {
const k = width / v[2];

view = v;

label.attr("transform", d => `translate(${(d.x - v[0]) * k},${(d.y - v[1]) * k})`);
node.attr("transform", d => `translate(${(d.x - v[0]) * k},${(d.y - v[1]) * k})`);
node.attr("r", d => d.r * k);
}

function zoom(d) {
const focus0 = focus;

focus = d;

const transition = svg.transition()
.duration(d3.event.altKey ? 7500 : 750)
.tween("zoom", d => {
const i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2]);
return t => zoomTo(i(t));
});

label
.filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
.transition(transition)
.style("fill-opacity", d => d.parent === focus ? 1 : 0)
.on("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
.on("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
}

return svg.node();
}
Insert cell
color = d3.scaleLinear()
.domain([0,5])
.range(['yellow','green'])
.interpolate(d3.interpolateHcl)
Insert cell
data = d3.json('https://raw.githubusercontent.com/Danyyy89/Hello-world/master/grafico_gerarchico.json')
Insert cell
pack = data => d3.pack()
.size([width, height])
.padding(3)
(d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value))
Insert cell
width = 932
Insert cell
height = width
Insert cell
format = d3.format(",d")
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