11import { isFunction } from '@antv/util' ;
22import { Component , isEqual as equal , ShapeStyleProps } from '@antv/f-engine' ;
3- import { ChartChildProps } from '../../chart' ;
3+ import { ChartChildProps , Chart } from '../../chart' ;
44
55function isEqual ( origin1 , origin2 , fields : string [ ] ) {
66 if ( origin1 === origin2 ) {
@@ -26,6 +26,7 @@ export interface SelectionProps {
2626 unSelectedStyle ?: ShapeStyleProps | StyleType ;
2727 cancelable ?: boolean ;
2828 onChange ?: Function ;
29+ mode : 'element-link' | 'element' | 'point' ;
2930 } ;
3031}
3132
@@ -46,21 +47,47 @@ class Selection<
4647 this . state . selected = defaultSelected ;
4748 }
4849
50+ getElementRecord ( shape ) {
51+ const origin = shape . get ( 'data' ) || { } ;
52+ return [
53+ {
54+ origin,
55+ } ,
56+ ] ;
57+ }
58+
59+ getLinkRecord ( shape , chart ) {
60+ const origin = shape . get ( 'data' ) || { } ;
61+
62+ const records = chart . getRecords ( origin , 'colorField' ) ;
63+
64+ return records ;
65+ }
66+
67+ getSelectRecords ( ev , triggerOn , mode : string , chart ) {
68+ const { points, canvasX : x , canvasY : y } = ev ;
69+ const point = triggerOn === 'click' ? { x, y } : points [ 0 ] ;
70+ if ( mode === 'element' ) return this . getElementRecord ( ev . target ) ;
71+ if ( mode === 'element-link' ) return this . getLinkRecord ( ev . target , chart ) ;
72+ return this . getSnapRecords ( point ) ;
73+ }
74+
4975 didMount ( ) {
5076 const { props, state } = this ;
51- const { selection, chart } = props ;
77+ const { selection } = props ;
5278 if ( ! selection ) return ;
5379 // 默认为 click
54- const { triggerOn = 'click' , onChange } = selection ;
80+ const { triggerOn = 'click' , onChange, mode } = selection ;
81+ // 监听在整个画布上,适用于折线图。
82+ const chart = props . chart || ( this as Chart ) ;
5583 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 ) ;
84+ const records = this . getSelectRecords ( ev , triggerOn , mode , chart ) ;
85+
5986 const { type = 'single' , cancelable = true } = selection ;
6087
6188 if ( ! records || ! records . length ) {
6289 if ( cancelable ) {
63- onChange && onChange ( { selected : null } )
90+ onChange && onChange ( { selected : null } ) ;
6491 this . setState ( {
6592 selected : null ,
6693 } as S ) ;
@@ -71,15 +98,15 @@ class Selection<
7198 const { selected } = state ;
7299 const origins = records . map ( ( record ) => record . origin ) ;
73100 if ( ! selected || ! selected . length ) {
74- onChange && onChange ( { selected : origins } )
101+ onChange && onChange ( { selected : origins } ) ;
75102 this . setState ( {
76103 selected : origins ,
77104 } as S ) ;
78105 }
79106
80107 if ( type === 'single' ) {
81108 if ( ! cancelable ) {
82- onChange && onChange ( { selected : origins } )
109+ onChange && onChange ( { selected : origins } ) ;
83110 this . setState ( {
84111 selected : origins ,
85112 } as S ) ;
@@ -91,7 +118,7 @@ class Selection<
91118 newSelected . push ( record . origin ) ;
92119 }
93120 } ) ;
94- onChange && onChange ( { selected : newSelected } )
121+ onChange && onChange ( { selected : newSelected } ) ;
95122 this . setState ( {
96123 selected : newSelected ,
97124 } as S ) ;
@@ -116,7 +143,7 @@ class Selection<
116143 . map ( ( key ) => selectedMap [ key ] )
117144 . filter ( Boolean ) ;
118145
119- onChange && onChange ( { selected : newSelected } )
146+ onChange && onChange ( { selected : newSelected } ) ;
120147 this . setState ( {
121148 selected : newSelected ,
122149 } as S ) ;
@@ -146,7 +173,7 @@ class Selection<
146173 if ( ! selected || ! selected . length ) {
147174 return false ;
148175 }
149- const { chart } = props ;
176+ const chart = props . chart || ( this as Chart ) ;
150177 const scales = chart . getScales ( ) ;
151178 const fields = Object . keys ( scales ) ;
152179 for ( let i = 0 , len = selected . length ; i < len ; i ++ ) {
0 commit comments