@@ -86,46 +86,59 @@ export function mouseDown(chart, event) {
8686 addHandler ( chart , window . document , 'keydown' , keyDown ) ;
8787}
8888
89+ function applyDimensions ( rect , width , height , beginPoint ) {
90+ if ( beginPoint . x === rect . left ) {
91+ rect . right = rect . left + width ;
92+ } else {
93+ rect . left = rect . right - width ;
94+ }
95+ if ( beginPoint . y === rect . top ) {
96+ rect . bottom = rect . top + height ;
97+ } else {
98+ rect . top = rect . bottom - height ;
99+ }
100+ }
101+
89102function applyAspectRatio ( rect , aspectRatio , beginPoint ) {
90103 let width = rect . right - rect . left ;
91104 let height = rect . bottom - rect . top ;
92105 const ratio = width / height ;
93106
94107 if ( ratio > aspectRatio ) {
95108 width = height * aspectRatio ;
96- if ( beginPoint . x === rect . left ) {
97- rect . right = rect . left + width ;
98- } else {
99- rect . left = rect . right - width ;
100- }
101109 } else if ( ratio < aspectRatio ) {
102110 height = width / aspectRatio ;
103- if ( beginPoint . y === rect . top ) {
104- rect . bottom = rect . top + height ;
105- } else {
106- rect . top = rect . bottom - height ;
107- }
108111 }
112+
113+ applyDimensions ( rect , width , height , beginPoint ) ;
114+ }
115+
116+ function applyX ( rect , beginPoint , endPoint ) {
117+ rect . left = Math . min ( beginPoint . x , endPoint . x ) ;
118+ rect . right = Math . max ( beginPoint . x , endPoint . x ) ;
109119}
110120
111- export function computeDragRect ( chart , mode , beginPointEvent , endPointEvent , maintainAspectRatio ) {
121+ function applyY ( rect , beginPoint , endPoint ) {
122+ rect . top = Math . min ( beginPoint . y , endPoint . y ) ;
123+ rect . bottom = Math . max ( beginPoint . y , endPoint . y ) ;
124+ }
125+
126+ export function computeDragRect ( chart , mode , points , maintainAspectRatio ) {
112127 const xEnabled = directionEnabled ( mode , 'x' , chart ) ;
113128 const yEnabled = directionEnabled ( mode , 'y' , chart ) ;
114- let { top, left, right, bottom, width : chartWidth , height : chartHeight } = chart . chartArea ;
129+ const { top, left, right, bottom, width : chartWidth , height : chartHeight } = chart . chartArea ;
130+ const rect = { top, left, right, bottom} ;
115131
116- const beginPoint = getRelativePosition ( beginPointEvent , chart ) ;
117- const endPoint = getRelativePosition ( endPointEvent , chart ) ;
132+ const beginPoint = getRelativePosition ( points . dragStart , chart ) ;
133+ const endPoint = getRelativePosition ( points . dragEnd , chart ) ;
118134
119135 if ( xEnabled ) {
120- left = Math . min ( beginPoint . x , endPoint . x ) ;
121- right = Math . max ( beginPoint . x , endPoint . x ) ;
136+ applyX ( rect , beginPoint , endPoint ) ;
122137 }
123138
124139 if ( yEnabled ) {
125- top = Math . min ( beginPoint . y , endPoint . y ) ;
126- bottom = Math . max ( beginPoint . y , endPoint . y ) ;
140+ applyY ( rect , beginPoint , endPoint ) ;
127141 }
128- const rect = { top, left, right, bottom} ;
129142
130143 if ( xEnabled && yEnabled && maintainAspectRatio ) {
131144 applyAspectRatio ( rect , chartWidth / chartHeight , beginPoint ) ;
@@ -151,7 +164,7 @@ export function mouseUp(chart, event) {
151164
152165 removeHandler ( chart , 'mousemove' ) ;
153166 const { mode, onZoomComplete, drag : { threshold = 0 , maintainAspectRatio} } = state . options . zoom ;
154- const rect = computeDragRect ( chart , mode , state . dragStart , event , maintainAspectRatio ) ;
167+ const rect = computeDragRect ( chart , mode , { dragStart : state . dragStart , dragEnd : event } , maintainAspectRatio ) ;
155168 const distanceX = directionEnabled ( mode , 'x' , chart ) ? rect . width : 0 ;
156169 const distanceY = directionEnabled ( mode , 'y' , chart ) ? rect . height : 0 ;
157170 const distance = Math . sqrt ( distanceX * distanceX + distanceY * distanceY ) ;
0 commit comments