-
Notifications
You must be signed in to change notification settings - Fork 319
/
point.js
84 lines (74 loc) · 2.39 KB
/
point.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
import {Element} from 'chart.js';
import {drawPoint} from 'chart.js/helpers';
import {inPointRange, getElementCenterPoint, resolvePointPosition, setBorderStyle, setShadowStyle, isImageOrCanvas} from '../helpers';
export default class PointAnnotation extends Element {
inRange(mouseX, mouseY, useFinalPosition) {
const {width} = this.getProps(['width'], useFinalPosition);
return inPointRange({x: mouseX, y: mouseY}, this.getCenterPoint(useFinalPosition), width / 2, this.options.borderWidth);
}
inXRange(mouseX, mouseY, useFinalPosition) {
const {x, width} = this.getProps(['x', 'width'], useFinalPosition);
const hBorderWidth = this.options.borderWidth / 2;
return mouseX >= x - width / 2 - hBorderWidth && mouseX <= x + width / 2 + hBorderWidth;
}
inYRange(mouseX, mouseY, useFinalPosition) {
const {y, height} = this.getProps(['y', 'height'], useFinalPosition);
const hBorderWidth = this.options.borderWidth / 2;
return mouseY >= y - height / 2 - hBorderWidth && mouseY <= y + height / 2 + hBorderWidth;
}
getCenterPoint(useFinalPosition) {
return getElementCenterPoint(this, useFinalPosition);
}
draw(ctx) {
const options = this.options;
const borderWidth = options.borderWidth;
if (options.radius < 0.1) {
return;
}
ctx.save();
ctx.fillStyle = options.backgroundColor;
setShadowStyle(ctx, options);
const stroke = setBorderStyle(ctx, options);
options.borderWidth = 0;
drawPoint(ctx, options, this.x, this.y);
if (stroke && !isImageOrCanvas(options.pointStyle)) {
ctx.shadowColor = options.borderShadowColor;
ctx.stroke();
}
ctx.restore();
options.borderWidth = borderWidth;
}
resolveElementProperties(chart, options) {
return resolvePointPosition(chart, options);
}
}
PointAnnotation.id = 'pointAnnotation';
PointAnnotation.defaults = {
adjustScaleRange: true,
backgroundShadowColor: 'transparent',
borderDash: [],
borderDashOffset: 0,
borderShadowColor: 'transparent',
borderWidth: 1,
display: true,
pointStyle: 'circle',
radius: 10,
rotation: 0,
shadowBlur: 0,
shadowOffsetX: 0,
shadowOffsetY: 0,
xAdjust: 0,
xMax: undefined,
xMin: undefined,
xScaleID: 'x',
xValue: undefined,
yAdjust: 0,
yMax: undefined,
yMin: undefined,
yScaleID: 'y',
yValue: undefined
};
PointAnnotation.defaultRoutes = {
borderColor: 'color',
backgroundColor: 'color'
};