Skip to content
Open
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
44 changes: 41 additions & 3 deletions src/coord/cartesian/legacyContainLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,56 @@ function legacyLayOutGridByContained(axesList: Axis2D[], gridRect: LayoutRect):
if (labelUnionRect) {
const dim: 'height' | 'width' = axis.isHorizontal() ? 'height' : 'width';
const margin = axis.model.get(['axisLabel', 'margin']);
gridRect[dim] -= labelUnionRect[dim] + margin;
const outerLabelSize = estimateOuterLabelSize(axis, labelUnionRect[dim]);
gridRect[dim] -= outerLabelSize + margin;
if (axis.position === 'top') {
gridRect.y += labelUnionRect.height + margin;
gridRect.y += outerLabelSize + margin;
}
else if (axis.position === 'left') {
gridRect.x += labelUnionRect.width + margin;
gridRect.x += outerLabelSize + margin;
}
}
}
});
}

/**
* Estimate how much of the axis labels actually lies outside the axis
* line, i.e. on the far side of the label anchor point, which is placed
* `axisLabel.margin` away from the axis line. `axisLabel.align` (for a
* vertical axis) and `axisLabel.verticalAlign` (for a horizontal axis)
* decide that: e.g. `align: 'left'` on a left y-axis makes the labels
* extend from the anchor towards the plot area, so only the margin
* itself has to be reserved outside the axis line, not the whole label
* width; the default alignment extends the labels fully outwards.
* When `axisLabel.rotate` is set, the alignment direction no longer
* coincides with the direction perpendicular to the axis, so the full
* label size is returned, as before.
*/
function estimateOuterLabelSize(axis: Axis2D, labelSize: number): number {
const axisLabelModel = axis.getLabelModel();
if (axisLabelModel.get('rotate')) {
return labelSize;
}
if (axis.isHorizontal()) {
const verticalAlign = axisLabelModel.get('verticalAlign')
|| axisLabelModel.get('baseline')
|| (axis.position === 'top' ? 'bottom' : 'top');
return verticalAlign === 'middle'
? labelSize / 2
: verticalAlign === (axis.position === 'top' ? 'bottom' : 'top')
? labelSize
: 0;
}
const align = axisLabelModel.get('align')
|| (axis.position === 'right' ? 'left' : 'right');
return align === 'center'
? labelSize / 2
: align === (axis.position === 'right' ? 'left' : 'right')
? labelSize
: 0;
}

/**
* @return Be null/undefined if no labels.
*/
Expand Down
232 changes: 232 additions & 0 deletions test/ut/spec/component/axis/containLabel.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.