@@ -96,7 +96,29 @@ export function mouseDown(chart, event) {
9696 addHandler ( chart , window . document , 'keydown' , keyDown ) ;
9797}
9898
99- export function computeDragRect ( chart , mode , beginPointEvent , endPointEvent ) {
99+ function applyAspectRatio ( rect , aspectRatio , beginPoint ) {
100+ let width = rect . right - rect . left ;
101+ let height = rect . bottom - rect . top ;
102+ const ratio = width / height ;
103+
104+ if ( ratio > aspectRatio ) {
105+ width = height * aspectRatio ;
106+ if ( beginPoint . x === rect . left ) {
107+ rect . right = rect . left + width ;
108+ } else {
109+ rect . left = rect . right - width ;
110+ }
111+ } else if ( ratio < aspectRatio ) {
112+ height = width / aspectRatio ;
113+ if ( beginPoint . y === rect . top ) {
114+ rect . bottom = rect . top + height ;
115+ } else {
116+ rect . top = rect . bottom - height ;
117+ }
118+ }
119+ }
120+
121+ export function computeDragRect ( chart , mode , beginPointEvent , endPointEvent , maintainAspectRatio ) {
100122 const xEnabled = directionEnabled ( mode , 'x' , chart ) ;
101123 const yEnabled = directionEnabled ( mode , 'y' , chart ) ;
102124 let { top, left, right, bottom, width : chartWidth , height : chartHeight } = chart . chartArea ;
@@ -113,14 +135,17 @@ export function computeDragRect(chart, mode, beginPointEvent, endPointEvent) {
113135 top = Math . max ( 0 , Math . min ( beginPoint . y , endPoint . y ) ) ;
114136 bottom = Math . min ( chart . height , Math . max ( beginPoint . y , endPoint . y ) ) ;
115137 }
116- const width = right - left ;
117- const height = bottom - top ;
138+ const rect = { top, left, right, bottom} ;
139+
140+ if ( xEnabled && yEnabled && maintainAspectRatio ) {
141+ applyAspectRatio ( rect , chartWidth / chartHeight , beginPoint ) ;
142+ }
143+
144+ const width = rect . right - rect . left ;
145+ const height = rect . bottom - rect . top ;
118146
119147 return {
120- left,
121- top,
122- right,
123- bottom,
148+ ...rect ,
124149 width,
125150 height,
126151 zoomX : xEnabled && width ? 1 + ( ( chartWidth - width ) / chartWidth ) : 1 ,
@@ -135,8 +160,8 @@ export function mouseUp(chart, event) {
135160 }
136161
137162 removeHandler ( chart , 'mousemove' ) ;
138- const { mode, onZoomComplete, drag : { threshold = 0 } } = state . options . zoom ;
139- const rect = computeDragRect ( chart , mode , state . dragStart , event ) ;
163+ const { mode, onZoomComplete, drag : { threshold = 0 , maintainAspectRatio } } = state . options . zoom ;
164+ const rect = computeDragRect ( chart , mode , state . dragStart , event , maintainAspectRatio ) ;
140165 const distanceX = directionEnabled ( mode , 'x' , chart ) ? rect . width : 0 ;
141166 const distanceY = directionEnabled ( mode , 'y' , chart ) ? rect . height : 0 ;
142167 const distance = Math . sqrt ( distanceX * distanceX + distanceY * distanceY ) ;
0 commit comments