Public
Edited
Jul 25, 2023
Insert cell
Insert cell
Insert cell
Insert cell
chart(zoom)
Insert cell
Insert cell
chart(apple)
Insert cell
Insert cell
chart(tesla)
Insert cell
md`### Amazon and the 10 biggest pharma companies
Market cap on January 19th, 2021`
Insert cell
chart(amazon)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
apple
Insert cell
chart = data => {

const root = treemap(data);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, 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.data.name}\n${format(d.value)}`);

leaf.append("rect")
.attr("id", d => (d.leafUid = DOM.uid("leaf")).id)
.attr("fill", d => d.data.color)
.attr("fill-opacity", 0.9)
.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")
.style("font-size", d => d.data.name === d.parent.data.children[0].name ? "xx-large" : "x-small")
.selectAll("tspan")
.data(d => d.data.name.split(/(?=[A-Z][a-z])|\s+/g).concat(format(d.value / 1e9) + " Mrd."))
.join("tspan")
.attr("x", 3)
.attr("y", (d, i, nodes) => `${(i === nodes.length - 1) * 0.3 + 1.1 + i * 0.9}em`)
.attr("fill-opacity", (d, i, nodes) => i === nodes.length - 1 ? 0.7 : null)
.text(d => d);

return svg.node();
}
Insert cell
viewof tile = {
const options = [
{name: "d3.treemapBinary", value: d3.treemapBinary},
{name: "d3.treemapDice", value: d3.treemapDice},
{name: "d3.treemapSlice", value: d3.treemapSlice},
{name: "d3.treemapSliceDice", value: d3.treemapSliceDice},
{name: "d3.treemapSquarify", value: d3.treemapSquarify, selected: true}
];
const form = html`<form style="display: flex; align-items: center; min-height: 33px;"><select name=i>${options.map(o => Object.assign(html`<option>`, {textContent: o.name, selected: o.selected}))}`;
form.i.onchange = () => form.dispatchEvent(new CustomEvent("input"));
form.oninput = () => form.value = options[form.i.selectedIndex].value;
form.oninput();
return form;
}
Insert cell
treemap = data => d3.treemap()
.tile(tile)
.size([width, height])
.padding(4)
.round(false)
(d3.hierarchy(data)
.sum(d => d.value)
//.sort((a, b) => b.value - a.value)
)
Insert cell
Insert cell
width = 670
Insert cell
height = 350
Insert cell
format = d3.format(".3s")
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
import {select} from "@jashkenas/inputs"
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