Published
Edited
Mar 7, 2020
7 forks
28 stars
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("defs")
.selectAll("linearGradient")
.data(data.filter(d => d.end !== null))
.join("linearGradient")
.attr("id", d => (d.gradientId = DOM.uid("gradient")).id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", d => x(d.start))
.attr("x2", d => x(d.end))
.call(g => g.append("stop").attr("stop-color", "currentColor"))
.call(g => g.append("stop").attr("offset", "100%").attr("stop-color", "#ccc"));

const line = svg.append("g")
.attr("stroke-width", 2)
.selectAll("line")
.data(data)
.join("line")
.attr("stroke", d => d.end === null ? "currentColor" : d.gradientId)
.attr("x1", d => x(d.start))
.attr("x2", d => x(d.end || x.domain()[1]))
.attr("y1", d => y(d.name) + 0.5)
.attr("y2", d => y(d.name) + 0.5);

svg.append("g")
.call(xAxis);

const label = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("text")
.data(data)
.join("text")
.attr("x", d => x(d.start) - 6)
.attr("y", d => y(d.name))
.attr("dy", "0.35em")
.attr("fill-opacity", d => d.end === null ? null : 0.6)
.text(d => d.name);

const dot = svg.append("g")
.attr("fill", "currentColor")
.selectAll("circle")
.data(data.filter(d => d.end !== null))
.join("circle")
.attr("cx", d => x(d.end))
.attr("cy", d => y(d.name) + 0.5)
.attr("r", 2);

return Object.assign(svg.node(), {
update(data) {
const t = svg.transition().duration(750);

y.domain(data.map(d => d.name));

line.data(data, d => d.name).transition(t)
.delay(d => y(d.name))
.attr("y1", d => y(d.name))
.attr("y2", d => y(d.name));

label.data(data, d => d.name).transition(t)
.delay(d => y(d.name))
.attr("y", d => y(d.name));

dot.data(data, d => d.name).transition(t)
.delay(d => y(d.name))
.attr("cy", d => y(d.name));
}
});
}
Insert cell
chart.update(data.slice().sort(order))
Insert cell
data = d3.csvParse(await FileAttachment("candidates.csv").text(), d3.autoType).sort(orders.End)
Insert cell
orders = ({
Start: (a, b) => d3.ascending(a.start, b.start),
End: (a, b) => d3.descending(a.end || today, b.end || today) || d3.descending(a.start, b.start),
Duration: (a, b) => d3.descending((a.end || today) - a.start, (b.end || today) - b.start)
})
Insert cell
today = new Date(Date.UTC(2020, 2, 6))
Insert cell
x = d3.scaleUtc()
.domain([d3.min(data, d => d.start), today])
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scalePoint()
.domain(data.map(d => d.name))
.rangeRound([margin.top, height - margin.bottom])
.padding(1)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g => g.append("g")
.attr("stroke", "white")
.attr("stroke-width", 2)
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y2", height - margin.bottom - margin.top))
Insert cell
margin = ({top: 30, right: 10, bottom: 0, left: 70})
Insert cell
height = 540
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