/
box.js
132 lines (119 loc) · 3.65 KB
/
box.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {Element} from 'chart.js';
import {toPadding} from 'chart.js/helpers';
import {drawBox, drawLabel, getRelativePosition, measureLabelSize, getRectCenterPoint, getChartRect, toPosition, inBoxRange} from '../helpers';
export default class BoxAnnotation extends Element {
inRange(mouseX, mouseY, useFinalPosition) {
return inBoxRange(mouseX, mouseY, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition));
}
getCenterPoint(useFinalPosition) {
return getRectCenterPoint(this.getProps(['x', 'y', 'width', 'height'], useFinalPosition));
}
draw(ctx) {
ctx.save();
drawBox(ctx, this, this.options);
ctx.restore();
}
drawLabel(ctx) {
const {x, y, width, height, options} = this;
const {label, borderWidth} = options;
const halfBorder = borderWidth / 2;
const position = toPosition(label.position);
const padding = toPadding(label.padding);
const labelSize = measureLabelSize(ctx, label);
const labelRect = {
x: calculateX(this, labelSize, position, padding),
y: calculateY(this, labelSize, position, padding),
width: labelSize.width,
height: labelSize.height
};
ctx.save();
ctx.beginPath();
ctx.rect(x + halfBorder + padding.left, y + halfBorder + padding.top,
width - borderWidth - padding.width, height - borderWidth - padding.height);
ctx.clip();
drawLabel(ctx, labelRect, label);
ctx.restore();
}
resolveElementProperties(chart, options) {
return getChartRect(chart, options);
}
}
BoxAnnotation.id = 'boxAnnotation';
BoxAnnotation.defaults = {
adjustScaleRange: true,
backgroundShadowColor: 'transparent',
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0,
borderJoinStyle: 'miter',
borderRadius: 0,
borderShadowColor: 'transparent',
borderWidth: 1,
cornerRadius: undefined, // TODO: v2 remove support for cornerRadius
display: true,
label: {
borderWidth: undefined,
color: 'black',
content: null,
drawTime: undefined,
enabled: false,
font: {
family: undefined,
lineHeight: undefined,
size: undefined,
style: undefined,
weight: 'bold'
},
height: undefined,
padding: 6,
position: 'center',
textAlign: 'start',
xAdjust: 0,
yAdjust: 0,
width: undefined
},
shadowBlur: 0,
shadowOffsetX: 0,
shadowOffsetY: 0,
xMax: undefined,
xMin: undefined,
xScaleID: 'x',
yMax: undefined,
yMin: undefined,
yScaleID: 'y'
};
BoxAnnotation.defaultRoutes = {
borderColor: 'color',
backgroundColor: 'color'
};
BoxAnnotation.descriptors = {
label: {
_fallback: true
}
};
function calculateX(box, labelSize, position, padding) {
const {x: start, x2: end, width: size, options} = box;
const {xAdjust: adjust, borderWidth} = options.label;
return calculatePosition({start, end, size}, {
position: position.x,
padding: {start: padding.left, end: padding.right},
adjust, borderWidth,
size: labelSize.width
});
}
function calculateY(box, labelSize, position, padding) {
const {y: start, y2: end, height: size, options} = box;
const {yAdjust: adjust, borderWidth} = options.label;
return calculatePosition({start, end, size}, {
position: position.y,
padding: {start: padding.top, end: padding.bottom},
adjust, borderWidth,
size: labelSize.height
});
}
function calculatePosition(boxOpts, labelOpts) {
const {start, end} = boxOpts;
const {position, padding: {start: padStart, end: padEnd}, adjust, borderWidth} = labelOpts;
const availableSize = end - borderWidth - start - padStart - padEnd - labelOpts.size;
return start + borderWidth / 2 + adjust + padStart + getRelativePosition(availableSize, position);
}