Published
Edited
Oct 5, 2021
1 fork
1 star
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10);

svg.append("g")
.attr("text-anchor", "middle")
.selectAll("g")
.data(data.columns)
.join("g")
.attr("transform", (d, i) => `translate(${x(i)},20)`)
.call(g => g.append("text").text(d => d))
.call(g => g.append("line").attr("y1", 3).attr("y2", 9).attr("stroke", "currentColor"));

svg.append("g")
.attr("fill", "none")
.attr("stroke", "currentColor")
.selectAll("path")
.data(data)
.join("path")
.attr("d", d => line(d.values))
.attr("class", (d) => "item" + d.id)
.attr("stroke", (d) => d.values.reduce((a,b) => a-b) < 0 ? "green" : "red" );

svg.append("g")
.selectAll("g")
.data(data.columns)
.join("g")
.attr("transform", (d, i) => `translate(${x(i) + (i === 0 ? -padding : i === n - 1 ? padding : 0)},0)`)
.attr("text-anchor", (d, i) => i === 0 ? "end" : i === n - 1 ? "start" : "middle")
.selectAll("text")
.data((d, i) => d3.zip(
data.map(i === 0 ? d => `${d.name} ${formatNumber(d.values[i])}`
: i === n - 1 ? d => `${formatNumber(d.values[i])} ${d.name}`
: d => `${formatNumber(d.values[i])}`),
dodge(data.map(d => y(d.values[i]))),
data.map((d) => d.id)
))
.join("text")
.attr("class", (d) => "itemtexts item" + d[2])
.attr("y", ([, y]) => y)
.attr("dy", "0.35em")
.text(([text]) => text)
.call(halo);

svg
.selectAll("path")
.on("touchmove mouseover", (event, d) => {
svg
.selectAll(".item" + d.id)
.style("font-weight", "bold")
.style("stroke-width", 3);
})
.on("touchend mouseout", (event, d) => {
svg
.selectAll(".item" + d.id)
.style("font-weight", "normal")
.style("stroke-width", 1);
});

svg
.selectAll(".itemtexts")
.on("touchmove mouseover", (event, d) => {
svg
.selectAll(".item" + d[2])
.style("font-weight", "bold")
.style("stroke-width", 3);
})
.on("touchend mouseout", (event, d) => {
svg
.selectAll(".item" + d[2])
.style("font-weight", "normal")
.style("stroke-width", 1);
});

return svg.node();
}
Insert cell
data = {
const data = d3.csvParseRows(await FileAttachment("government-receipts-of-gdp.csv").text());
return Object.assign(data.slice(1).map(([name, ...values], index) => ({id: index, name, values: values.map(x => +x)})), {
columns: data[0].slice(1)
});
}
Insert cell
function dodge(positions, separation = 10, maxiter = 10, maxerror = 1e-1) {
positions = Array.from(positions);
let n = positions.length;
if (!positions.every(isFinite)) throw new Error("invalid position");
if (!(n > 1)) return positions;
let index = d3.range(positions.length);
for (let iter = 0; iter < maxiter; ++iter) {
index.sort((i, j) => d3.ascending(positions[i], positions[j]));
let error = 0;
for (let i = 1; i < n; ++i) {
let delta = positions[index[i]] - positions[index[i - 1]];
if (delta < separation) {
delta = (separation - delta) / 2;
error = Math.max(error, delta);
positions[index[i - 1]] -= delta;
positions[index[i]] += delta;
}
}
if (error < maxerror) break;
}
return positions;
}
Insert cell
n = data.columns.length
Insert cell
function halo(text) {
text.clone(true)
.each(function() { this.parentNode.insertBefore(this, this.previousSibling); })
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 4)
.attr("stroke-linejoin", "round");
}
Insert cell
line = d3.line()
.x((d, i) => x(i))
.y(y)
Insert cell
x = d3.scalePoint()
.domain(d3.range(n))
.range([margin.left, width - margin.right])
.padding(0.5)
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data.flatMap(d => d.values)))
.range([height - margin.bottom, margin.top])
Insert cell
formatNumber = y.tickFormat(100)
Insert cell
height = 600
Insert cell
margin = ({top: 40, right: 50, bottom: 10, left: 50})
Insert cell
padding = 3
Insert cell
slopecolor = d3
.scaleOrdinal()
.domain(["Percentage Increase", "Percertage Decrease"])
.range(["green", "red"])
.unknown("#ccc")
Insert cell
Insert cell
d3 = require("d3@6")
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