Public
Edited
Oct 24, 2023
8 forks
30 stars
Insert cell
Insert cell
lin = d3.scaleLinear()
Insert cell
Insert cell
Insert cell
Insert cell
run2pixels = d3.scaleLinear()
.domain([0, 42.195]) // unit: km
.range([0, 600]) // unit: pixels
Insert cell
run2pixels(0) // The start line
Insert cell
run2pixels(42.195) // The distance of a full marathon is 42.195 km; represented by 600 pixels
Insert cell
run2pixels(10 * 1.609344) // 10 miles
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
grades = d3.scaleLinear().rangeRound([0, 3])
Insert cell
grades(63 / 100)
Insert cell
["D", "C", "B", "A"][grades(63 / 100)]
Insert cell
Insert cell
children = d3.scaleLinear().rangeRound([0, 46])
Insert cell
children(90 / 100)
Insert cell
Insert cell
Insert cell
piecewiseScale = d3.scaleLinear()
.domain([0, 100, 200, 300, 400])
.range([220, 200, 100, 200, 150]) // add + Math.sin(now / 1000) * 50 to one of these for animation
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
crayons = d3.scaleLinear()
.domain([-1, 0, 1])
.range(["orange", "white", "green"])
Insert cell
Insert cell
Insert cell
Insert cell
date = d3.scaleLinear()
.range([new Date("2009-01-01T00:00:00"), new Date("2009-12-31T23:59:59")])
Insert cell
date(0.5)
Insert cell
days = d3.scaleLinear().range([new Date("2009-01-01"), new Date("2009-12-31")])
Insert cell
days(0.5)
Insert cell
Insert cell
Insert cell
opacity = d3.scaleLinear()
.domain([0, 10])
.range([0, 1])
.clamp(true) // try with false instead
Insert cell
opacity(-1) // -1 is outside the domain!
Insert cell
opacity(15) // 15 is outside the domain!
Insert cell
Insert cell
opacity.clamp()
Insert cell
Insert cell
paint = d3.scaleLinear()
.range(["yellow", "red"])
.unknown("#ccc")
Insert cell
paint(NaN)
Insert cell
Insert cell
Insert cell
Insert cell
paint.unknown()
Insert cell
Insert cell
paint.invert("red")
Insert cell
Insert cell
opacity.clamp(true).invert(-1)
Insert cell
Insert cell
d3.scaleLinear()
.domain([-30, 0, 30])
.range([1, 0, 1])
.clamp(true)
.invert(0.4)
Insert cell
Insert cell
scaleRound = d3.scaleLinear()
.range([0, 960])
.interpolate(d3.interpolateRound)
Insert cell
scaleRound(1 / 3)
Insert cell
Insert cell
scaleDecimal = d3.scaleLinear()
.range([0, 960])
.interpolate(function(a, b) {
const c = b - a;
return function(t) {
return +(a + t * c).toFixed(1);
};
})
Insert cell
scaleDecimal(2 / 7)
Insert cell
Insert cell
scaleDecimal.interpolate()
Insert cell
Insert cell
Insert cell
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