Published
Edited
Dec 15, 2020
Insert cell
Insert cell
Insert cell
Insert cell
ramp(d3.scaleLinear().range(['lightgreen', 'darkblue']).domain([0, 1])/*.interpolate(d3.interpolateRgb)*/, 7)
Insert cell
Insert cell
ramp(d3.scaleLinear().range(['lightgreen', 'darkblue']).domain([0, 1]).interpolate(d3.interpolateHsl), 7)
Insert cell
Insert cell
ramp(d3.scaleLinear().range(['lightgreen', 'darkblue']).domain([0, 1]).interpolate(d3.interpolateHcl), 7)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
swatches({color: categoricalColor})
Insert cell
categoricalColor = d3.scaleOrdinal()
.domain(gapminderData.map(d => d.cluster))
.range(d3.schemeSet1);
Insert cell
legend({color: sequentialColor})
Insert cell
sequentialColor = d3.scaleSequential(d3.interpolateOranges)
.domain(d3.extent(gapminderData, d => d.life_expect))
Insert cell
legend({color: divergingColor})
Insert cell
divergingColor = d3.scaleDiverging(d3.interpolatePRGn)
.domain([-1, 0, 3])
Insert cell
d3.extent(gapminderData, d => d.life_expect)
Insert cell
legend({color: quantizedColor, tickFormat: ".0f"})
Insert cell
quantizedColor = d3.scaleQuantize()
.domain(d3.extent(gapminderData, d => d.life_expect))
.range(d3.schemeOranges[5]) // d3.schemeRdPu
Insert cell
Insert cell
makeScatterPlot = circlesContainer => {
const scatterData = gapminderData.filter(d => d.year === year);
circlesContainer.selectAll("circle.dot")
.data(scatterData)
.join("circle")
.attr("class", "dot")
.attr("cx", d => xScaleScatter(d.fertility))
.attr("cy", d => yScaleScatter(d.life_expect))
.attr("r", d => radiusScale(d.pop))
.attr("fill", d => d.country === selectedCountry ? "#3484c7" : quantizedColor(d.life_expect))
.attr("opacity", 0.8)
.on("click", (event, d) => {
if (d.country === selectedCountry) {
mutable selectedCountry = null;
}
else mutable selectedCountry = d.country;
});
circlesContainer.append("g")
.call(yAxisScatter);
circlesContainer.append("g")
.call(xAxisScatter);
circlesContainer.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", 14)
.attr("fill", "currentColor")
.attr("y", 30)
.attr("x", 10)
.text(`Average Fertility & Life Expectancy by Country in ${year}`);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {swatches, legend} from "@d3/color-legend"
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