@@ -3,6 +3,14 @@ import {zoom, zoomRect} from './core';
3
3
import { callback as call , getRelativePosition , _isPointInArea } from 'chart.js/helpers' ;
4
4
import { getState } from './state' ;
5
5
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
+
6
14
function removeHandler ( chart , type ) {
7
15
const { handlers} = getState ( chart ) ;
8
16
const handler = handlers [ type ] ;
@@ -96,9 +104,9 @@ export function mouseDown(chart, event) {
96
104
addHandler ( chart , window . document , 'keydown' , keyDown ) ;
97
105
}
98
106
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 ;
102
110
const ratio = Math . abs ( width / height ) ;
103
111
104
112
if ( ratio > aspectRatio ) {
@@ -107,41 +115,43 @@ function applyAspectRatio(endPoint, beginPoint, aspectRatio) {
107
115
height = Math . sign ( height ) * Math . abs ( width / aspectRatio ) ;
108
116
}
109
117
110
- endPoint . x = beginPoint . x + width ;
111
- endPoint . y = beginPoint . y + height ;
118
+ end . x = begin . x + width ;
119
+ end . y = begin . y + height ;
112
120
}
113
121
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 ] ) ;
117
125
}
118
126
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
+ } ;
122
132
123
133
if ( maintainAspectRatio ) {
124
134
const aspectRatio = chart . chartArea . width / chart . chartArea . height ;
125
- applyAspectRatio ( endPoint , beginPoint , aspectRatio ) ;
135
+ applyAspectRatio ( points , aspectRatio ) ;
126
136
}
127
137
128
- return { beginPoint , endPoint } ;
138
+ return points ;
129
139
}
130
140
131
- export function computeDragRect ( chart , mode , points , maintainAspectRatio ) {
141
+ export function computeDragRect ( chart , mode , pointEvents , maintainAspectRatio ) {
132
142
const xEnabled = directionEnabled ( mode , 'x' , chart ) ;
133
143
const yEnabled = directionEnabled ( mode , 'y' , chart ) ;
134
144
const { top, left, right, bottom, width : chartWidth , height : chartHeight } = chart . chartArea ;
135
145
const rect = { top, left, right, bottom} ;
136
146
137
- const { beginPoint , endPoint } = getReplativePoints ( chart , points , maintainAspectRatio && xEnabled && yEnabled ) ;
147
+ const points = getRelativePoints ( chart , pointEvents , maintainAspectRatio && xEnabled && yEnabled ) ;
138
148
139
149
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' } ) ;
141
151
}
142
152
143
153
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' } ) ;
145
155
}
146
156
147
157
const width = rect . right - rect . left ;
0 commit comments