Skip to content

Commit

Permalink
axis.offset
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Feb 21, 2021
1 parent 829873d commit 8769f6d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ var top = 1,
epsilon = 1e-6;

function translateX(x) {
return "translate(" + (x + 0.5) + ",0)";
return "translate(" + x + ",0)";
}

function translateY(y) {
return "translate(0," + (y + 0.5) + ")";
return "translate(0," + y + ")";
}

function number(scale) {
return d => +scale(d);
}

function center(scale) {
var offset = Math.max(0, scale.bandwidth() - 1) / 2; // Adjust for 0.5px offset.
function center(scale, offset) {
offset = Math.max(0, scale.bandwidth() - offset * 2) / 2;
if (scale.round()) offset = Math.round(offset);
return function(d) {
return +scale(d) + offset;
Expand All @@ -38,6 +38,7 @@ function axis(orient, scale) {
tickSizeInner = 6,
tickSizeOuter = 6,
tickPadding = 3,
offset = typeof window !== "undefined" && window.devicePixelRatio >= 2 ? 0 : 0.5,
k = orient === top || orient === left ? -1 : 1,
x = orient === left || orient === right ? "x" : "y",
transform = orient === top || orient === bottom ? translateX : translateY;
Expand All @@ -47,9 +48,9 @@ function axis(orient, scale) {
format = tickFormat == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments) : identity) : tickFormat,
spacing = Math.max(tickSizeInner, 0) + tickPadding,
range = scale.range(),
range0 = +range[0] + 0.5,
range1 = +range[range.length - 1] + 0.5,
position = (scale.bandwidth ? center : number)(scale.copy()),
range0 = +range[0] + offset,
range1 = +range[range.length - 1] + offset,
position = (scale.bandwidth ? center : number)(scale.copy(), offset),
selection = context.selection ? context.selection() : context,
path = selection.selectAll(".domain").data([null]),
tick = selection.selectAll(".tick").data(values, scale).order(),
Expand Down Expand Up @@ -81,23 +82,23 @@ function axis(orient, scale) {

tickExit = tickExit.transition(context)
.attr("opacity", epsilon)
.attr("transform", function(d) { return isFinite(d = position(d)) ? transform(d) : this.getAttribute("transform"); });
.attr("transform", function(d) { return isFinite(d = position(d)) ? transform(d + offset) : this.getAttribute("transform"); });

tickEnter
.attr("opacity", epsilon)
.attr("transform", function(d) { var p = this.parentNode.__axis; return transform(p && isFinite(p = p(d)) ? p : position(d)); });
.attr("transform", function(d) { var p = this.parentNode.__axis; return transform((p && isFinite(p = p(d)) ? p : position(d)) + offset); });
}

tickExit.remove();

path
.attr("d", orient === left || orient === right
? (tickSizeOuter ? "M" + k * tickSizeOuter + "," + range0 + "H0.5V" + range1 + "H" + k * tickSizeOuter : "M0.5," + range0 + "V" + range1)
: (tickSizeOuter ? "M" + range0 + "," + k * tickSizeOuter + "V0.5H" + range1 + "V" + k * tickSizeOuter : "M" + range0 + ",0.5H" + range1));
? (tickSizeOuter ? "M" + k * tickSizeOuter + "," + range0 + "H" + offset + "V" + range1 + "H" + k * tickSizeOuter : "M" + offset + "," + range0 + "V" + range1)
: (tickSizeOuter ? "M" + range0 + "," + k * tickSizeOuter + "V" + offset + "H" + range1 + "V" + k * tickSizeOuter : "M" + range0 + "," + offset + "H" + range1));

tick
.attr("opacity", 1)
.attr("transform", function(d) { return transform(position(d)); });
.attr("transform", function(d) { return transform(position(d) + offset); });

line
.attr(x + "2", k * tickSizeInner);
Expand Down Expand Up @@ -152,6 +153,10 @@ function axis(orient, scale) {
return arguments.length ? (tickPadding = +_, axis) : tickPadding;
};

axis.offset = function(_) {
return arguments.length ? (offset = +_, axis) : offset;
};

return axis;
}

Expand Down

0 comments on commit 8769f6d

Please sign in to comment.