Published
Edited
Mar 26, 2022
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3
.select(DOM.svg(width, height))
.style("font", "10px sans-serif")
.style("width", "100%")
.style("height", "auto")
.style("background", "#fff")
.attr("text-anchor", "middle");

let simulations = new Map();

let g = svg.append("g");

let topSimulation = d3
.forceSimulation()
.force("collide", forceCollideTop)
.force("manybody", d3.forceManyBody().strength(0))
.force("center", d3.forceCenter(width / 2, height / 2))
.stop();

function render() {
let groups = getData(sliderTime.value.getFullYear());

let ng = g
.selectAll("g.node-group")
.data(groups, g => g[0].name)
.attr("stroke", "#fff")
.attr("stroke-width", 1.5);

ng.exit().remove();
let enter = ng
.enter()
.append("g")
.attr("class", "node-group");

ng = ng.merge(enter);

ng.each(function(nodes) {
const nodeG = d3.select(this);

let simulation;
if (simulations.has(nodes[0].name)) {
simulation = simulations.get(nodes[0].name);
} else {
simulation = d3
.forceSimulation()
.force("collide", forceCollide)
.force("center", d3.forceCenter(0, 0))
.stop();
simulations.set(nodes[0].name, simulation);
}

simulation.nodes(nodes);
tickSimulation(simulation);

let node = nodeG
.selectAll("g.node")
.data(nodes, d => d.name)
.join(
enter => {
let g = enter
.append("g")
.attr("class", "node")
.attr("transform", d => `translate(${d.x}, ${d.y})`);
g.append("circle")
.attr("r", d => radius(d))
.attr("fill", "#fff")
.attr("stroke", "#555");
g.append("image")
.attr("href", d => d.logo)
.attr("width", d => radius(d) * 1.4)
.attr("height", d => radius(d) * 1.4)
.attr(
"transform",
d => `translate(${-radius(d) * 0.7}, ${-radius(d) * 0.7})`
);
g.append("title").text(d => d.name);
return g;
},
update =>
update
.transition()
.attr("transform", d => `translate(${d.x}, ${d.y})`),
exit => exit.remove()
);
});

for (let group of groups) {
let [minX, maxX] = d3.extent(group, g => g.x);
let [minY, maxY] = d3.extent(group, g => g.y);
group.size = Math.max(maxX - minX, maxY - minY);
}

topSimulation.nodes(groups);
tickSimulation(topSimulation);

ng.attr("transform", d => `translate(${d.x}, ${d.y})`);
}

render();

sliderTime.addEventListener("input", render);

