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

Fix/arc label text ellipse #1440

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "fix: fix maxLineWidth of arc label\n\n",
"type": "none",
"packageName": "@visactor/vrender-components"
}
],
"packageName": "@visactor/vrender-components",
"email": "dingling112@gmail.com"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "fix: fix maxLineWidth of arc label\n\n",
"type": "none",
xile611 marked this conversation as resolved.
Show resolved Hide resolved
"packageName": "@visactor/vrender-core"
}
],
"packageName": "@visactor/vrender-core",
"email": "dingling112@gmail.com"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "fix: fix maxLineWidth of arc label\n\n",
"type": "none",
"packageName": "@visactor/vrender"
}
],
"packageName": "@visactor/vrender",
"email": "dingling112@gmail.com"
}
44 changes: 22 additions & 22 deletions common/config/rush/pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion docs/demos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@internal/eslint-config": "workspace:*",
"@internal/ts-config": "workspace:*",
"@visactor/vrender-kits": "workspace:0.14.8",
"@visactor/vutils": "~0.18.15",
"@visactor/vutils": "~0.18.16",
"d3-scale-chromatic": "^3.0.0",
"lodash": "4.17.21",
"dat.gui": "^0.7.9",
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@arco-design/web-react": "2.46.1",
"@visactor/vchart": "1.3.0",
"@visactor/vutils": "~0.18.15",
"@visactor/vutils": "~0.18.16",
"@visactor/vgrammar": "~0.5.7",
"@visactor/vrender": "workspace:0.20.4",
"markdown-it": "^13.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-vrender-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dependencies": {
"@visactor/vrender": "workspace:0.20.4",
"@visactor/react-vrender": "workspace:0.20.4",
"@visactor/vutils": "~0.18.15",
"@visactor/vutils": "~0.18.16",
"react-reconciler": "^0.29.0",
"tslib": "^2.3.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-vrender/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"dependencies": {
"@visactor/vrender": "workspace:0.20.4",
"@visactor/vutils": "~0.18.15",
"@visactor/vutils": "~0.18.16",
"react-reconciler": "^0.29.0",
"tslib": "^2.3.1"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/vrender-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"analysis-core": "cross-env DEBUG='Bundler*' bundle -f umd -a -i core.ts"
},
"dependencies": {
"@visactor/vutils": "~0.18.15",
"@visactor/vscale": "~0.18.15",
"@visactor/vutils": "~0.18.16",
"@visactor/vscale": "~0.18.16",
"@visactor/vrender-core": "workspace:0.20.4",
"@visactor/vrender-kits": "workspace:0.20.4"
},
Expand All @@ -34,7 +34,7 @@
"@internal/eslint-config": "workspace:*",
"@internal/ts-config": "workspace:*",
"@rushstack/eslint-patch": "~1.1.4",
"@visactor/vscale": "~0.18.15",
"@visactor/vscale": "~0.18.16",
"@types/jest": "^26.0.0",
"jest": "^26.0.0",
"jest-electron": "^0.1.12",
Expand Down
30 changes: 21 additions & 9 deletions packages/vrender-components/src/label/arc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@visactor/vutils';
import { LabelBase } from './base';
import type { ArcLabelAttrs, IPoint, Quadrant, BaseLabelAttrs, LabelItem, IArcLabelLineSpec } from './type';
import type { IArc, IRichTextAttribute } from '@visactor/vrender-core';
import type { IArc, IRichTextAttribute, ITextAttribute } from '@visactor/vrender-core';
// eslint-disable-next-line no-duplicate-imports
import { type IRichText, type IText, type IArcGraphicAttribute, type IGraphic } from '@visactor/vrender-core';
import {
Expand Down Expand Up @@ -82,7 +82,6 @@ export class ArcInfo {
this.outerRadius = outerRadius;
this.circleCenter = circleCenter;
this.labelVisible = true;
this.labelLimit = 0;
}

getLabelBounds(): IBoundsLike {
Expand Down Expand Up @@ -170,7 +169,9 @@ export class ArcLabel extends LabelBase<ArcLabelAttrs> {
}

const labels = super._layout(texts);
const textBoundsArray = labels.map(label => this.getGraphicBounds(label as any));
const textBoundsArray = labels.map(label => {
return this.getGraphicBounds(label as any);
});
const ellipsisLabelAttribute = {
...this.attribute.textStyle,
text: '…'
Expand Down Expand Up @@ -220,16 +221,21 @@ export class ArcLabel extends LabelBase<ArcLabelAttrs> {
x: basedArc.labelPosition.x,
y: basedArc.labelPosition.y,
angle: basedArc.angle,
maxLineWidth: basedArc.labelLimit,
points:
basedArc.pointA && basedArc.pointB && basedArc.pointC
? [basedArc.pointA, basedArc.pointB, basedArc.pointC]
: undefined,
line: basedArc.labelLine
};
if (labels[i].type === 'richtext') {
(labelAttribute as unknown as IRichTextAttribute).width = basedArc.labelLimit;

if (basedArc.labelLimit) {
if (labels[i].type === 'richtext') {
(labelAttribute as unknown as IRichTextAttribute).width = basedArc.labelLimit;
} else {
(labelAttribute as unknown as ITextAttribute).maxLineWidth = basedArc.labelLimit;
}
}

labels[i].setAttributes(labelAttribute);
}
}
Expand Down Expand Up @@ -526,12 +532,18 @@ export class ArcLabel extends LabelBase<ArcLabelAttrs> {
break;
}
labelWidth = Math.max(this._ellipsisWidth, labelWidth);
arc.labelLimit = labelWidth;
const needAdjustLimit = labelWidth < arc.labelSize.width - 1;

if (needAdjustLimit) {
arc.labelLimit = labelWidth;
} else {
arc.labelLimit = null;
}

arc.pointC = { x: cx, y: labelPosition.y };

const align = this._computeAlign(arc, attribute);
const targetCenterOffset =
getAlignOffset(align) * (arc.labelLimit < arc.labelSize.width ? arc.labelLimit : arc.labelSize.width);
const targetCenterOffset = getAlignOffset(align) * (needAdjustLimit ? labelWidth : arc.labelSize.width);

if (labelLayoutAlign === 'edge') {
// edge 模式下的多行文本对齐方向与其他模式相反
Expand Down
8 changes: 6 additions & 2 deletions packages/vrender-components/src/util/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
import { getTextBounds, graphicCreator } from '@visactor/vrender-core';
import type { ITextMeasureOption } from '@visactor/vutils';
// eslint-disable-next-line no-duplicate-imports
import { TextMeasure, isObject } from '@visactor/vutils';
import { TextMeasure, isObject, isValid } from '@visactor/vutils';
import { DEFAULT_TEXT_FONT_FAMILY, DEFAULT_TEXT_FONT_SIZE } from '../constant';
import type { HTMLTextContent, ReactTextContent, TextContent } from '../core/type';

Expand Down Expand Up @@ -75,9 +75,13 @@ export function getTextType(attributes: TextContent, typeKey = 'type') {
}

export function richTextAttributeTransform(attributes: ITextGraphicAttribute & IRichTextAttribute & TextContent) {
if (isValid(attributes.maxLineWidth)) {
attributes.maxWidth = attributes.maxLineWidth;
delete attributes.maxLineWidth;
}

attributes.width = attributes.width ?? 0;
attributes.height = attributes.height ?? 0;
attributes.maxWidth = attributes.maxLineWidth;
attributes.textConfig = (attributes.text as unknown as any).text || attributes.text;
return attributes;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vrender-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"color-convert": "2.0.1",
"@visactor/vutils": "~0.18.15"
"@visactor/vutils": "~0.18.16"
},
"devDependencies": {
"@internal/bundler": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/vrender-core/src/graphic/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Text extends Graphic<ITextGraphicAttribute> implements IText {
return false;
}
this.tryUpdateAABBBounds();
if (this.cache.layoutData.lines) {
if (this.cache?.layoutData?.lines) {
let mergedText = '';
this.cache.layoutData.lines.forEach(item => {
mergedText += item.str;
Expand Down
2 changes: 1 addition & 1 deletion packages/vrender-kits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test": ""
},
"dependencies": {
"@visactor/vutils": "~0.18.15",
"@visactor/vutils": "~0.18.16",
"@visactor/vrender-core": "workspace:0.20.4",
"@resvg/resvg-js": "2.4.1",
"roughjs": "4.5.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/vrender/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@internal/eslint-config": "workspace:*",
"@internal/ts-config": "workspace:*",
"@rushstack/eslint-patch": "~1.1.4",
"@visactor/vutils": "~0.18.15",
"@visactor/vutils": "~0.18.16",
"canvas": "2.11.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
Expand Down
Loading