Skip to content

Commit 63569db

Browse files
authored
fix #544; handle no ticks when rotating (#545)
1 parent aa6b2be commit 63569db

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/axis.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,24 @@ function createAxis(axis, scale, {ticks, tickSize, tickPadding, tickFormat}) {
220220
.tickValues(Array.isArray(ticks) ? ticks : null);
221221
}
222222

223+
const radians = Math.PI / 180;
224+
223225
function maybeTickRotate(g, rotate) {
224226
if (!(rotate = +rotate)) return;
225-
const radians = Math.PI / 180;
226-
const labels = g.selectAll("text").attr("dy", "0.32em");
227-
const y = +labels.attr("y");
228-
if (y) {
229-
const s = Math.sign(y);
230-
labels
231-
.attr("y", null)
232-
.attr("transform", `translate(0, ${y + s * 4 * Math.cos(rotate * radians)}) rotate(${rotate})`)
233-
.attr("text-anchor", Math.abs(rotate) < 10 ? "middle" : (rotate < 0) ^ (s > 0) ? "start" : "end");
234-
} else {
235-
const x = +labels.attr("x");
236-
const s = Math.sign(x);
237-
labels
238-
.attr("x", null)
239-
.attr("transform", `translate(${x + s * 4 * Math.abs(Math.sin(rotate * radians))}, 0) rotate(${rotate})`)
240-
.attr("text-anchor", Math.abs(rotate) > 60 ? "middle" : s > 0 ? "start" : "end");
227+
for (const text of g.selectAll("text")) {
228+
const x = +text.getAttribute("x");
229+
const y = +text.getAttribute("y");
230+
if (Math.abs(y) > Math.abs(x)) {
231+
const s = Math.sign(y);
232+
text.setAttribute("transform", `translate(0, ${y + s * 4 * Math.cos(rotate * radians)}) rotate(${rotate})`);
233+
text.setAttribute("text-anchor", Math.abs(rotate) < 10 ? "middle" : (rotate < 0) ^ (s > 0) ? "start" : "end");
234+
} else {
235+
const s = Math.sign(x);
236+
text.setAttribute("transform", `translate(${x + s * 4 * Math.abs(Math.sin(rotate * radians))}, 0) rotate(${rotate})`);
237+
text.setAttribute("text-anchor", Math.abs(rotate) > 60 ? "middle" : s > 0 ? "start" : "end");
238+
}
239+
text.removeAttribute("x");
240+
text.removeAttribute("y");
241+
text.setAttribute("dy", "0.32em");
241242
}
242243
}

0 commit comments

Comments
 (0)