Skip to content
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,7 @@ If *padding* is specified, sets the padding to the specified value in pixels and
<a name="axis_offset" href="#axis_offset">#</a> <i>axis</i>.<b>offset</b>([<i>offset</i>]) · [Source](https://github.com/d3/d3-axis/blob/master/src/axis.js)

If *offset* is specified, sets the offset to the specified value in pixels and returns the axis. If *offset* is not specified, returns the current offset which defaults to 0 on devices with a [devicePixelRatio](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio) greater than 1, and 0.5px otherwise. This default offset ensures crisp edges on low-resolution devices.

<a name="axis_line" href="#axis_line">#</a> <i>axis</i>.<b>line</b>([<i>line</i>]) · [Source](https://github.com/d3/d3-axis/blob/master/src/axis.js)

If *line* is specified, enables (if truthy) or suppresses (if falsy) the domain line (and outer ticks) and returns the axis. If *line* is not specified, returns the current line option which defaults to true.
16 changes: 14 additions & 2 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function axis(orient, scale) {
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;
transform = orient === top || orient === bottom ? translateX : translateY,
domainLine = true;

function axis(context) {
var values = tickValues == null ? (scale.ticks ? scale.ticks.apply(scale, tickArguments) : scale.domain()) : tickValues,
Expand All @@ -49,7 +50,8 @@ function axis(orient, scale) {
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]),
path = selection.selectAll(".domain").data(domainLine ? [null] : []),
pathExit = path.exit(),
tick = selection.selectAll(".tick").data(values, scale).order(),
tickExit = tick.exit(),
tickEnter = tick.enter().append("g").attr("class", "tick"),
Expand Down Expand Up @@ -81,13 +83,19 @@ function axis(orient, scale) {
.attr("opacity", epsilon)
.attr("transform", function(d) { return isFinite(d = position(d)) ? transform(d + offset) : this.getAttribute("transform"); });

path.attr("stroke-opacity", 1);
pathExit = pathExit.transition(context)
.attr("stroke-opacity", epsilon);

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

tickExit.remove();

pathExit.remove();

path
.attr("d", orient === left || orient === right
? (tickSizeOuter ? "M" + k * tickSizeOuter + "," + range0 + "H" + offset + "V" + range1 + "H" + k * tickSizeOuter : "M" + offset + "," + range0 + "V" + range1)
Expand Down Expand Up @@ -154,6 +162,10 @@ function axis(orient, scale) {
return arguments.length ? (offset = +_, axis) : offset;
};

axis.line = function(_) {
return arguments.length ? (domainLine = !!_, axis) : domainLine;
};

return axis;
}

Expand Down
9 changes: 8 additions & 1 deletion test/snapshots.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {scaleLinear} from "d3-scale";
import {create} from "d3-selection";
import {axisLeft} from "../src/index.js";
import {axisBottom, axisLeft} from "../src/index.js";

export function axisLeftScaleLinear() {
const svg = create("svg");
Expand All @@ -13,3 +13,10 @@ export function axisLeftScaleLinearNonNumericRange() {
svg.append("g").call(axisLeft(scaleLinear().range([0, "500"])));
return svg.node();
}

export function axisBottomScaleLinearNoLine() {
const axis = axisBottom(scaleLinear([0, 10], [10, 290])).line(false);
const svg = create("svg");
svg.append("g").call(axis);
return svg.node();
}
38 changes: 38 additions & 0 deletions test/snapshots/axisBottomScaleLinear.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<svg>
<g fill="none" font-size="10" font-family="sans-serif" text-anchor="middle">
<g class="tick" opacity="1" transform="translate(10.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">0</text>
</g>
<g class="tick" opacity="1" transform="translate(38.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">1</text>
</g>
<g class="tick" opacity="1" transform="translate(66.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">2</text>
</g>
<g class="tick" opacity="1" transform="translate(94.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">3</text>
</g>
<g class="tick" opacity="1" transform="translate(122.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">4</text>
</g>
<g class="tick" opacity="1" transform="translate(150.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">5</text>
</g>
<g class="tick" opacity="1" transform="translate(178.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">6</text>
</g>
<g class="tick" opacity="1" transform="translate(206.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">7</text>
</g>
<g class="tick" opacity="1" transform="translate(234.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">8</text>
</g>
<g class="tick" opacity="1" transform="translate(262.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">9</text>
</g>
<g class="tick" opacity="1" transform="translate(290.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">10</text>
</g>
</g>
</svg>
38 changes: 38 additions & 0 deletions test/snapshots/axisBottomScaleLinearNoLine.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<svg>
<g fill="none" font-size="10" font-family="sans-serif" text-anchor="middle">
<g class="tick" opacity="1" transform="translate(10.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">0</text>
</g>
<g class="tick" opacity="1" transform="translate(38.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">1</text>
</g>
<g class="tick" opacity="1" transform="translate(66.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">2</text>
</g>
<g class="tick" opacity="1" transform="translate(94.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">3</text>
</g>
<g class="tick" opacity="1" transform="translate(122.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">4</text>
</g>
<g class="tick" opacity="1" transform="translate(150.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">5</text>
</g>
<g class="tick" opacity="1" transform="translate(178.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">6</text>
</g>
<g class="tick" opacity="1" transform="translate(206.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">7</text>
</g>
<g class="tick" opacity="1" transform="translate(234.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">8</text>
</g>
<g class="tick" opacity="1" transform="translate(262.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">9</text>
</g>
<g class="tick" opacity="1" transform="translate(290.5,0)">
<line stroke="currentColor" y2="6"></line><text fill="currentColor" y="9" dy="0.71em">10</text>
</g>
</g>
</svg>