Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ 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;
};
return d => +scale(d) + offset;
}

function entering() {
Expand All @@ -38,6 +36,7 @@ function axis(orient, scale) {
tickSizeInner = 6,
tickSizeOuter = 6,
tickPadding = 3,
offset = typeof window !== "undefined" && window.devicePixelRatio > 1 ? 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 +46,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 +80,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 +151,10 @@ function axis(orient, scale) {
return arguments.length ? (tickPadding = +_, axis) : tickPadding;
};

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

return axis;
}

Expand Down