Skip to content

Commit 8769f6d

Browse files
committed
axis.offset
1 parent 829873d commit 8769f6d

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/axis.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ var top = 1,
88
epsilon = 1e-6;
99

1010
function translateX(x) {
11-
return "translate(" + (x + 0.5) + ",0)";
11+
return "translate(" + x + ",0)";
1212
}
1313

1414
function translateY(y) {
15-
return "translate(0," + (y + 0.5) + ")";
15+
return "translate(0," + y + ")";
1616
}
1717

1818
function number(scale) {
1919
return d => +scale(d);
2020
}
2121

22-
function center(scale) {
23-
var offset = Math.max(0, scale.bandwidth() - 1) / 2; // Adjust for 0.5px offset.
22+
function center(scale, offset) {
23+
offset = Math.max(0, scale.bandwidth() - offset * 2) / 2;
2424
if (scale.round()) offset = Math.round(offset);
2525
return function(d) {
2626
return +scale(d) + offset;
@@ -38,6 +38,7 @@ function axis(orient, scale) {
3838
tickSizeInner = 6,
3939
tickSizeOuter = 6,
4040
tickPadding = 3,
41+
offset = typeof window !== "undefined" && window.devicePixelRatio >= 2 ? 0 : 0.5,
4142
k = orient === top || orient === left ? -1 : 1,
4243
x = orient === left || orient === right ? "x" : "y",
4344
transform = orient === top || orient === bottom ? translateX : translateY;
@@ -47,9 +48,9 @@ function axis(orient, scale) {
4748
format = tickFormat == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments) : identity) : tickFormat,
4849
spacing = Math.max(tickSizeInner, 0) + tickPadding,
4950
range = scale.range(),
50-
range0 = +range[0] + 0.5,
51-
range1 = +range[range.length - 1] + 0.5,
52-
position = (scale.bandwidth ? center : number)(scale.copy()),
51+
range0 = +range[0] + offset,
52+
range1 = +range[range.length - 1] + offset,
53+
position = (scale.bandwidth ? center : number)(scale.copy(), offset),
5354
selection = context.selection ? context.selection() : context,
5455
path = selection.selectAll(".domain").data([null]),
5556
tick = selection.selectAll(".tick").data(values, scale).order(),
@@ -81,23 +82,23 @@ function axis(orient, scale) {
8182

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

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

9192
tickExit.remove();
9293

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

9899
tick
99100
.attr("opacity", 1)
100-
.attr("transform", function(d) { return transform(position(d)); });
101+
.attr("transform", function(d) { return transform(position(d) + offset); });
101102

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

156+
axis.offset = function(_) {
157+
return arguments.length ? (offset = +_, axis) : offset;
158+
};
159+
155160
return axis;
156161
}
157162

0 commit comments

Comments
 (0)