@@ -9,21 +9,30 @@ const interpolateX = (y, p1, p2) => pointInLine(p1, p2, Math.abs((y - p1.y) / (p
99const interpolateY = ( x , p1 , p2 ) => pointInLine ( p1 , p2 , Math . abs ( ( x - p1 . x ) / ( p2 . x - p1 . x ) ) ) . y ;
1010const toPercent = ( s ) => typeof s === 'string' && s . endsWith ( '%' ) && parseFloat ( s ) / 100 ;
1111
12+ function isLineInArea ( { x, y, x2, y2} , { top, right, bottom, left} ) {
13+ return ! (
14+ ( x < left && x2 < left ) ||
15+ ( x > right && x2 > right ) ||
16+ ( y < top && y2 < top ) ||
17+ ( y > bottom && y2 > bottom )
18+ ) ;
19+ }
20+
1221function limitPointToArea ( { x, y} , p2 , { top, right, bottom, left} ) {
1322 if ( x < left ) {
14- y = p2 . x < left ? NaN : interpolateY ( left , { x, y} , p2 ) ;
23+ y = interpolateY ( left , { x, y} , p2 ) ;
1524 x = left ;
1625 }
1726 if ( x > right ) {
18- y = p2 . x > right ? NaN : interpolateY ( right , { x, y} , p2 ) ;
27+ y = interpolateY ( right , { x, y} , p2 ) ;
1928 x = right ;
2029 }
2130 if ( y < top ) {
22- x = p2 . y < top ? NaN : interpolateX ( top , { x, y} , p2 ) ;
31+ x = interpolateX ( top , { x, y} , p2 ) ;
2332 y = top ;
2433 }
2534 if ( y > bottom ) {
26- x = p2 . y > bottom ? NaN : interpolateX ( bottom , { x, y} , p2 ) ;
35+ x = interpolateX ( bottom , { x, y} , p2 ) ;
2736 y = bottom ;
2837 }
2938 return { x, y} ;
@@ -58,9 +67,11 @@ export default class LineAnnotation extends Element {
5867 return ( sqr ( x - xx ) + sqr ( y - yy ) ) < epsilon ;
5968 }
6069
61- labelIsVisible ( ) {
70+ labelIsVisible ( chartArea ) {
6271 const label = this . options . label ;
63- return label && label . enabled && label . content ;
72+
73+ const inside = ! chartArea || isLineInArea ( this , chartArea ) ;
74+ return inside && label && label . enabled && label . content ;
6475 }
6576
6677 isOnLabel ( mouseX , mouseY ) {
@@ -107,7 +118,7 @@ export default class LineAnnotation extends Element {
107118 }
108119
109120 drawLabel ( ctx , chartArea ) {
110- if ( this . labelIsVisible ( ) ) {
121+ if ( this . labelIsVisible ( chartArea ) ) {
111122 ctx . save ( ) ;
112123 drawLabel ( ctx , this , chartArea ) ;
113124 ctx . restore ( ) ;
@@ -143,7 +154,10 @@ export default class LineAnnotation extends Element {
143154 y2 = scaleValue ( yScale , options . yMax , y2 ) ;
144155 }
145156 }
146- return limitLineToArea ( { x, y} , { x : x2 , y : y2 } , chart . chartArea ) ;
157+ const inside = isLineInArea ( { x, y, x2, y2} , chart . chartArea ) ;
158+ return inside
159+ ? limitLineToArea ( { x, y} , { x : x2 , y : y2 } , chart . chartArea )
160+ : { x, y, x2, y2, width : Math . abs ( x2 - x ) , height : Math . abs ( y2 - y ) } ;
147161 }
148162}
149163
0 commit comments