@@ -41,36 +41,47 @@ export type ShapePoint = {
4141 series : ChartSeries ;
4242} ;
4343
44- function getClosestPointsByXValue ( x : number , y : number , points : ShapePoint [ ] ) {
45- const sorted = sort ( points , ( p ) => p . x ) ;
46- const closestXIndex = bisector < ShapePoint , number > ( ( p ) => p . x ) . center ( sorted , x ) ;
47- if ( closestXIndex === - 1 ) {
48- return [ ] ;
49- }
50-
51- const closestX = sorted [ closestXIndex ] . x ;
52- const closestPoints = sort (
53- points . filter ( ( p ) => p . x === closestX ) ,
54- ( p ) => p . y0 ,
55- ) ;
56-
44+ function getClosestYIndex ( items : ShapePoint [ ] , y : number ) {
5745 let closestYIndex = - 1 ;
58- if ( y < closestPoints [ 0 ] ?. y0 ) {
46+ if ( y < items [ 0 ] ?. y0 ) {
5947 closestYIndex = 0 ;
60- } else if ( y > closestPoints [ closestPoints . length - 1 ] ?. y1 ) {
61- closestYIndex = closestPoints . length - 1 ;
48+ } else if ( y > items [ items . length - 1 ] ?. y1 ) {
49+ closestYIndex = items . length - 1 ;
6250 } else {
63- closestYIndex = closestPoints . findIndex ( ( p ) => y > p . y0 && y < p . y1 ) ;
51+ closestYIndex = items . findIndex ( ( p ) => y > p . y0 && y < p . y1 ) ;
6452 if ( closestYIndex === - 1 ) {
6553 const sortedY = sort (
66- closestPoints . map ( ( p , index ) => ( { index, y : p . y1 + ( p . y0 - p . y1 ) / 2 } ) ) ,
54+ items . map ( ( p , index ) => ( { index, y : p . y1 + ( p . y0 - p . y1 ) / 2 } ) ) ,
6755 ( p ) => p . y ,
6856 ) ;
6957 const sortedYIndex = bisector < { y : number } , number > ( ( p ) => p . y ) . center ( sortedY , y ) ;
7058 closestYIndex = sortedY [ sortedYIndex ] ?. index ?? - 1 ;
7159 }
7260 }
7361
62+ return closestYIndex ;
63+ }
64+
65+ function getClosestPointsByXValue ( x : number , y : number , points : ShapePoint [ ] ) {
66+ const sorted = sort ( points , ( p ) => p . x ) ;
67+ const closestXIndex = bisector < ShapePoint , number > ( ( p ) => p . x ) . center ( sorted , x ) ;
68+ if ( closestXIndex === - 1 ) {
69+ return [ ] ;
70+ }
71+
72+ const closestX = sorted [ closestXIndex ] . x ;
73+ const filtered = points . filter ( ( p ) => p . x === closestX ) ;
74+
75+ const groupedBySeries = Object . values ( groupBy ( filtered , ( p ) => get ( p . series , 'id' ) ) ) . map (
76+ ( items ) => {
77+ const sortedByY = sort ( items , ( p ) => p . y0 ) ;
78+ const index = getClosestYIndex ( sortedByY , y ) ;
79+ return sortedByY [ index === - 1 ? 0 : index ] ;
80+ } ,
81+ ) ;
82+
83+ const closestPoints = sort ( groupedBySeries , ( p ) => p . y0 ) ;
84+ const closestYIndex = getClosestYIndex ( closestPoints , y ) ;
7485 return closestPoints . map ( ( p , i ) => ( {
7586 data : p . data ,
7687 series : p . series ,
0 commit comments