@@ -25,9 +25,9 @@ export type BoxSelectOptions = {
25
25
} | null ;
26
26
brush : EChartsOption [ 'brush' ] ;
27
27
clearSelection : ( ) => void ;
28
+ floatingTriggerPosition : { left : number ; top : number } | null ;
28
29
onBrushEnd : EChartBrushEndHandler ;
29
30
onBrushStart : EChartBrushStartHandler ;
30
- pageCoords : { x : number ; y : number } | null ;
31
31
reActivateSelection : ( ) => void ;
32
32
toolBox : ToolboxComponentOption | undefined ;
33
33
} ;
@@ -64,7 +64,10 @@ export function useChartBoxSelect({
64
64
65
65
// This exposes the page coordinates when the user finishes drawing the box. This is used
66
66
// to render floating CTAs on top of the chart.
67
- const [ pageCoords , setPageCoords ] = useState < { x : number ; y : number } | null > ( null ) ;
67
+ const [ floatingTriggerPosition , setFloatingTriggerPosition ] = useState < {
68
+ left : number ;
69
+ top : number ;
70
+ } | null > ( null ) ;
68
71
69
72
// This increments a counter to force a re-activation of the brush mode. We expose the
70
73
// re-activation function in the return value, so that the parent component can call it
@@ -96,47 +99,50 @@ export function useChartBoxSelect({
96
99
97
100
const area = evt . areas [ 0 ] ;
98
101
102
+ const [ x0 , x1 ] = area . coordRange [ 0 ] ;
103
+ const [ y0 , y1 ] = area . coordRange [ 1 ] ;
104
+
105
+ const clampedCoordRange : [ [ number , number ] , [ number , number ] ] = [
106
+ [ Math . max ( xMin , x0 ) , Math . min ( xMax , x1 ) ] ,
107
+ [ Math . max ( yMin , y0 ) , Math . min ( yMax , y1 ) ] ,
108
+ ] ;
109
+
99
110
const newBrushArea : EchartBrushAreas = [
100
111
{
101
112
...area ,
102
- coordRange : [
103
- [
104
- Math . max ( xMin , area . coordRange [ 0 ] [ 0 ] ) ,
105
- Math . min ( xMax , area . coordRange [ 0 ] [ 1 ] ) ,
106
- ] ,
107
- [
108
- Math . max ( yMin , area . coordRange [ 1 ] [ 0 ] ) ,
109
- Math . min ( yMax , area . coordRange [ 1 ] [ 1 ] ) ,
110
- ] ,
111
- ] ,
113
+ coordRange : clampedCoordRange ,
112
114
} ,
113
115
] ;
114
116
115
117
setBrushArea ( newBrushArea ) ;
116
- } ,
117
- [ chartRef ]
118
- ) ;
119
118
120
- useEffect ( ( ) => {
121
- const handleMouseUp = ( e : MouseEvent ) => {
122
- if ( brushArea ) {
123
- setPageCoords ( { x : e . clientX , y : e . clientY + window . scrollY } ) ;
124
- } else {
125
- setPageCoords ( null ) ;
126
- }
127
- } ;
119
+ // Get the bottom right coordinates of the box
120
+ const [ clamped_x1 , clamped_y0 ] = [ clampedCoordRange [ 0 ] [ 1 ] , clampedCoordRange [ 1 ] [ 0 ] ] ;
128
121
129
- const wrapper = chartWrapperRef . current ;
130
- if ( ! wrapper ) return ;
122
+ // Convert the bottom right coordinates to pixel coordinates, so that we can use them to
123
+ // absolutely position the floating CTAs.
124
+ const [ clamped_x1_pixels , clamped_y0_pixels ] = chart . convertToPixel (
125
+ { xAxisIndex : 0 , yAxisIndex : 0 } ,
126
+ [ clamped_x1 , clamped_y0 ]
127
+ ) ;
131
128
132
- wrapper . addEventListener ( 'mouseup' , handleMouseUp ) ;
133
- } , [ brushArea , chartWrapperRef ] ) ;
129
+ const chartRect = chart . getDom ( ) . getBoundingClientRect ( ) ;
130
+
131
+ if ( chartRect ) {
132
+ setFloatingTriggerPosition ( {
133
+ left : chartRect . left + clamped_x1_pixels ,
134
+ top : chartRect . top + clamped_y0_pixels + window . scrollY ,
135
+ } ) ;
136
+ }
137
+ } ,
138
+ [ chartRef ]
139
+ ) ;
134
140
135
141
const clearSelection = useCallback ( ( ) => {
136
142
const chartInstance = chartRef . current ?. getEchartsInstance ( ) ;
137
143
chartInstance ?. dispatchAction ( { type : 'brush' , areas : [ ] } ) ;
138
144
setBrushArea ( null ) ;
139
- setPageCoords ( null ) ;
145
+ setFloatingTriggerPosition ( null ) ;
140
146
} , [ chartRef ] ) ;
141
147
142
148
const handleOutsideClick = useCallback (
@@ -199,7 +205,7 @@ export function useChartBoxSelect({
199
205
chartRef . current ,
200
206
enableBrushMode ,
201
207
handleOutsideClick ,
202
- pageCoords ,
208
+ floatingTriggerPosition ,
203
209
forceReActivateSelection ,
204
210
] ) ;
205
211
@@ -237,7 +243,7 @@ export function useChartBoxSelect({
237
243
onBrushEnd,
238
244
onBrushStart,
239
245
toolBox,
240
- pageCoords ,
246
+ floatingTriggerPosition ,
241
247
reActivateSelection,
242
248
clearSelection,
243
249
} ;
@@ -247,7 +253,7 @@ export function useChartBoxSelect({
247
253
brush ,
248
254
toolBox ,
249
255
onBrushStart ,
250
- pageCoords ,
256
+ floatingTriggerPosition ,
251
257
reActivateSelection ,
252
258
clearSelection ,
253
259
] ) ;
0 commit comments