11import { isFunction } from '@antv/util' ;
22import { Component , isEqual as equal , ShapeStyleProps } from '@antv/f-engine' ;
3+ import Chart from '../../chart' ;
34import { ChartChildProps } from '../../chart' ;
45
56function isEqual ( origin1 , origin2 , fields : string [ ] ) {
@@ -26,6 +27,7 @@ export interface SelectionProps {
2627 unSelectedStyle ?: ShapeStyleProps | StyleType ;
2728 cancelable ?: boolean ;
2829 onChange ?: Function ;
30+ mode : 'element-link' | 'element' | 'point' ;
2931 } ;
3032}
3133
@@ -46,21 +48,47 @@ class Selection<
4648 this . state . selected = defaultSelected ;
4749 }
4850
51+ getElementRecord ( shape ) {
52+ const origin = shape . get ( 'data' ) || { } ;
53+ return [
54+ {
55+ origin,
56+ } ,
57+ ] ;
58+ }
59+
60+ getLinkRecord ( shape , chart ) {
61+ const origin = shape . get ( 'data' ) || { } ;
62+
63+ const records = chart . getRecords ( origin , 'colorField' ) ;
64+
65+ return records ;
66+ }
67+
68+ getSelectRecords ( ev , triggerOn , mode : string , chart ) {
69+ const { points, canvasX : x , canvasY : y } = ev ;
70+ const point = triggerOn === 'click' ? { x, y } : points [ 0 ] ;
71+ if ( mode === 'element' ) return this . getElementRecord ( ev . target ) ;
72+ if ( mode === 'element-link' ) return this . getLinkRecord ( ev . target , chart ) ;
73+ return this . getSnapRecords ( point ) ;
74+ }
75+
4976 didMount ( ) {
5077 const { props, state } = this ;
51- const { selection, chart } = props ;
78+ const { selection } = props ;
5279 if ( ! selection ) return ;
5380 // 默认为 click
54- const { triggerOn = 'click' , onChange } = selection ;
81+ const { triggerOn = 'click' , onChange, mode } = selection ;
82+ // 监听在整个画布上,适用于折线图。
83+ const chart = ( props . chart || this ) as Chart ; // 添加类型断言
5584 chart . on ( triggerOn , ( ev ) => {
56- const { points, canvasX : x , canvasY : y } = ev ;
57- const point = triggerOn === 'click' ? { x, y } : points [ 0 ] ;
58- const records = this . getSnapRecords ( point ) ;
85+ const records = this . getSelectRecords ( ev , triggerOn , mode , chart ) ;
86+
5987 const { type = 'single' , cancelable = true } = selection ;
6088
6189 if ( ! records || ! records . length ) {
6290 if ( cancelable ) {
63- onChange && onChange ( { selected : null } )
91+ onChange && onChange ( { selected : null } ) ;
6492 this . setState ( {
6593 selected : null ,
6694 } as S ) ;
@@ -71,15 +99,15 @@ class Selection<
7199 const { selected } = state ;
72100 const origins = records . map ( ( record ) => record . origin ) ;
73101 if ( ! selected || ! selected . length ) {
74- onChange && onChange ( { selected : origins } )
102+ onChange && onChange ( { selected : origins } ) ;
75103 this . setState ( {
76104 selected : origins ,
77105 } as S ) ;
78106 }
79107
80108 if ( type === 'single' ) {
81109 if ( ! cancelable ) {
82- onChange && onChange ( { selected : origins } )
110+ onChange && onChange ( { selected : origins } ) ;
83111 this . setState ( {
84112 selected : origins ,
85113 } as S ) ;
@@ -91,7 +119,7 @@ class Selection<
91119 newSelected . push ( record . origin ) ;
92120 }
93121 } ) ;
94- onChange && onChange ( { selected : newSelected } )
122+ onChange && onChange ( { selected : newSelected } ) ;
95123 this . setState ( {
96124 selected : newSelected ,
97125 } as S ) ;
@@ -116,14 +144,14 @@ class Selection<
116144 . map ( ( key ) => selectedMap [ key ] )
117145 . filter ( Boolean ) ;
118146
119- onChange && onChange ( { selected : newSelected } )
147+ onChange && onChange ( { selected : newSelected } ) ;
120148 this . setState ( {
121149 selected : newSelected ,
122150 } as S ) ;
123151 } ) ;
124152 }
125153
126- willReceiveProps ( nextProps : P ) : void {
154+ willReceiveProps ( nextProps : P , context ? ) : void {
127155 const { selection : nextSelection } = nextProps ;
128156 const { selection : lastSelection } = this . props ;
129157 if ( ! nextSelection || ! lastSelection ) {
@@ -146,7 +174,7 @@ class Selection<
146174 if ( ! selected || ! selected . length ) {
147175 return false ;
148176 }
149- const { chart } = props ;
177+ const chart = ( props . chart || this ) as Chart ;
150178 const scales = chart . getScales ( ) ;
151179 const fields = Object . keys ( scales ) ;
152180 for ( let i = 0 , len = selected . length ; i < len ; i ++ ) {
0 commit comments