Public
Edited
Nov 8, 2023
9 forks
56 stars
Insert cell
Insert cell
import {chart} from "@d3/bar-chart"
Insert cell
chart
Insert cell
Insert cell
quarter = d3
.scaleBand()
.domain(["one", "two", "three", "four"])
.range([0, 100])
Insert cell
Insert cell
Insert cell
quarter.bandwidth()
Insert cell
bars(quarter) // bars is a function defined in the Annex
Insert cell
Insert cell
quarter.step()
Insert cell
Insert cell
quarter50 = quarter.copy().paddingInner(0.6)
Insert cell
bars(quarter50) // edit the padding value in [0,1] to see how the bands geometries change.
Insert cell
quarter50.step() // should be equal to 2/(3+4) * width
Insert cell
Insert cell
Insert cell
bars(
quarter
.copy()
.paddingInner(0) // edit the inner padding value in [0,1]
.paddingOuter(0.5) // edit the outer padding value in [0,1]
.align(0.5) // edit the align: 0 is aligned left, 0.5 centered, 1 aligned right.
)
Insert cell
Insert cell
scale = d3.scaleBand()
.domain("ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("").slice(0, domainCardinality))
.range([40, 40 + rangeLength])
.paddingInner(paddingInner)
.paddingOuter(paddingOuter)
.align(align)
.round(enableRounding)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
testround = d3.scaleBand()
.domain([1, 2, 3, 4, 5, 6])
.range([0, 100])
.round(true) // 👀 change to false to see the impact below
Insert cell
testround.bandwidth()
Insert cell
testround.step()
Insert cell
testround(1) // left pad + n * step = 100 - right pad
Insert cell
Insert cell
Insert cell
quarter.round()
Insert cell
testround.round()
Insert cell
Insert cell
Insert cell
quarter("nine")
Insert cell
quarter(1.5)
Insert cell
Insert cell
Insert cell
naiveObjectBand = d3.scaleBand()
.domain([{ name: "Bob" }, { name: "Susie" }, { name: "Alice" }])
.rangeRound([0, 100])
Insert cell
susie = ({ name: "Susie" })
Insert cell
// 🌶 this gives an unexpected value, because every object was coerced to the same string "[object Object]"
naiveObjectBand(susie)
Insert cell
Insert cell
idObjectBand = d3.scaleBand()
.domain(
[{ name: "Bob" }, { name: "Susie" }, { name: "Alice" }].map(d => d.name)
)
.rangeRound([0, 100])
Insert cell
idObjectBand(susie.name)
Insert cell
Insert cell
function bars(scale) {
const margin = 10,
width = 100,
height = 60,
chart = svg`<svg width=${width + 2 * margin} height=${height + 2 * margin}>
<g transform="translate(${margin}, ${margin})">

<rect width=${width} height="${height}"
fill="none" stroke="black" stroke-width="0.5" />

<rect x="${scale("one")}" width=${scale.bandwidth()} height="${height}"
fill="red"/>

<rect x="${scale("two")}" width=${scale.bandwidth()} height="${height}"
fill="green"/>

<rect x="${scale("three")}" width=${scale.bandwidth()} height="${height}"
fill="blue" />

<rect x="${scale("four")}" width=${scale.bandwidth()} height="${height}"
fill="#777" />

</g></svg>`;

return chart;
}
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