chart = Plot.plot({
marginBottom: 100,
marginLeft: 70,
marginTop: 40,
x: {
label: "Company",
tickRotate: -45,
domain: sortedData.map(d => d.Company)
},
y: {
label: metric,
grid: true,
tickFormat: d => {
if (d >= 1e12) return (d / 1e12).toFixed(1) + "T";
if (d >= 1e9) return (d / 1e9).toFixed(1) + "B";
if (d >= 1e6) return (d / 1e6).toFixed(1) + "M";
if (d >= 1e3) return (d / 1e3).toFixed(1) + "K";
return d;
}
},
color: {
domain: ["Tech", "Non-Tech"],
range: ["green", "#5dade2"],
legend: true
},
marks: [
Plot.text([{}], {
text: () => `Top 20 Largest Companies in US`,
frameAnchor: "top",
anchor: "middle",
dy: -25,
fontSize: 15,
fontWeight: "bold"
}),
Plot.text([{}], {
text: () => `Ranked by ${metric}`,
frameAnchor: "top",
anchor: "middle",
dy: -5,
fontSize: 15,
fontWeight: "bold"
}),
Plot.barY(sortedData, {
x: "Company",
y: metric,
fill: "Category",
title: d => `${d.Company}\n${metric}: ${d[metric]}`,
})
]
})