diff --git a/__tests__/integration/snapshots/static/aaplLineMissingLabel.png b/__tests__/integration/snapshots/static/aaplLineMissingLabel.png new file mode 100644 index 0000000000..9bceda8100 Binary files /dev/null and b/__tests__/integration/snapshots/static/aaplLineMissingLabel.png differ diff --git a/__tests__/plots/static/aapl-line-missing-label.ts b/__tests__/plots/static/aapl-line-missing-label.ts new file mode 100644 index 0000000000..9ce2d4d764 --- /dev/null +++ b/__tests__/plots/static/aapl-line-missing-label.ts @@ -0,0 +1,31 @@ +import { G2Spec } from '../../../src'; + +export function aaplLineMissingLabel(): G2Spec { + return { + type: 'line', + data: [ + { time: '10:10', call: 4, waiting: 2, people: 2 }, + { time: '10:15', call: 2, waiting: 6, people: 3 }, + { time: '10:20', call: 13, waiting: 2 }, + { time: '10:25', call: 9, waiting: 9, people: 1 }, + { time: '10:30', call: 5, waiting: 2, people: 3 }, + { time: '10:35', call: 8, waiting: 2, people: 1 }, + { time: '10:40', call: 13, waiting: 1, people: 2 }, + ], + encode: { + x: 'time', + y: 'people', + }, + labels: [ + { + text: 'people', + dy: -20, + connector: true, + connectorStroke: 'red', + connectorLineWidth: 2, + }, + ], + }; +} + +aaplLineMissingLabel.maxError = 100; diff --git a/__tests__/plots/static/index.ts b/__tests__/plots/static/index.ts index d037ebb756..92a1be8089 100644 --- a/__tests__/plots/static/index.ts +++ b/__tests__/plots/static/index.ts @@ -63,6 +63,7 @@ export { unemploymentLineMultiSeries } from './unemployment-line-multi-series'; export { receiptsLineSlope } from './receipts-line-slope'; export { stocksLineAggregateLabel } from './stocks-line-aggregate-label'; export { aaplLineMissing } from './aapl-line-missing'; +export { aaplLineMissingLabel } from './aapl-line-missing-label'; export { aaplLineMissingConnect } from './aapl-line-missing-connect'; export { aaplLineMissingStyled } from './aapl-line-missing-styled'; export { paragraphTextVis } from './paragraph-text-vis'; diff --git a/src/runtime/plot.ts b/src/runtime/plot.ts index 5e3538f87d..93406d96ad 100644 --- a/src/runtime/plot.ts +++ b/src/runtime/plot.ts @@ -1031,16 +1031,14 @@ function getLabels( ]; } const selector = normalizeLabelSelector(label); - const F = SI.filter((_, i) => points[i].every(defined)).map( - (index: number, i: number) => ({ - ...label, - key: `${seriesKey[i]}-${labelIndex}`, - bounds: [points[i]], - index, - points, - dependentElement: element, - }), - ); + const F = SI.map((index: number, i: number) => ({ + ...label, + key: `${seriesKey[i]}-${labelIndex}`, + bounds: [points[i]], + index, + points, + dependentElement: element, + })); return selector ? selector(F) : F; } diff --git a/src/shape/text/advance.ts b/src/shape/text/advance.ts index 1c42fc0f56..3c46287f80 100644 --- a/src/shape/text/advance.ts +++ b/src/shape/text/advance.ts @@ -7,6 +7,7 @@ import { RectStyleProps, PathStyleProps, } from '@antv/g'; +import { isNumber } from '@antv/util'; import { Marker } from '@antv/gui'; import { line } from 'd3-shape'; import { WithPrefix } from '../../runtime'; @@ -103,7 +104,7 @@ function inferConnectorPath( } const P: any = [[x0 - x1, y0 - y1]].concat( - controlPoints.length ? controlPoints : [0, 0], + controlPoints.length ? controlPoints : [[0, 0]], ); const p0 = [coordCenter[0] - x1, coordCenter[1] - y1] as Vector2; @@ -142,6 +143,9 @@ export const Advance = createElement((g) => { coordCenter, ...rest } = g.attributes as TextShapeStyleProps; + // Position is invalid, do not render the UI. + if ([x, y, x0, y0].some((v) => !isNumber(v))) return; + const { padding, ...backgroundStyle } = subObject(rest, 'background'); const { points = [], ...connectorStyle } = subObject(rest, 'connector'); const endPoints: Vector2[] = [