Public
Edited
Aug 29, 2023
3 forks
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
myScale = (d) => 10 * d
Insert cell
myScale(0)
Insert cell
myScale(5)
Insert cell
myScale(-100)
Insert cell
Insert cell
Insert cell
scale = d3.scaleLinear()
Insert cell
Insert cell
{
const dMin = 0, dMax = 10
const rMin = 0, rMax = 100
scale
.domain([ dMin, dMax ])
.range([ rMin, rMax ])
return scale(5)
}
Insert cell
Insert cell
Insert cell
// A scale to set y values
ySliderScale = d3.scaleLinear()
.domain([ 0, 140 ])
.range([ 10, 130 ])
Insert cell
// A scale to set x values
// Ordinal scales require domain and range values to match 1:1 to work as expected
xSliderScale = d3.scaleOrdinal()
.domain([ 0, 1, 2, 3, 4 ])
.range([ 10, 40, 70, 100, 130 ])
Insert cell
// A scale to set colors
colorSliderScale = d3.scaleLinear()
.domain([0,100])
.range(['#eee', 'steelblue'])
Insert cell
{
const svg = html `<svg width="160" height="160" style="border: 1px solid #ccc"/>`
d3.select(svg).selectAll('rect').remove()
d3.select(svg)
.append('rect')
.attr('width', 20)
.attr('height', 20)
// Set the x value based on the slider value (passed into the x scale)
.attr('x', xSliderScale(xSliderValue))
// Same for y/y ccale!
.attr('y', ySliderScale(ySliderValue))
// Same for color/color scale!
.style('fill', colorSliderScale(colorSliderValue))
return svg
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const svgHTML = html `<svg height="160" width="160" style="border: 1px solid #ccc" />`
// Define our data
const data = [ 0, 1, 2, 3, 4 ]
// The max height of the rects for convenience
const maxHeight = 140
// Set the x positions of our rects (ordinal)
// The domain is our data because the data values
// will be passed to the scale when we draw
const xScale = d3.scaleOrdinal()
.domain( data )
.range([ 10, 40, 70, 100, 130 ])
// The domain is the minimum value of our data
// to the maximum value of our data (continuous)
const yScale = d3.scaleLinear()
.domain([ 0, 4 ])
.range([ 10, maxHeight ])

// Color will be set based on the value of our data
// It's convenient to use a linear scale so we don't
// have to define all of the colors in between by hand
const colorScale = d3.scaleLinear()
.domain([ 0, 4 ])
.range(['#eee', 'steelblue'])
const svg = d3.select(svgHTML)
svg.selectAll('rect')
// data bind
.data( data )
// append all 5 rects
.join('rect')
.attr('width', 20)
.attr('y', 10)
// pass in the bound data value to the scales
// with an accessor function
// so that we can set the value based on the scale
.attr('x', (d) => xScale(d))
.attr('height', (d) => yScale(d))
.style('fill', (d) => colorScale(d))
return svgHTML
}
Insert cell
Insert cell
Insert cell
d3 = require('d3@6')
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