return svg.node();
}
Insert cell
tickSimulation = simulation => {
for (
let i = 0,
n = Math.ceil(
Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay())
);
i < n;
++i
) {
simulation.tick();
}
}
Insert cell
radius = ({ root }) => (root ? 30 : 20)
Insert cell
height = width
Insert cell
getData = year => {
const companiesThatExist = JSON.parse(JSON.stringify(all))
.filter(
entry =>
entry.period[0] <= year &&
(entry.period[1] === undefined || entry.period[1] >= year)
)
.map(c => ({ ...c, children: [] }));

let roots = companiesThatExist.filter(c => !c.parent);

let index = new Map(companiesThatExist.map(m => [m.name, m]));

for (let elem of companiesThatExist) {
const { parent } = elem;
if (parent) {
if (index.has(parent)) {
index.get(parent).children.push(elem);
} else {
if (holdingCompanies.has(parent)) {
let holder = {
...holdingCompanies.get(parent),
children: []
};
roots.push(holder);
index.set(parent, holder);
holder.children.push(elem);
} else {
throw new Error(`Looked for ${parent} but couldn’t find it.`);
}
}
}
}

return roots.map(r => {
let { children, ...rest } = r;
return [
{
root: true,
...rest
}
].concat(children);
});
}
Insert cell
pack = data =>
d3
.pack()
.size([width - 2, height - 2])
.padding(3)(
d3
.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value)
)
Insert cell
all = {
const name = (name, logo) => elem => ({ ...elem, name, logo });
return [
/* GM */
...[{ period: [1908] }].map(name("General Motors")),
...[{ period: [1911, 1918] }, { period: [1918], p: "General Motors" }].map(
name("Chevrolet")
),
...[{ period: [1989, 1997], p: "Chevrolet" }].map(name("Geo")),
...[{ period: [1985, 1990], p: "General Motors" }].map(name("Saturn")),
...[
{ period: [1897, 1908] },
{ period: [1908, 2004], p: "General Motors" }
].map(name("Oldsmobile")),
...[{ period: [1899, 1908] }, { period: [1908], p: "General Motors" }].map(
name("Buick")
),
...[{ period: [1902, 1909] }, { period: [1909], p: "General Motors" }].map(
name("Cadillac")
),
...[{ period: [1926, 2010], p: "General Motors" }].map(name("Pontiac")),
...[{ period: [1856, 1931] }, { period: [1931], p: "General Motors" }].map(
name("Holden")
),

/* Ford */
...[{ period: [1903] }].map(
name("Ford", "https://www.carlogos.org/logo/Ford-logo-2003-640x240.jpg")
),
...[{ period: [1917, 1922] }, { period: [1922], p: "Ford" }].map(
name("Lincoln")
),

/* VW */
...[{ period: [1937] }].map(
name(
"VW",
"https://www.carlogos.org/logo/Volkswagen-logo-2015-640x550.jpg"
)
),
...[
{ period: [1910, 1910] },
{ period: [1932, 1969], p: "Auto Union" },
{ period: [1969], p: "VW" }
].map(name("Audi")),
...[{ period: [1931, 1937] }, { period: [1937], p: "VW" }].map(
name("Porsche")
),
...[{ period: [1963, 1998] }, { period: [1998], p: "Audi" }].map(
name("Lamborghini")
),
...[
{ period: [1919, 1931] },
{ period: [1931, 1980], p: "Rolls Royce" },
{ period: [1980, 1998], p: "Vickers" },
{ period: [1998], p: "VW" }
].map(name("Bentley")),

/* Chrysler */
...[
{ period: [1925, 1998] },
{ period: [1998, 2007], p: "DaimlerChrysler" },
{ period: [2007, 2014] },
{ period: [2014], p: "Fiat Chrysler" }
].map(name("Chrysler")),
...[{ period: [2014] }].map(
name(
"Fiat Chrysler",
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Fiat_Chrysler_Automobiles_logo.svg/1200px-Fiat_Chrysler_Automobiles_logo.svg.png"
)
),
...[
{ period: [1945, 1970] },
{ period: [1970, 1986], p: "AMC" },
{ period: [1987], p: "Chrysler" }
].map(name("Jeep")),
...[
{ period: [1978, 1986], p: "British Leyland" },
{ period: [1986, 1994], p: "Rover Group" },
{ period: [1994, 2000], p: "BMW" },
{ period: [2000, 2008], p: "Ford" },
{ period: [2008, 2012], p: "Tata" },
{ period: [2012], p: "Jaguar Land Rover" }
].map(name("Land Rover")),
...[{ period: [1998, 2007] }].map(
name(
"DaimlerChrysler",
"https://www.carlogos.org/logo/Daimler-logo-640x80.jpg"
)
),

/* Nissan */
...[{ period: [1933] }].map(name("Nissan")),
...[{ period: [1989], p: "Nissan" }].map(name("Infiniti")),

/* Toyota */
...[{ period: [1937] }].map(
name(
"Toyota",
"https://www.carlogos.org/logo/Toyota-logo-1989-640x524.jpg"
)
),
...[{ period: [1989], p: "Toyota" }].map(name("Lexus")),
...[{ period: [2003, 2016], p: "Toyota" }].map(name("Scion")),

/* Honda */
...[{ period: [1946] }].map(name("Honda")),
...[{ period: [1986], p: "Honda" }].map(name("Acura")),

/* BMW */
...[{ period: [1916] }].map(name("BMW")),

/* Movers */
...[
{ period: [1906, 1971] },
{ period: [1971, 1980], p: "British taxpayer-owned" },
{ period: [1980, 2003], p: "Vickers" },
{ period: [2003], p: "BMW" }
].map(name("Rolls Royce")),
...[
{ period: [1933, 1966] },
{ period: [1966, 1968], p: "British Motor Holdings" },
{ period: [1968, 1984], p: "British Leyland" },
{ period: [1990, 2008], p: "Ford" },
{ period: [2008, 2012], p: "Tata" },
{ period: [2012], p: "Jaguar Land Rover" }
].map(name("Jaguar")),
...[
{ period: [1927, 1999] },
{ period: [1999, 2000], p: "Ford" },
{ period: [2000], p: "Geely Holding Group" }
].map(name("Volvo")),

/* Independents */
...[{ period: [1899, 2014] }].map(name("Fiat")),
...[{ period: [2003] }].map(name("Tesla")),
...[{ period: [1868] }].map(name("Tata")),
...[{ period: [1999] }].map(name("Spyker")),
...[
{ period: [1945, 1968] },
{
period: [1990, 2010],
p: "General Motors"
},
{
period: [2010, 2012],
p: "Spyker"
}
].map(name("Saab")),
...[{ period: [1954, 1988] }].map(
name(
"AMC",
"https://upload.wikimedia.org/wikipedia/commons/thumb/9/96/American-motors.svg/1200px-American-motors.svg.png"
)
)
].map(({ name, logo, p: parent, holding, period }) => ({
name,
period,
logo:
logo ||
`https://www.carlogos.org/logo/${name.replace(/ /, "-")}-logo.png`,
parent
}));
}
Insert cell
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