1
1
import { DataContext } from '@visx/xychart' ;
2
2
import { useEffect , useState , useCallback } from 'react' ;
3
- import LineChartAnnotation from './line- chart-annotation ' ;
3
+ import { useChartContext } from '../../providers/ chart-context ' ;
4
4
import styles from './line-chart.module.scss' ;
5
- import type { LineChartRef } from './line-chart' ;
6
- import type { LineChartAnnotationProps } from './line-chart-annotation' ;
7
5
import type { AxisScale } from '@visx/axis' ;
8
- import type { FC , RefObject } from 'react' ;
6
+ import type { FC , ReactNode } from 'react' ;
9
7
10
8
interface LineChartAnnotationsProps {
11
- chartRef : RefObject < LineChartRef > ;
12
- annotations : LineChartAnnotationProps [ ] ;
13
- chartWidth : number ;
14
- chartHeight : number ;
9
+ chartId : string ;
10
+ children ?: ReactNode ;
15
11
}
16
12
17
13
interface ScaleData {
18
14
xScale : AxisScale < Date > ;
19
15
yScale : AxisScale < number > ;
20
16
}
21
17
22
- const LineChartAnnotationsOverlay : FC < LineChartAnnotationsProps > = ( {
23
- chartRef,
24
- annotations,
25
- chartWidth,
26
- chartHeight,
27
- } ) => {
18
+ const LineChartAnnotationsOverlay : FC < LineChartAnnotationsProps > = ( { chartId, children } ) => {
19
+ const { getChartData } = useChartContext ( ) ;
20
+
21
+ let chartData = null ;
22
+ if ( chartId ) {
23
+ chartData = getChartData ( chartId ) ;
24
+ }
25
+
26
+ const chartRef = chartData ?. chartRef ;
27
+ const chartWidth = chartData ?. chartWidth || 0 ;
28
+ const chartHeight = chartData ?. chartHeight || 0 ;
29
+
28
30
const [ scales , setScales ] = useState < ScaleData | null > ( null ) ;
29
31
const [ scalesStable , setScalesStable ] = useState < boolean > ( false ) ;
30
32
@@ -42,7 +44,7 @@ const LineChartAnnotationsOverlay: FC< LineChartAnnotationsProps > = ( {
42
44
43
45
// Get scales from chart ref and return them with signature for comparison
44
46
const getScalesData = useCallback ( ( ) => {
45
- if ( chartRef . current ) {
47
+ if ( chartRef ? .current ) {
46
48
const scaleData = chartRef . current . getScales ( ) ;
47
49
48
50
if ( scaleData ) {
@@ -107,6 +109,11 @@ const LineChartAnnotationsOverlay: FC< LineChartAnnotationsProps > = ( {
107
109
} ;
108
110
} , [ getScalesData , chartWidth , chartHeight ] ) ;
109
111
112
+ // Early return if no chart data available
113
+ if ( ! chartRef || ! children ) {
114
+ return null ;
115
+ }
116
+
110
117
if ( ! scales || ! scalesStable ) {
111
118
return null ;
112
119
}
@@ -116,16 +123,9 @@ const LineChartAnnotationsOverlay: FC< LineChartAnnotationsProps > = ( {
116
123
const dataContextValue = {
117
124
xScale : scales . xScale ,
118
125
yScale : scales . yScale ,
119
- // Add minimal required properties for DataContext
120
- theme : { } ,
121
126
margin : { top : 0 , right : 0 , bottom : 0 , left : 0 } ,
122
127
width : chartWidth ,
123
128
height : chartHeight ,
124
- // Additional visx DataContext properties that may be needed
125
- colorScale : undefined ,
126
- dataRegistry : new Map ( ) ,
127
- registerData : ( ) => { } ,
128
- unregisterData : ( ) => { } ,
129
129
} as unknown as Parameters < typeof DataContext . Provider > [ 0 ] [ 'value' ] ;
130
130
131
131
return (
@@ -136,13 +136,7 @@ const LineChartAnnotationsOverlay: FC< LineChartAnnotationsProps > = ( {
136
136
className = { styles [ 'line-chart__annotations-overlay' ] }
137
137
data-testid = "line-chart-annotations-overlay"
138
138
>
139
- { annotations . map ( ( annotation , index ) => (
140
- < LineChartAnnotation
141
- key = { `overlay-annotation-${ index } ` }
142
- testId = { `overlay-annotation-${ index } ` }
143
- { ...annotation }
144
- />
145
- ) ) }
139
+ { children }
146
140
</ svg >
147
141
</ DataContext . Provider >
148
142
) ;
0 commit comments