/
filler.target.js
95 lines (75 loc) · 2.22 KB
/
filler.target.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
import {isFinite} from '../../helpers';
import {_createBoundaryLine} from './filler.helper';
import {_getTargetPixel, _getTargetValue} from './filler.options';
import {_buildStackLine} from './filler.target.stack';
import {simpleArc} from './simpleArc';
/**
* @typedef { import('../../core/core.controller').default } Chart
* @typedef { import('../../core/core.scale').default } Scale
* @typedef { import('../../elements/element.point').default } PointElement
*/
export function _getTarget(source) {
const {chart, fill, line} = source;
if (isFinite(fill)) {
return getLineByIndex(chart, fill);
}
if (fill === 'stack') {
return _buildStackLine(source);
}
if (fill === 'shape') {
return true;
}
const boundary = computeBoundary(source);
if (boundary instanceof simpleArc) {
return boundary;
}
return _createBoundaryLine(boundary, line);
}
/**
* @param {Chart} chart
* @param {number} index
*/
function getLineByIndex(chart, index) {
const meta = chart.getDatasetMeta(index);
const visible = meta && chart.isDatasetVisible(index);
return visible ? meta.dataset : null;
}
function computeBoundary(source) {
const scale = source.scale || {};
if (scale.getPointPositionForValue) {
return computeCircularBoundary(source);
}
return computeLinearBoundary(source);
}
function computeLinearBoundary(source) {
const {scale = {}, fill} = source;
const pixel = _getTargetPixel(fill, scale);
if (isFinite(pixel)) {
const horizontal = scale.isHorizontal();
return {
x: horizontal ? pixel : null,
y: horizontal ? null : pixel
};
}
return null;
}
function computeCircularBoundary(source) {
const {scale, fill} = source;
const options = scale.options;
const length = scale.getLabels().length;
const start = options.reverse ? scale.max : scale.min;
const value = _getTargetValue(fill, scale, start);
const target = [];
if (options.grid.circular) {
const center = scale.getPointPositionForValue(0, start);
return new simpleArc({
x: center.x,
y: center.y,
radius: scale.getDistanceFromCenterForValue(value)
});
}
for (let i = 0; i < length; ++i) {
target.push(scale.getPointPositionForValue(i, value));
}
return target;
}