-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
index.js
97 lines (80 loc) · 2.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Plugin based on discussion from the following Chart.js issues:
* @see https://github.com/chartjs/Chart.js/issues/2380#issuecomment-279961569
* @see https://github.com/chartjs/Chart.js/issues/2440#issuecomment-256461897
*/
import LineElement from '../../elements/element.line.js';
import {_drawfill} from './filler.drawing.js';
import {_shouldApplyFill} from './filler.helper.js';
import {_decodeFill, _resolveTarget} from './filler.options.js';
export default {
id: 'filler',
afterDatasetsUpdate(chart, _args, options) {
const count = (chart.data.datasets || []).length;
const sources = [];
let meta, i, line, source;
for (i = 0; i < count; ++i) {
meta = chart.getDatasetMeta(i);
line = meta.dataset;
source = null;
if (line && line.options && line instanceof LineElement) {
source = {
visible: chart.isDatasetVisible(i),
index: i,
fill: _decodeFill(line, i, count),
chart,
axis: meta.controller.options.indexAxis,
scale: meta.vScale,
line,
};
}
meta.$filler = source;
sources.push(source);
}
for (i = 0; i < count; ++i) {
source = sources[i];
if (!source || source.fill === false) {
continue;
}
source.fill = _resolveTarget(sources, i, options.propagate);
}
},
beforeDraw(chart, _args, options) {
const draw = options.drawTime === 'beforeDraw';
const metasets = chart.getSortedVisibleDatasetMetas();
const area = chart.chartArea;
for (let i = metasets.length - 1; i >= 0; --i) {
const source = metasets[i].$filler;
if (!source) {
continue;
}
source.line.updateControlPoints(area, source.axis);
if (draw && source.fill) {
_drawfill(chart.ctx, source, area);
}
}
},
beforeDatasetsDraw(chart, _args, options) {
if (options.drawTime !== 'beforeDatasetsDraw') {
return;
}
const metasets = chart.getSortedVisibleDatasetMetas();
for (let i = metasets.length - 1; i >= 0; --i) {
const source = metasets[i].$filler;
if (_shouldApplyFill(source)) {
_drawfill(chart.ctx, source, chart.chartArea);
}
}
},
beforeDatasetDraw(chart, args, options) {
const source = args.meta.$filler;
if (!_shouldApplyFill(source) || options.drawTime !== 'beforeDatasetDraw') {
return;
}
_drawfill(chart.ctx, source, chart.chartArea);
},
defaults: {
propagate: true,
drawTime: 'beforeDatasetDraw'
}
};