@@ -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 ] ;
@@ -111,12 +119,12 @@ function applyAspectRatio(endPoint, beginPoint, aspectRatio) {
111
119
endPoint . y = beginPoint . 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 , beginPoint , endPoint , { min, max, prop} ) {
123
+ rect [ min ] = clamp ( Math . min ( beginPoint [ prop ] , endPoint [ prop ] ) , chartArea [ min ] , chartArea [ max ] ) ;
124
+ rect [ max ] = clamp ( Math . max ( beginPoint [ prop ] , endPoint [ prop ] ) , chartArea [ min ] , chartArea [ max ] ) ;
117
125
}
118
126
119
- function getReplativePoints ( chart , points , maintainAspectRatio ) {
127
+ function getRelativePoints ( chart , points , maintainAspectRatio ) {
120
128
const beginPoint = getPointPosition ( points . dragStart , chart ) ;
121
129
const endPoint = getPointPosition ( points . dragEnd , chart ) ;
122
130
@@ -134,14 +142,14 @@ export function computeDragRect(chart, mode, points, maintainAspectRatio) {
134
142
const { top, left, right, bottom, width : chartWidth , height : chartHeight } = chart . chartArea ;
135
143
const rect = { top, left, right, bottom} ;
136
144
137
- const { beginPoint, endPoint} = getReplativePoints ( chart , points , maintainAspectRatio && xEnabled && yEnabled ) ;
145
+ const { beginPoint, endPoint} = getRelativePoints ( chart , points , maintainAspectRatio && xEnabled && yEnabled ) ;
138
146
139
147
if ( xEnabled ) {
140
- applyMinMaxProps ( rect , beginPoint , endPoint , { min : 'left' , max : 'right' , prop : 'x' } ) ;
148
+ applyMinMaxProps ( rect , chart . chartArea , beginPoint , endPoint , { min : 'left' , max : 'right' , prop : 'x' } ) ;
141
149
}
142
150
143
151
if ( yEnabled ) {
144
- applyMinMaxProps ( rect , beginPoint , endPoint , { min : 'top' , max : 'bottom' , prop : 'y' } ) ;
152
+ applyMinMaxProps ( rect , chart . chartArea , beginPoint , endPoint , { min : 'top' , max : 'bottom' , prop : 'y' } ) ;
145
153
}
146
154
147
155
const width = rect . right - rect . left ;
0 commit comments