function createScale(
x,
{
width = 500,
maxDepth = 0,
displayWidth = 40,
height = 20,
unit = "",
format = (i) => i
} = {}
) {
const ticks = getTicks(x, { width, displayWidth, maxDepth });
const last = ticks[ticks.length - 1];
const tickPairs = d3.pairs(ticks);
const half = height / 2;
const $scale = d3.create("svg:g");
$scale
.selectAll("text")
.data(ticks)
.join("text")
.call(attrs, {
x,
y: 12,
"text-anchor": (v, i) =>
i === 0 ? "start" : v === last ? "end" : "middle"
})
.text(format);
const $bar = $scale
.append("g")
.attr("transform", "translate(0, 18)")
.call(
build({
append: "rect",
x: x(0),
y: 0,
width: x(last) - 10,
height,
fill: "white",
"stroke-width": 2,
stroke: "black"
})
)
.call(
build({
append: "text",
x: x(last) + 5,
y: half,
"dominant-baseline": "middle",
call: (g) => g.text(unit)
})
)
.call((g) =>
g
.append("g")
.selectAll("rect")
.data(tickPairs)
.join("rect")
.call(attrs, {
x: (v) => x(v[0]),
y: (v, i) => (i % 2) * half,
width: (v) => x(v[1]) - x(v[0]),
height: half
})
);
return $scale.node();
}