Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed calculated label gap to be passed from top #16825

51 changes: 37 additions & 14 deletions src/component/axis/AxisBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {createTextStyle} from '../../label/labelStyle';
import Model from '../../model/Model';
import {isRadianAroundZero, remRadian} from '../../util/number';
import {createSymbol, normalizeSymbolOffset} from '../../util/symbol';
import { estimateLabelUnionRect, shouldShowAllLabels } from '../../coord/axisHelper';
import * as matrixUtil from 'zrender/src/core/matrix';
import {applyTransform as v2ApplyTransform} from 'zrender/src/core/vector';
import {shouldShowAllLabels} from '../../coord/axisHelper';
import { AxisBaseModel } from '../../coord/AxisBaseModel';
import { ZRTextVerticalAlign, ZRTextAlign, ECElement, ColorString } from '../../util/types';
import { AxisBaseOption } from '../../coord/axisCommonTypes';
Expand Down Expand Up @@ -234,7 +234,8 @@ interface AxisElementsBuilder {
opt: AxisBuilderCfg,
axisModel: AxisBaseModel,
group: graphic.Group,
transformGroup: graphic.Group
transformGroup: graphic.Group,
labelGap?: number
):void
}

Expand Down Expand Up @@ -365,29 +366,47 @@ const builders: Record<'axisLine' | 'axisTickLabel' | 'axisName', AxisElementsBu
},

axisName(opt, axisModel, group, transformGroup) {
function calcDistanceToAxis() {
const defaultMargin = 10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably use xAxis.nameGap instead. The document can be updated so that when the nameLocation is outsideXXX, it means the distance between the axis name and the axis labels.

const axis = axisModel.axis;
const isHorizontal = axis.isHorizontal();
const labelUnionRect = estimateLabelUnionRect(axis);
if (!labelUnionRect) {
return 0;
}
const dim = isHorizontal ? 'height' : 'width';
const margin = axisModel.getModel('axisLabel').get('margin');
return labelUnionRect[dim] + margin + defaultMargin;
}
const name = retrieve(opt.axisName, axisModel.get('name'));

if (!name) {
return;
}

const isOutside = name === 'outsideStart' || name === 'outsideMiddle' || name === 'outsideEnd';
const labelGap = isOutside ? calcDistanceToAxis() : 0;
const nameLocation = axisModel.get('nameLocation');
const nameDirection = opt.nameDirection;
const textStyleModel = axisModel.getModel('nameTextStyle');
const gap = axisModel.get('nameGap') || 0;

const gap = (axisModel.get('nameGap') || 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const nameGap = axisModel.get('nameGap') || 0;
const gap = isOutside ? calcDistanceToAxis(nameGap) : nameGap;

And labelGap is not necessary any more.

const extent = axisModel.axis.getExtent();
const gapSignal = extent[0] > extent[1] ? -1 : 1;

const pos = [
nameLocation === 'start'
? extent[0] - gapSignal * gap
: nameLocation === 'outsideStart'
? extent[0] + gapSignal * (gap) + (nameDirection * labelGap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'start' and 'outsideStart' can use the same logic because labelGap is 0 for 'start'.

: nameLocation === 'end'
? extent[1] + gapSignal * gap
: (extent[0] + extent[1]) / 2, // 'middle'
: nameLocation === 'outsideEnd'
? extent[1] - gapSignal * (gap) - (nameDirection * labelGap)
: (extent[0] + extent[1]) / 2, // 'middle' or 'outsideMiddle'
// Reuse labelOffset.
isNameLocationCenter(nameLocation) ? opt.labelOffset + nameDirection * gap : 0
isNameLocationCenter(nameLocation)
? opt.labelOffset + nameDirection * (gap + (isNameLocationOutside(nameLocation) ? labelGap : 0))
: isNameLocationOutside(nameLocation) ? nameDirection * (gap + labelGap) : 0
];

let labelLayout;

let nameRotation = axisModel.get('nameRotate');
Expand Down Expand Up @@ -475,15 +494,16 @@ const builders: Record<'axisLine' | 'axisTickLabel' | 'axisName', AxisElementsBu

};

function endTextLayout(
rotation: number, textPosition: 'start' | 'middle' | 'end', textRotate: number, extent: number[]
) {
function endTextLayout(rotation: number,
textPosition: 'start' | 'middle' | 'end' | 'outsideStart' | 'outsideMiddle' | 'outsideEnd',
textRotate: number, extent: number[]) {
const rotationDiff = remRadian(textRotate - rotation);
let textAlign: ZRTextAlign;
let textVerticalAlign: ZRTextVerticalAlign;
const inverse = extent[0] > extent[1];
const onLeft = (textPosition === 'start' && !inverse)
|| (textPosition !== 'start' && inverse);
const textIsStart = textPosition === 'start' || textPosition === 'outsideStart';
const onLeft = ((textIsStart) && !inverse)
|| (!textIsStart && inverse);

if (isRadianAroundZero(rotationDiff - PI / 2)) {
textVerticalAlign = onLeft ? 'bottom' : 'top';
Expand Down Expand Up @@ -600,9 +620,12 @@ function isTwoLabelOverlapped(
}

function isNameLocationCenter(nameLocation: string) {
return nameLocation === 'middle' || nameLocation === 'center';
return nameLocation === 'middle' || nameLocation === 'center' || nameLocation === 'outsideMiddle';
}

function isNameLocationOutside(nameLocation: string) {
return nameLocation === 'outsideStart' || nameLocation === 'outsideMiddle' || nameLocation === 'outsideEnd';
}

function createTicks(
ticksCoords: TickCoord[],
Expand Down
2 changes: 1 addition & 1 deletion src/coord/axisCommonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface AxisBaseOptionCommon extends ComponentOption,
inverse?: boolean;
// Axis name displayed.
name?: string;
nameLocation?: 'start' | 'middle' | 'end';
nameLocation?: 'start' | 'middle' | 'end' | 'outsideStart' | 'outsideMiddle' | 'outsideEnd';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a new type and export it so that it can be used in endTextLayout.

// By degree.
nameRotate?: number;
nameTruncate?: {
Expand Down
73 changes: 73 additions & 0 deletions test/axisLabel.html

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

Loading