Public
Edited
Oct 26, 2023
1 fork
12 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
html`<svg width="420" height="120" font-family="sans-serif" font-size="10" text-anchor="end">
<g transform="translate(0,0)">
<rect fill="steelblue" width="40" height="19"></rect>
<text fill="white" x="37" y="9.5" dy=".35em">4</text>
</g>
<g transform="translate(0,20)">
<rect fill="steelblue" width="80" height="19"></rect>
<text fill="white" x="77" y="9.5" dy=".35em">8</text>
</g>
<g transform="translate(0,40)">
<rect fill="steelblue" width="150" height="19"></rect>
<text fill="white" x="147" y="9.5" dy=".35em">15</text>
</g>
<g transform="translate(0,60)">
<rect fill="steelblue" width="160" height="19"></rect>
<text fill="white" x="157" y="9.5" dy=".35em">16</text>
</g>
<g transform="translate(0,80)">
<rect fill="steelblue" width="230" height="19"></rect>
<text fill="white" x="227" y="9.5" dy=".35em">23</text>
</g>
<g transform="translate(0,100)">
<rect fill="steelblue" width="420" height="19"></rect>
<text fill="white" x="417" y="9.5" dy=".35em">42</text>
</g>
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
data = [4, 8, 15, 16, 23, 42]
Insert cell
width = 420
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data)])
.range([0, width])
Insert cell
y = d3.scaleBand()
.domain(d3.range(data.length))
.range([0, 20 * data.length])
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("width", width)
.attr("height", y.range()[1])
.attr("font-family", "sans-serif")
.attr("font-size", "10")
.attr("text-anchor", "end");

const bar = svg.selectAll("g")
.data(data)
.join("g")
.attr("transform", (d, i) => `translate(0,${y(i)})`);

bar.append("rect")
.attr("fill", "steelblue")
.attr("width", x)
.attr("height", y.bandwidth() - 1);

bar.append("text")
.attr("fill", "white")
.attr("x", d => x(d) - 3)
.attr("y", (y.bandwidth() - 1) / 2)
.attr("dy", "0.35em")
.text(d => d);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
x.domain() // quantitative, continuous
Insert cell
y.domain() // ordinal
Insert cell
Insert cell
Insert cell
z = d3.scaleOrdinal()
.domain(["apples", "limes", "blueberries"])
.range(["red", "green", "blue"])
Insert cell
z("apples")
Insert cell
z("limes")
Insert cell
Insert cell
Insert cell
y(0) // the y-coordinate of the first bar’s top edge
Insert cell
y.bandwidth() // the height of each bar
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