Published
Edited
Dec 7, 2020
2 forks
1 star
Insert cell
Insert cell
Insert cell
printTable(gapminder.slice(0, 14))
Insert cell
Insert cell
Insert cell
gapminder2000 = gapminder // data to use in your chart, filtered on 2000 and sorted by fertility
.filter(d => d.year === 2000)
.sort((a, b) => b.fertility - a.fertility)
Insert cell
chartHeight = 500;
Insert cell
chartWidth = width; // use observable's built-in width for responsive size
Insert cell
margin = ({top: 20, right: 20, bottom: 40, left: 100}); // spacing around chart to make room for axes
Insert cell
//x =... create the d3 scale for the x axis here
x = d3.scaleLinear()
.domain([0, d3.max(gapminder2000, d => d.fertility)])
.range([margin.left, width - margin.right])
Insert cell
//y =... create the d3 scale for the y axis here
y = d3.scaleBand()
.domain(gapminder2000.map(d => d.country))
.range([margin.top, chartHeight - margin.bottom])
.padding(0.1)
Insert cell
barchart = {
const svg = d3.create("svg")
.attr("width", chartWidth)
.attr("height", chartHeight);
svg.selectAll("rect")
.data(gapminder2000)
.join("rect")
.attr("x", x(0))
.attr("y", d => y(d.country))
.attr("height", y.bandwidth())
.attr("width", d => x(d.fertility) - x(0))
.attr("fill", d => selectedData !== null && selectedData !== d ? "lightgrey" : "steelblue")
.on("click", (event, d) => {
if(selectedData === d) {
mutable selectedData = null;
}
else mutable selectedData = d;
});
svg.append("g")
.call(d3.axisLeft(y))
.attr("transform", `translate(${margin.left}, 0)`);
svg.append("g")
.call(d3.axisBottom(x))
.attr("transform", `translate(0, ${chartHeight - margin.bottom})`);
return svg.node();
}
Insert cell
Insert cell
Insert cell
yScatter = d3.scaleLinear()
.domain(d3.extent(gapminder2000, d => d.life_expect))
.range([chartHeight - margin.bottom, margin.top]);
Insert cell
colorScatter = d3.scaleOrdinal()
.domain(gapminder2000.map(d => d.cluster))
.range(d3.schemeCategory10)
Insert cell
scatterplot = {
const svg = d3.create("svg")
.attr("width", chartWidth)
.attr("height", chartHeight);
svg.selectAll("circle")
.data(gapminder2000)
.join("circle")
.attr("cx", d => x(d.fertility))
.attr("cy", d => yScatter(d.life_expect))
.attr("r", 5)
.attr("fill", d => selectedData !== null && selectedData !== d ? "lightgrey" : colorScatter(d.cluster))
.on("click", (event, d) => {
if(selectedData === d) {
mutable selectedData = null;
}
else mutable selectedData = d;
});
svg.append("g")
.call(d3.axisLeft(yScatter))
.attr("transform", `translate(${margin.left}, 0)`);
svg.append("g")
.call(d3.axisBottom(x))
.attr("transform", `translate(0, ${chartHeight - margin.bottom})`);
return svg.node();
}
Insert cell
mutable selectedData = null;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more