Published
Edited
Jun 1, 2022
Fork of d3.scaleBand
Insert cell
Insert cell
import {chart} from "@d3/bar-chart"
Insert cell
chart
Insert cell
Insert cell
four = d3
.scaleBand()
.domain(["one", "two", "three", "four"])
.range([0, 100])
Insert cell
Insert cell
Insert cell
four.bandwidth()
Insert cell
bars(four) // bars is a function defined in the Annex
Insert cell
Insert cell
four.step()
Insert cell
Insert cell
four50 = four.copy().paddingInner(0.6)
Insert cell
bars(four50) // edit the padding value in [0,1] to see how the bands geometries change.
Insert cell
four50.step() // should be equal to 2/(3+4) * width
Insert cell
Insert cell
Insert cell
bars(
four
.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
four.round()
Insert cell
testround.round()
Insert cell
Insert cell
Insert cell
four("nine")
Insert cell
four(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
d3 = require("d3-scale@4", "d3-array@3")
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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more