@@ -3,6 +3,14 @@ import {zoom, zoomRect} from './core';
33import { callback as call , getRelativePosition , _isPointInArea } from 'chart.js/helpers' ;
44import { getState } from './state' ;
55
6+ /**
7+ * @param {number } x
8+ * @param {number } from
9+ * @param {number } to
10+ * @returns {number }
11+ */
12+ const clamp = ( x , from , to ) => Math . min ( to , Math . max ( from , x ) ) ;
13+
614function removeHandler ( chart , type ) {
715 const { handlers} = getState ( chart ) ;
816 const handler = handlers [ type ] ;
@@ -96,9 +104,9 @@ export function mouseDown(chart, event) {
96104 addHandler ( chart , window . document , 'keydown' , keyDown ) ;
97105}
98106
99- function applyAspectRatio ( endPoint , beginPoint , aspectRatio ) {
100- let width = endPoint . x - beginPoint . x ;
101- let height = endPoint . y - beginPoint . y ;
107+ function applyAspectRatio ( { begin , end } , aspectRatio ) {
108+ let width = end . x - begin . x ;
109+ let height = end . y - begin . y ;
102110 const ratio = Math . abs ( width / height ) ;
103111
104112 if ( ratio > aspectRatio ) {
@@ -107,41 +115,43 @@ function applyAspectRatio(endPoint, beginPoint, aspectRatio) {
107115 height = Math . sign ( height ) * Math . abs ( width / aspectRatio ) ;
108116 }
109117
110- endPoint . x = beginPoint . x + width ;
111- endPoint . y = beginPoint . y + height ;
118+ end . x = begin . x + width ;
119+ end . y = begin . y + height ;
112120}
113121
114- function applyMinMaxProps ( rect , beginPoint , endPoint , { min, max, prop} ) {
115- rect [ min ] = Math . max ( 0 , Math . min ( beginPoint [ prop ] , endPoint [ prop ] ) ) ;
116- rect [ max ] = Math . max ( beginPoint [ prop ] , endPoint [ prop ] ) ;
122+ function applyMinMaxProps ( rect , chartArea , points , { min, max, prop} ) {
123+ rect [ min ] = clamp ( Math . min ( points . begin [ prop ] , points . end [ prop ] ) , chartArea [ min ] , chartArea [ max ] ) ;
124+ rect [ max ] = clamp ( Math . max ( points . begin [ prop ] , points . end [ prop ] ) , chartArea [ min ] , chartArea [ max ] ) ;
117125}
118126
119- function getReplativePoints ( chart , points , maintainAspectRatio ) {
120- const beginPoint = getPointPosition ( points . dragStart , chart ) ;
121- const endPoint = getPointPosition ( points . dragEnd , chart ) ;
127+ function getRelativePoints ( chart , pointEvents , maintainAspectRatio ) {
128+ const points = {
129+ begin : getPointPosition ( pointEvents . dragStart , chart ) ,
130+ end : getPointPosition ( pointEvents . dragEnd , chart ) ,
131+ } ;
122132
123133 if ( maintainAspectRatio ) {
124134 const aspectRatio = chart . chartArea . width / chart . chartArea . height ;
125- applyAspectRatio ( endPoint , beginPoint , aspectRatio ) ;
135+ applyAspectRatio ( points , aspectRatio ) ;
126136 }
127137
128- return { beginPoint , endPoint } ;
138+ return points ;
129139}
130140
131- export function computeDragRect ( chart , mode , points , maintainAspectRatio ) {
141+ export function computeDragRect ( chart , mode , pointEvents , maintainAspectRatio ) {
132142 const xEnabled = directionEnabled ( mode , 'x' , chart ) ;
133143 const yEnabled = directionEnabled ( mode , 'y' , chart ) ;
134144 const { top, left, right, bottom, width : chartWidth , height : chartHeight } = chart . chartArea ;
135145 const rect = { top, left, right, bottom} ;
136146
137- const { beginPoint , endPoint } = getReplativePoints ( chart , points , maintainAspectRatio && xEnabled && yEnabled ) ;
147+ const points = getRelativePoints ( chart , pointEvents , maintainAspectRatio && xEnabled && yEnabled ) ;
138148
139149 if ( xEnabled ) {
140- applyMinMaxProps ( rect , beginPoint , endPoint , { min : 'left' , max : 'right' , prop : 'x' } ) ;
150+ applyMinMaxProps ( rect , chart . chartArea , points , { min : 'left' , max : 'right' , prop : 'x' } ) ;
141151 }
142152
143153 if ( yEnabled ) {
144- applyMinMaxProps ( rect , beginPoint , endPoint , { min : 'top' , max : 'bottom' , prop : 'y' } ) ;
154+ applyMinMaxProps ( rect , chart . chartArea , points , { min : 'top' , max : 'bottom' , prop : 'y' } ) ;
145155 }
146156
147157 const width = rect . right - rect . left ;
0 commit comments