Public
Edited
Oct 26, 2023
26 forks
Importers
98 stars
Insert cell
Insert cell
fruits = [
{name: "🍊", count: 21},
{name: "🍇", count: 13},
{name: "🍏", count: 8},
{name: "🍌", count: 5},
{name: "🍐", count: 3},
{name: "🍋", count: 2},
{name: "🍎", count: 1},
{name: "🍉", count: 1}
]
Insert cell
Insert cell
fruits.map(d => d.count) // the count dimension (quantitative)
Insert cell
fruits.map(d => d.name) // the name dimension (nominal)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(fruits, d => d.count)])
.range([margin.left, width - margin.right])
.interpolate(d3.interpolateRound)
Insert cell
y = d3.scaleBand()
.domain(fruits.map(d => d.name))
.range([margin.top, height - margin.bottom])
.padding(0.1)
.round(true)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const x = d3.scaleLinear();
x.domain([0, d3.max(fruits, d => d.count)]);
x.range([margin.left, width - margin.right]);
x.interpolate(d3.interpolateRound);
return x;
}
Insert cell
Insert cell
x.domain()
Insert cell
x.range()
Insert cell
Insert cell
y("🍇") // the y-coordinate for name = grapes
Insert cell
x(21) // the x-coordinate corresponding to count = 21
Insert cell
x(0) // the x-coordinate corresponding to count = 0
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x(21) - x(0) // the width of the bar corresponding to count = 21
Insert cell
y.bandwidth() // the height of each bar
Insert cell
Insert cell
Insert cell
htl.html`<svg viewBox="0 0 ${width} 33" style="max-width: ${width}px; font: 10px sans-serif; display: block;">
<rect fill="steelblue" x="${x(0)}" width="${x(count) - x(0)}" height="33"></rect>
<text fill="white" text-anchor="end" x="${x(count)}" dx="-6" dy="21">${count}</text>
</svg>`
Insert cell
Insert cell
Insert cell
htl.html`<svg viewBox="0 ${margin.top} ${width} ${height - margin.top}" style="max-width: ${width}px; font: 10px sans-serif;">
<g fill="steelblue">
${fruits.map(d => htl.svg`<rect y="${y(d.name)}" x="${x(0)}" width="${x(d.count) - x(0)}" height="${y.bandwidth()}"></rect>`)}
</g>
<g fill="white" text-anchor="end" transform="translate(-6,${y.bandwidth() / 2})">
${fruits.map(d => htl.svg`<text y="${y(d.name)}" x="${x(d.count)}" dy="0.35em">${d.count}</text>`)}
</g>
</svg>`
Insert cell
Insert cell
Insert cell
htl.html`<svg viewBox="0 0 ${width} ${height}" style="max-width: ${width}px; font: 10px sans-serif;">
<g fill="steelblue">
${fruits.map(d => htl.svg`<rect y="${y(d.name)}" x="${x(0)}" width="${x(d.count) - x(0)}" height="${y.bandwidth()}"></rect>`)}
</g>
<g fill="white" text-anchor="end" transform="translate(-6,${y.bandwidth() / 2})">
${fruits.map(d => htl.svg`<text y="${y(d.name)}" x="${x(d.count)}" dy="0.35em">${d.count}</text>`)}
</g>
${d3.select(htl.svg`<g transform="translate(0,${margin.top})">`)
.call(d3.axisTop(x))
.call(g => g.select(".domain").remove())
.node()}
${d3.select(htl.svg`<g transform="translate(${margin.left},0)">`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.node()}
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
color = d3.scaleSequential()
.domain([0, d3.max(fruits, d => d.count)])
.interpolator(d3.interpolateBlues)
Insert cell
Insert cell
color(0)
Insert cell
color(21)
Insert cell
Insert cell
Insert cell
htl.html`<svg viewBox="0 ${margin.top} ${width} ${height - margin.top}" style="max-width: ${width}px; font: 10px sans-serif;">
<g>
${fruits.map(d => htl.svg`<rect fill="${color(d.count)}" y="${y(d.name)}" x="${x(0)}" width="${x(d.count) - x(0)}" height="${y.bandwidth()}"></rect>`)}
</g>
<g text-anchor="end" transform="translate(-6,${y.bandwidth() / 2})">
${fruits.map(d => htl.svg`<text fill="${d3.lab(color(d.count)).l < 60 ? "white" : "black"}" y="${y(d.name)}" x="${x(d.count)}" dy="0.35em">${d.count}</text>`)}
</g>
${d3.select(htl.svg`<g transform="translate(${margin.left},0)">`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.node()}
</svg>`
Insert cell
Insert cell
Insert cell
width = 640
Insert cell
height = 202
Insert cell
margin = ({top: 20, right: 0, bottom: 0, left: 30})
Insert cell
import {Legend} from "@d3/color-legend"
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