Published
Edited
Nov 8, 2019
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const root = treemap(data);

const svg = d3.create("svg")
.attr('width', width)
.attr('height', height)
.style("font", "10px sans-serif");

const leaf = svg.selectAll("g")
.data(root.leaves())
.join("g")
.attr("transform", d => `translate(${d.x0},${d.y0})`);

leaf.append("title")
.text(d => `${d.ancestors().reverse().map(d => d.data.name).join("/")}\n${format(d.value)}`);

leaf.append("rect")
.attr("id", d => (d.leafUid = DOM.uid("leaf")).id)
.attr("fill", '#b10000')
.attr("fill-opacity", 0.6)
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0);

leaf.append("clipPath")
.attr("id", d => (d.clipUid = DOM.uid("clip")).id)
.append("use")
.attr("xlink:href", d => d.leafUid.href);

leaf.append("text")
.attr("clip-path", d => d.clipUid)
.attr('fill', '#fff')
.attr('fill-opacity', 1)
.attr('font-size', 14)
.attr('x', 5)
.attr('dy', '1.33em')
.text(d => `${d.ancestors().reverse().map(d => d.data.name).join("/")}\n${format(d.value)}`)

return svg.node();
}
Insert cell
getSideLength = () => {
// 1 Canada exports 532297
// 2 Canada imports 124331
// 3 Mexico exports 1360190
// 4 Mexico imports 115088
const area = d3.scaleLinear()
.domain([0, 1360190])
.range([0, 500000]);
const value = {
exportsToMexico: 1360190,
importsFromMexico: 115088,
exportsToCanada: 532297,
importsFromCanada: 124331
};
const a = area(value[dataset]);
const sideLength = Math.sqrt(a)
return sideLength;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
treemap = data => d3.treemap()
.tile(d3[tile])
.size([width, height])
.padding(1)
.round(true)
(d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value))
Insert cell
format = d3.format(",d")
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
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