export function willLabelsWordBreak()

in src/axis/labelLayoutStrategy.ts [43:107]


export function willLabelsWordBreak(
    axisProperties: IAxisProperties,
    margin: IMargin,
    availableWidth: number,
    textWidthMeasurer: ITextAsSVGMeasurer,
    textHeightMeasurer: ITextAsSVGMeasurer,
    textTruncator: (properties: TextProperties, maxWidth: number) => string,
    properties: TextProperties) {

    let labels = axisProperties.values;
    let labelMaxWidth = axisProperties.xLabelMaxWidth !== undefined
        ? axisProperties.xLabelMaxWidth
        : availableWidth / labels.length;
    let maxRotatedLength = margin.bottom / DefaultRotation.sine;
    let height = textHeightMeasurer(properties);
    let maxNumLines = Math.max(1, Math.floor(margin.bottom / height));

    if (labels.length === 0)
        return false;

    // If no break character and exceeds max width, word breaking will not work, return false
    let mustRotate = labels.some(label => {
        // Detect must rotate and return immediately
        properties.text = label;
        return !wordBreaker.hasBreakers(label) && textWidthMeasurer(properties) > labelMaxWidth;
    });

    if (mustRotate) {
        return false;
    }

    let moreWordBreakChars = labels.filter((label, index: number) => {
        // ...otherwise compare rotation versus word breaking
        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 * (index + 0.5)
            // Subtracting the left padding space from the allowed length
            - LeftPadding;

        let allowedLength = allowedLengthProjectedOnXAxis / DefaultRotation.cosine;
        let rotatedLength = Math.min(allowedLength, maxRotatedLength);

        // Which shows more characters? Rotated or maxNumLines truncated to labelMaxWidth?
        let wordBreakChars = wordBreaker.splitByWidth(
            label,
            properties,
            textWidthMeasurer,
            labelMaxWidth,
            maxNumLines,
            textTruncator).join(" ");

        properties.text = label;
        let rotateChars = textTruncator(properties, rotatedLength);

        // prefer word break (>=) as it takes up less plot area
        return textUtil.removeEllipses(wordBreakChars).length >= textUtil.removeEllipses(rotateChars).length;
    });

    // prefer word break (>=) as it takes up less plot area
    return moreWordBreakChars.length >= Math.floor(labels.length / 2);
}