1
- import { clipArea , unclipArea } from '../../helpers/index.js' ;
1
+ import { clipArea , unclipArea , getDatasetClipArea } from '../../helpers/index.js' ;
2
2
import { _findSegmentEnd , _getBounds , _segments } from './filler.segment.js' ;
3
3
import { _getTarget } from './filler.target.js' ;
4
4
5
5
export function _drawfill ( ctx , source , area ) {
6
6
const target = _getTarget ( source ) ;
7
- const { line, scale, axis} = source ;
7
+ const { chart , index , line, scale, axis} = source ;
8
8
const lineOpts = line . options ;
9
9
const fillOption = lineOpts . fill ;
10
10
const color = lineOpts . backgroundColor ;
11
11
const { above = color , below = color } = fillOption || { } ;
12
+ const meta = chart . getDatasetMeta ( index ) ;
13
+ const clip = getDatasetClipArea ( chart , meta ) ;
12
14
if ( target && line . points . length ) {
13
15
clipArea ( ctx , area ) ;
14
- doFill ( ctx , { line, target, above, below, area, scale, axis} ) ;
16
+ doFill ( ctx , { line, target, above, below, area, scale, axis, clip } ) ;
15
17
unclipArea ( ctx ) ;
16
18
}
17
19
}
18
20
19
21
function doFill ( ctx , cfg ) {
20
- const { line, target, above, below, area, scale} = cfg ;
22
+ const { line, target, above, below, area, scale, clip } = cfg ;
21
23
const property = line . _loop ? 'angle' : cfg . axis ;
22
24
23
25
ctx . save ( ) ;
24
26
25
27
if ( property === 'x' && below !== above ) {
26
28
clipVertical ( ctx , target , area . top ) ;
27
- fill ( ctx , { line, target, color : above , scale, property} ) ;
29
+ fill ( ctx , { line, target, color : above , scale, property, clip } ) ;
28
30
ctx . restore ( ) ;
29
31
ctx . save ( ) ;
30
32
clipVertical ( ctx , target , area . bottom ) ;
31
33
}
32
- fill ( ctx , { line, target, color : below , scale, property} ) ;
34
+ fill ( ctx , { line, target, color : below , scale, property, clip } ) ;
33
35
34
36
ctx . restore ( ) ;
35
37
}
@@ -65,7 +67,7 @@ function clipVertical(ctx, target, clipY) {
65
67
}
66
68
67
69
function fill ( ctx , cfg ) {
68
- const { line, target, property, color, scale} = cfg ;
70
+ const { line, target, property, color, scale, clip } = cfg ;
69
71
const segments = _segments ( line , target , property ) ;
70
72
71
73
for ( const { source : src , target : tgt , start, end} of segments ) {
@@ -75,7 +77,7 @@ function fill(ctx, cfg) {
75
77
ctx . save ( ) ;
76
78
ctx . fillStyle = backgroundColor ;
77
79
78
- clipBounds ( ctx , scale , notShape && _getBounds ( property , start , end ) ) ;
80
+ clipBounds ( ctx , scale , clip , notShape && _getBounds ( property , start , end ) ) ;
79
81
80
82
ctx . beginPath ( ) ;
81
83
@@ -103,12 +105,35 @@ function fill(ctx, cfg) {
103
105
}
104
106
}
105
107
106
- function clipBounds ( ctx , scale , bounds ) {
107
- const { top , bottom } = scale . chart . chartArea ;
108
+ function clipBounds ( ctx , scale , clip , bounds ) {
109
+ const chartArea = scale . chart . chartArea ;
108
110
const { property, start, end} = bounds || { } ;
109
- if ( property === 'x' ) {
111
+
112
+ if ( property === 'x' || property === 'y' ) {
113
+ let left , top , right , bottom ;
114
+
115
+ if ( property === 'x' ) {
116
+ left = start ;
117
+ top = chartArea . top ;
118
+ right = end ;
119
+ bottom = chartArea . bottom ;
120
+ } else {
121
+ left = chartArea . left ;
122
+ top = start ;
123
+ right = chartArea . right ;
124
+ bottom = end ;
125
+ }
126
+
110
127
ctx . beginPath ( ) ;
111
- ctx . rect ( start , top , end - start , bottom - top ) ;
128
+
129
+ if ( clip ) {
130
+ left = Math . max ( left , clip . left ) ;
131
+ right = Math . min ( right , clip . right ) ;
132
+ top = Math . max ( top , clip . top ) ;
133
+ bottom = Math . min ( bottom , clip . bottom ) ;
134
+ }
135
+
136
+ ctx . rect ( left , top , right - left , bottom - top ) ;
112
137
ctx . clip ( ) ;
113
138
}
114
139
}
0 commit comments