Published
Edited
Dec 5, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Inputs.table(gapminder)
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 scale for the x axis here
Insert cell
//y =... create the scale for the y axis here
Insert cell
barchart = {
const svg = d3.create("svg")
.attr("width", chartWidth)
.attr("height", chartHeight);
svg.selectAll("rect")
.data(gapminder2000)
.join("rect")
// TODO: set the attributes for the bars:
// .attr("x", ... )
// .attr("y", ... )
// .attr("height", ... )
// .attr("width", ... )
// .attr("fill", ... )
// uncomment the next line to add rudimentary tooltips to the bars eg. to double check if your data is
// shown correctly and matches the scale as displayed on the axis.
// .append("title").text(d => "tooltip information here")
// TODO: uncomment to add axes to the chart:
// 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
// TODO: define the x scale for the scatter plot
// xScatter = ...
Insert cell
// TODO: define the y scale for the scatter plot
// yScatter = ...;
Insert cell
// color scale for the scatter plot (maps each cluster to a different color from d3.schemeCategory10)
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")
// TODO: to reach the target image you need to define the attributes cx, cy, r (radius), and fill (color)
// - you can use a radius of 5 pixels for example
// - for the fill make use of the color scale defined above "colorScatter"
// .attr("cx", ...)
// .attr("cy", ...)
// .attr("r", ...)
// .attr("fill", ...);
// TODO: add x and y axes to the scatter plot.
return svg.node();
}
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