@@ -11,7 +11,7 @@ export default class Delaunay {
11
11
onLeave = ( ) => null
12
12
rect = null
13
13
14
- constructor ( { points, xAccessor, yAccessor, xScale, yScale, onPoint, onLeave, nested } ) {
14
+ constructor ( { points, xAccessor, yAccessor, xScale, yScale, onPoint, onLeave, onClick , nested } ) {
15
15
// Case 1: There is only one dimension of points (e.g. one line).
16
16
// In this case, only use the x-distance by setting all y values to zero.
17
17
// if the points are one-dimensional, treat them like that.
@@ -42,6 +42,15 @@ export default class Delaunay {
42
42
this . onPoint ( { ...this . points [ index ] , index } )
43
43
}
44
44
45
+ clickedPoint ( rawX , rawY ) {
46
+ const x = this . xScale . scaleObject . invert ( rawX )
47
+ const y = this . yScale . scaleObject . invert ( rawY )
48
+
49
+ // find nearest point
50
+ const index = this . delaunay . find ( x , y )
51
+ this . onClick ( { ...this . points [ index ] , index } )
52
+ }
53
+
45
54
mountTo ( svg ) {
46
55
this . rect = svg
47
56
. append ( 'rect' )
@@ -58,5 +67,9 @@ export default class Delaunay {
58
67
this . rect . on ( 'mouseleave' , ( ) => {
59
68
this . onLeave ( )
60
69
} )
70
+ this . rect . on ( 'click' , ( ) => {
71
+ const rawCoords = mouse ( svg . node ( ) )
72
+ this . clickedPoint ( rawCoords [ 0 ] , rawCoords [ 1 ] )
73
+ } )
61
74
}
62
75
}
0 commit comments