Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@visactor/vchart",
"comment": "fix: prevent rich text labels from inheriting line dash styles (Issue #4595)",
"type": "patch"
}
],
"packageName": "@visactor/vchart",
"email": "biukam.w@gmail.com"
}
94 changes: 92 additions & 2 deletions packages/vchart/__tests__/unit/mark/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { markContext as ctx } from '../../util/context';
import { TextMark } from '../../../src/mark/text';
import { LayoutZIndex } from '../../../src/constant/layout';
import { default as VChart } from '../../../src';
import { createCanvas, removeDom } from '../../util/dom';

test('rule mark initial style', () => {
test('text mark initial style', () => {
const textMark = new TextMark('rule0', ctx);
textMark.created();
const visible = textMark.getAttribute('visible', {});
Expand Down Expand Up @@ -30,6 +32,94 @@ test('rule mark initial style', () => {
expect(stroke).toEqual(undefined);
expect(strokeOpacity).toEqual(undefined);
expect(strokeWidth).toEqual(0);
expect(lineDash).toEqual(undefined);
expect(lineDash).toEqual([]);
expect(cursor).toEqual(undefined);
});

describe('rich text line dash rendering', () => {
let canvasDom: HTMLCanvasElement;
let chart: VChart;
let setLineDashSpy: jest.SpyInstance;
let strokeTextSpy: jest.SpyInstance;

beforeEach(() => {
canvasDom = createCanvas();
canvasDom.width = 500;
canvasDom.height = 500;
});

afterEach(() => {
setLineDashSpy?.mockRestore();
strokeTextSpy?.mockRestore();
chart?.release();
removeDom(canvasDom);
});

test('resets line dash before drawing a rich text label', () => {
setLineDashSpy = jest.spyOn(CanvasRenderingContext2D.prototype, 'setLineDash');
strokeTextSpy = jest.spyOn(CanvasRenderingContext2D.prototype, 'strokeText');

chart = new VChart(
{
type: 'scatter',
data: [{ id: 'data1', values: [{ x: 1, y: 1, size: 50 }] }],
xField: 'x',
yField: 'y',
sizeField: 'size',
point: {
style: {
stroke: '#ff0000',
lineWidth: 4,
lineDash: [10, 10]
}
},
label: {
visible: true,
position: 'top',
offset: 10,
formatMethod: () => ({
type: 'rich',
text: [
{
text: 'label',
fontSize: 30,
fill: '#000',
stroke: '#00ff00',
lineWidth: 4
}
]
})
},
animation: false
} as any,
{
renderCanvas: canvasDom,
animation: false
}
);

chart.renderSync();

const richTextGraphic = chart.getStage().getElementsByType('richtext')[0] as any;
const lineDashCalls = setLineDashSpy.mock.calls.map(([lineDash]) => lineDash);
const pointLineDashIndex = lineDashCalls.findIndex(
lineDash => Array.isArray(lineDash) && lineDash[0] === 10 && lineDash[1] === 10
);
const labelStrokeTextIndex = strokeTextSpy.mock.calls.findIndex(([text]) => text === 'label');
const labelStrokeTextInvocationOrder = strokeTextSpy.mock.invocationCallOrder[labelStrokeTextIndex];
const lineDashCallsBeforeLabel = setLineDashSpy.mock.calls
.map(([lineDash], index) => ({
lineDash,
invocationOrder: setLineDashSpy.mock.invocationCallOrder[index]
}))
.filter(({ invocationOrder }) => invocationOrder < labelStrokeTextInvocationOrder)
.sort((a, b) => a.invocationOrder - b.invocationOrder);
const lastLineDashBeforeLabel = lineDashCallsBeforeLabel[lineDashCallsBeforeLabel.length - 1];

expect(richTextGraphic.attribute.lineDash).toEqual([]);
expect(pointLineDashIndex).toBeGreaterThanOrEqual(0);
expect(labelStrokeTextIndex).toBeGreaterThanOrEqual(0);
expect(setLineDashSpy.mock.invocationCallOrder[pointLineDashIndex]).toBeLessThan(labelStrokeTextInvocationOrder);
expect(lastLineDashBeforeLabel?.lineDash).toEqual([]);
});
});
1 change: 1 addition & 0 deletions packages/vchart/src/mark/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class TextMark extends BaseMark<IComposedTextMarkSpec> implements ITextMa
angle: 0,
textAlign: 'center',
lineWidth: 0,
lineDash: [],
textConfig: []
};
return defaultStyle;
Expand Down
Loading