in src/axis/labelLayoutStrategy.ts [139:211]
export function rotate(
labelSelection: Selection<any, any, any, any>,
maxBottomMargin: number,
textTruncator: (properties: TextProperties, maxWidth: number) => string,
textProperties: TextProperties,
needRotate: boolean,
needEllipsis: boolean,
axisProperties: IAxisProperties,
margin: IMargin,
scrollbarVisible: boolean) {
let rotatedLength;
let defaultRotation: any;
let tickSize = axisProperties.axis.tickSize();
if (scrollbarVisible) {
if (!tickSize) // zero or undefined
defaultRotation = DefaultRotationWithScrollbarTickSizeZero;
else
defaultRotation = DefaultRotationWithScrollbar;
}
else {
defaultRotation = DefaultRotation;
}
if (needRotate) {
rotatedLength = maxBottomMargin / defaultRotation.sine;
}
labelSelection.each(function (datum) {
let axisLabel = select(this);
let labelText = axisLabel.text();
textProperties.text = labelText;
if (needRotate) {
let textContentIndex = axisProperties.values.indexOf(this.textContent);
let allowedLengthProjectedOnXAxis =
// Left margin is the width of Y axis.
margin.left
// There could be a padding before the first category.
+ axisProperties.outerPadding
// Align the rotated text's top right corner to the middle of the corresponding category first.
+ axisProperties.categoryThickness * (textContentIndex + 0.5);
// Subtracting the left padding space from the allowed length.
if (!scrollbarVisible)
allowedLengthProjectedOnXAxis -= LeftPadding;
// Truncate if scrollbar is visible or rotatedLength exceeds allowedLength
let allowedLength = allowedLengthProjectedOnXAxis / defaultRotation.cosine;
if (scrollbarVisible || needEllipsis || (allowedLength < rotatedLength)) {
labelText = textTruncator(textProperties, Math.min(allowedLength, rotatedLength));
axisLabel.text(labelText);
}
// NOTE: see note above - rotation only lines up with default d3 tickSize(6,0)
// TODO don't do these rotations if we already did them
axisLabel.style("text-anchor", "end")
.attr("dx", "-0.5em")
.attr("dy", defaultRotation.dy)
.attr("transform", defaultRotation.transform);
} else {
let maxLabelWidth = !arrayIsEmpty(axisProperties.xLabelMaxWidths) ? axisProperties.xLabelMaxWidths[datum] : axisProperties.xLabelMaxWidth;
let newLabelText = textTruncator(textProperties, maxLabelWidth);
if (newLabelText !== labelText)
axisLabel.text(newLabelText);
// TODO don't do these rotations if we already did them
axisLabel.style("text-anchor", "middle")
.attr("dx", "0em")
.attr("dy", "1em")
.attr("transform", "rotate(0)");
}
});
}