fix: label background word wrap width (#6055)

* fix: label background 折行宽度修改

* feat: 更新label-oversized 测试demo和截图

---------

Co-authored-by: xwb163631 <xwb163631@gongdao.com>
This commit is contained in:
xuwb 2024-07-18 11:40:53 +08:00 committed by GitHub
parent 3f775f9ef8
commit f72d4c24a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 9 deletions

View File

@ -8,7 +8,7 @@ export const elementLabelOversized: TestCase = async (context) => {
data: { data: {
x: 100, x: 100,
y: 150, y: 150,
label: `This label is too long to be displayed`, label: `This label with padding is too long to be displayed`,
size: 100, size: 100,
}, },
}, },
@ -17,7 +17,7 @@ export const elementLabelOversized: TestCase = async (context) => {
data: { data: {
x: 400, x: 400,
y: 150, y: 150,
label: 'This label is too long to be displayed', label: 'This label with padding is too long to be displayed',
size: 150, size: 150,
}, },
}, },
@ -50,6 +50,7 @@ export const elementLabelOversized: TestCase = async (context) => {
labelBackgroundFill: '#eee', labelBackgroundFill: '#eee',
labelBackgroundFillOpacity: 0.5, labelBackgroundFillOpacity: 0.5,
labelBackgroundRadius: 4, labelBackgroundRadius: 4,
labelPadding: [0, 10, 0, 10],
labelWordWrap: true, labelWordWrap: true,
labelMaxLines: 4, labelMaxLines: 4,
}, },

View File

@ -31,15 +31,18 @@
</g> </g>
<g fill="none" class="label" transform="matrix(1,0,0,1,0,52)"> <g fill="none" class="label" transform="matrix(1,0,0,1,0,52)">
<g> <g>
<path fill="rgba(238,238,238,1)" d="M -40.06,0 l 81.12,0 a 4,4,0,0,1,4,4 l 0,47 a 4,4,0,0,1,-4,4 l -81.12,0 a 4,4,0,0,1,-4,-4 l 0,-47 a 4,4,0,0,1,4,-4 z" class="background" opacity="0.75" stroke-width="0" fill-opacity="0.5" x="-44.06" y="0" width="89.12" height="55"/> <path fill="rgba(238,238,238,1)" d="M -49.019999999999996,0 l 99.03999999999999,0 a 4,4,0,0,1,4,4 l 0,63 a 4,4,0,0,1,-4,4 l -99.03999999999999,0 a 4,4,0,0,1,-4,-4 l 0,-63 a 4,4,0,0,1,4,-4 z" class="background" opacity="0.75" stroke-width="0" fill-opacity="0.5" x="-53.019999999999996" y="0" width="107.03999999999999" height="71"/>
</g> </g>
<g> <g>
<text fill="rgba(0,0,0,1)" dominant-baseline="hanging" paint-order="stroke" class="text" font-size="12" font-family="system-ui, sans-serif" text-anchor="middle" fill-opacity="0.85" font-weight="400"> <text fill="rgba(0,0,0,1)" dominant-baseline="hanging" paint-order="stroke" class="text" font-size="12" font-family="system-ui, sans-serif" text-anchor="middle" fill-opacity="0.85" font-weight="400">
<tspan x="0" dx="0.5" dy="0"> <tspan x="0" dx="0.5" dy="0">
This label is This label with
</tspan> </tspan>
<tspan x="0" dx="0.5" dy="16"> <tspan x="0" dx="0.5" dy="16">
too long to be padding is too
</tspan>
<tspan x="0" dx="0.5" dy="16">
long to be
</tspan> </tspan>
<tspan x="0" dx="0.5" dy="16"> <tspan x="0" dx="0.5" dy="16">
displayed displayed
@ -54,15 +57,18 @@
</g> </g>
<g fill="none" class="label" transform="matrix(1,0,0,1,0,77)"> <g fill="none" class="label" transform="matrix(1,0,0,1,0,77)">
<g> <g>
<path fill="rgba(238,238,238,1)" d="M -65.38000000000001,0 l 127,0 a 4,4,0,0,1,4,4 l 0,31 a 4,4,0,0,1,-4,4 l -127,0 a 4,4,0,0,1,-4,-4 l 0,-31 a 4,4,0,0,1,4,-4 z" class="background" opacity="0.75" stroke-width="0" fill-opacity="0.5" x="-69.38000000000001" y="0" width="135" height="39"/> <path fill="rgba(238,238,238,1)" d="M -72.96000000000001,0 l 146.92000000000002,0 a 4,4,0,0,1,4,4 l 0,47 a 4,4,0,0,1,-4,4 l -146.92000000000002,0 a 4,4,0,0,1,-4,-4 l 0,-47 a 4,4,0,0,1,4,-4 z" class="background" opacity="0.75" stroke-width="0" fill-opacity="0.5" x="-76.96000000000001" y="0" width="154.92000000000002" height="55"/>
</g> </g>
<g> <g>
<text fill="rgba(0,0,0,1)" dominant-baseline="hanging" paint-order="stroke" class="text" font-size="12" font-family="system-ui, sans-serif" text-anchor="middle" fill-opacity="0.85" font-weight="400"> <text fill="rgba(0,0,0,1)" dominant-baseline="hanging" paint-order="stroke" class="text" font-size="12" font-family="system-ui, sans-serif" text-anchor="middle" fill-opacity="0.85" font-weight="400">
<tspan x="0" dx="0.5" dy="0"> <tspan x="0" dx="0.5" dy="0">
This label is too long to This label with padding
</tspan> </tspan>
<tspan x="0" dx="0.5" dy="16"> <tspan x="0" dx="0.5" dy="16">
be displayed is too long to be
</tspan>
<tspan x="0" dx="0.5" dy="16">
displayed
</tspan> </tspan>
</text> </text>
</g> </g>

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -91,7 +91,7 @@ export class Label extends BaseShape<LabelStyleProps> {
Object.assign(backgroundStyle, { Object.assign(backgroundStyle, {
x: minX - left, x: minX - left,
y: minY - top, y: minY - top,
width: wordWrap ? Math.min(totalWidth, wordWrapWidth) : totalWidth, width: wordWrap ? Math.min(totalWidth, wordWrapWidth + left + right) : totalWidth,
height: halfHeight * 2 + top + bottom, height: halfHeight * 2 + top + bottom,
}); });
} }