Skip to content

Commit 2b0c472

Browse files
committed
fix: annotation markers render when parent size is computed
1 parent 1fd26fb commit 2b0c472

2 files changed

Lines changed: 29 additions & 28 deletions

File tree

.playground/playground.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License. */
1818

1919
import React from 'react';
20-
import { Example } from '../stories/treemap/6_custom_style';
20+
import { Example } from '../stories/annotations/lines/3_x_time';
2121

2222
export class Playground extends React.Component {
2323
render() {

src/chart_types/xy_chart/renderer/dom/annotations/annotations.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ interface AnnotationsOwnProps {
5555

5656
type AnnotationsProps = AnnotationsDispatchProps & AnnotationsStateProps & AnnotationsOwnProps;
5757

58+
function renderAnnotationLineMarkers(
59+
chartDimensions: Dimensions,
60+
annotationLines: AnnotationLineProps[],
61+
id: AnnotationId,
62+
) {
63+
return annotationLines.reduce<JSX.Element[]>((markers, { marker }: AnnotationLineProps, index: number) => {
64+
if (!marker) {
65+
return markers;
66+
}
67+
68+
const { icon, color, position } = marker;
69+
const style = {
70+
color,
71+
top: chartDimensions.top + position.top,
72+
left: chartDimensions.left + position.left,
73+
};
74+
75+
markers.push(
76+
<div className="echAnnotation" style={{ ...style }} key={`annotation-${id}-${index}`}>
77+
{icon}
78+
</div>,
79+
);
80+
81+
return markers;
82+
}, []);
83+
}
5884
const AnnotationsComponent = ({
5985
tooltipState,
6086
isChartEmpty,
@@ -65,31 +91,6 @@ const AnnotationsComponent = ({
6591
chartId,
6692
onPointerMove,
6793
}: AnnotationsProps) => {
68-
const renderAnnotationLineMarkers = useCallback(
69-
(annotationLines: AnnotationLineProps[], id: AnnotationId) =>
70-
annotationLines.reduce<JSX.Element[]>((markers, { marker }: AnnotationLineProps, index: number) => {
71-
if (!marker) {
72-
return markers;
73-
}
74-
75-
const { icon, color, position } = marker;
76-
const style = {
77-
color,
78-
top: chartDimensions.top + position.top,
79-
left: chartDimensions.left + position.left,
80-
};
81-
82-
markers.push(
83-
<div className="echAnnotation" style={{ ...style }} key={`annotation-${id}-${index}`}>
84-
{icon}
85-
</div>,
86-
);
87-
88-
return markers;
89-
}, []),
90-
[], // eslint-disable-line react-hooks/exhaustive-deps
91-
);
92-
9394
const renderAnnotationMarkers = useCallback((): JSX.Element[] => {
9495
const markers: JSX.Element[] = [];
9596

@@ -101,13 +102,13 @@ const AnnotationsComponent = ({
101102

102103
if (isLineAnnotation(annotationSpec)) {
103104
const annotationLines = dimensions as AnnotationLineProps[];
104-
const lineMarkers = renderAnnotationLineMarkers(annotationLines, id);
105+
const lineMarkers = renderAnnotationLineMarkers(chartDimensions, annotationLines, id);
105106
markers.push(...lineMarkers);
106107
}
107108
});
108109

109110
return markers;
110-
}, [annotationDimensions, annotationSpecs, renderAnnotationLineMarkers]);
111+
}, [chartDimensions, annotationDimensions, annotationSpecs]);
111112

112113
const onScroll = useCallback(() => {
113114
onPointerMove({ x: -1, y: -1 }, new Date().getTime());

0 commit comments

Comments
 (0)