-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
element.point.ts
107 lines (87 loc) · 2.75 KB
/
element.point.ts
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
import Element from '../core/core.element.js';
import {drawPoint, _isPointInArea} from '../helpers/helpers.canvas.js';
import type {
CartesianParsedData,
ChartArea,
Point,
PointHoverOptions,
PointOptions,
} from '../types/index.js';
function inRange(el: PointElement, pos: number, axis: 'x' | 'y', useFinalPosition?: boolean) {
const options = el.options;
const {[axis]: value} = el.getProps([axis], useFinalPosition);
return (Math.abs(pos - value) < options.radius + options.hitRadius);
}
export type PointProps = Point
export default class PointElement extends Element<PointProps, PointOptions & PointHoverOptions> {
static id = 'point';
parsed: CartesianParsedData;
skip?: boolean;
stop?: boolean;
/**
* @type {any}
*/
static defaults = {
borderWidth: 1,
hitRadius: 1,
hoverBorderWidth: 1,
hoverRadius: 4,
pointStyle: 'circle',
radius: 3,
rotation: 0
};
/**
* @type {any}
*/
static defaultRoutes = {
backgroundColor: 'backgroundColor',
borderColor: 'borderColor'
};
constructor(cfg) {
super();
this.options = undefined;
this.parsed = undefined;
this.skip = undefined;
this.stop = undefined;
if (cfg) {
Object.assign(this, cfg);
}
}
inRange(mouseX: number, mouseY: number, useFinalPosition?: boolean) {
const options = this.options;
const {x, y} = this.getProps(['x', 'y'], useFinalPosition);
return ((Math.pow(mouseX - x, 2) + Math.pow(mouseY - y, 2)) < Math.pow(options.hitRadius + options.radius, 2));
}
inXRange(mouseX: number, useFinalPosition?: boolean) {
return inRange(this, mouseX, 'x', useFinalPosition);
}
inYRange(mouseY: number, useFinalPosition?: boolean) {
return inRange(this, mouseY, 'y', useFinalPosition);
}
getCenterPoint(useFinalPosition?: boolean) {
const {x, y} = this.getProps(['x', 'y'], useFinalPosition);
return {x, y};
}
size(options?: Partial<PointOptions & PointHoverOptions>) {
options = options || this.options || {};
let radius = options.radius || 0;
radius = Math.max(radius, radius && options.hoverRadius || 0);
const borderWidth = radius && options.borderWidth || 0;
return (radius + borderWidth) * 2;
}
draw(ctx: CanvasRenderingContext2D, area: ChartArea) {
const options = this.options;
if (this.skip || options.radius < 0.1 || !_isPointInArea(this, area, this.size(options) / 2)) {
return;
}
ctx.strokeStyle = options.borderColor;
ctx.lineWidth = options.borderWidth;
ctx.fillStyle = options.backgroundColor;
drawPoint(ctx, options, this.x, this.y);
}
getRange() {
const options = this.options || {};
// @ts-expect-error Fallbacks should never be hit in practice
return options.radius + options.hitRadius;
}
}