Public
Edited
Nov 22
Insert cell
Insert cell
Part of my new practice of building out small parts of a larger viz into notebooks - nothing new but useful for me and hopefully someone else as well.
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 1000, height])

const startYear = sampleYearData[0];
const yAxis = svg.append("g");

yAxis.call(d3.axisLeft(yScale).ticks(0).tickSizeOuter(0))
.attr("transform", `translate(${margins},${margins})`);

svg.append("text")
.attr("class", "dragText")
.attr("font-size", 24)
.attr("text-anchor", "end")
.attr("cursor", "grab")
.attr("x", margins - 5)
.attr("y", margins + yScale(startYear) + 12)
.text(startYear)
.call(
d3.drag()
.on("drag", (event, d) => {
const roundedYear = parseInt(yScale.invert(event.y - margins - 12));
if(roundedYear >= yScale.domain()[0] && roundedYear <= yScale.domain()[1]){
svg.select(".dragText").attr("y", event.y);
svg.select(".dragText").text(roundedYear);
svg.select(".dragTextCommentary").text(`dragYear: ${roundedYear}`);
}
})
);

svg.append("text")
.attr("class", "dragTextCommentary")
.attr("font-size", 18)
.attr("fill", "purple")
.attr("x", margins + 10)
.attr("y", margins + 16)
.text(`dragYear: ${startYear}`);
return svg.node();
}
Insert cell
yScaleForInvert = d3.scaleLinear().domain(yScale.domain()).range(yScale.range().reverse())
Insert cell
yScale = d3.scaleLinear().domain([2000,2024]).range([height - (margins * 2),0]);
Insert cell
margins = 60;
Insert cell
height=400;
Insert cell
sampleYearData = Array.from({length: 24}, (_, i) => i + 2000)
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