-
Notifications
You must be signed in to change notification settings - Fork 323
/
ellipse.spec.js
133 lines (115 loc) · 5.26 KB
/
ellipse.spec.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
133
describe('Ellipse annotation', function() {
describe('auto', jasmine.fixtures('ellipse'));
const rotated = window.helpers.rotated;
describe('inRange', function() {
for (const rotation of [0, 45, 90, 135, 180, 225, 270, 315]) {
const annotation = {
type: 'ellipse',
xMin: 3,
yMin: 4,
xMax: 7,
yMax: 5,
borderWidth: 0,
rotation
};
const chart = window.scatterChart(10, 10, {test: annotation});
const element = window.getAnnotationElements(chart)[0];
const center = element.getCenterPoint();
const xRadius = element.width / 2;
const yRadius = element.height / 2;
it(`should return true when point is inside element\n{x: ${center.x}, y: ${center.y}, xRadius: ${xRadius.toFixed(1)}, yRadius: ${yRadius.toFixed(1)}}`, function() {
for (const borderWidth of [0, 10]) {
const halfBorder = borderWidth / 2;
element.options.borderWidth = borderWidth;
for (const angle of [0, 45, 90, 135, 180, 225, 270, 315]) {
const rad = angle / 180 * Math.PI;
const {x, y} = rotated({
x: center.x + Math.cos(rad) * (xRadius + halfBorder),
y: center.y + Math.sin(rad) * (yRadius + halfBorder)
}, center, rotation / 180 * Math.PI);
expect(element.inRange(x, y)).withContext(`rotation: ${rotation}, angle: ${angle}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(true);
}
}
});
it(`should return false when point is outside element\n{x: ${center.x}, y: ${center.y}, xRadius: ${xRadius.toFixed(1)}, yRadius: ${yRadius.toFixed(1)}}`, function() {
for (const borderWidth of [0, 10]) {
const halfBorder = borderWidth / 2;
element.options.borderWidth = borderWidth;
for (const angle of [0, 45, 90, 135, 180, 225, 270, 315]) {
const rad = angle / 180 * Math.PI;
const {x, y} = rotated({
x: center.x + Math.cos(rad) * (xRadius + halfBorder + 1),
y: center.y + Math.sin(rad) * (yRadius + halfBorder + 1)
}, center, rotation / 180 * Math.PI);
expect(element.inRange(x, y)).withContext(`rotation: ${rotation}, angle: ${angle}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(false);
}
}
});
}
it('should refurn false for zero width/height ellipse', function() {
const chart = window.scatterChart(10, 10, [
{type: 'ellipse', xMin: 1, xMax: 1, yMin: 1, yMax: 1},
{type: 'ellipse', xMin: 2, xMax: 3, yMin: 1, yMax: 1},
{type: 'ellipse', xMin: 1, xMax: 1, yMin: 2, yMax: 3}
]);
const elements = window.getAnnotationElements(chart);
for (const element of elements) {
expect(element.inRange(element.clientX, element.clientY)).toEqual(false);
}
});
});
describe('interaction', function() {
for (const rotation of [0, 45, 90, 135, 180, 225, 270, 315]) {
const outer = {
type: 'ellipse',
xMin: 3,
xMax: 7,
yMin: 3,
yMax: 7,
borderWidth: 0,
rotation
};
const inner = {
type: 'ellipse',
xMin: 4.5,
xMax: 6,
yMin: 4.5,
yMax: 6,
borderWidth: 0,
rotation
};
const chart = window.scatterChart(10, 10, {outer, inner});
const elements = window.getAnnotationElements(chart);
const visible = elements.filter(el => !el.skip && el.options.display);
const interactionOpts = {};
const outerEl = elements[0];
const innerEl = elements[1];
it('should return the right amount of annotation elements', function() {
for (const interaction of window.interactionData) {
const mode = interaction.mode;
interactionOpts.mode = mode;
for (const axis of Object.keys(interaction.axes)) {
interactionOpts.axis = axis;
[true, false].forEach(function(intersect) {
interactionOpts.intersect = intersect;
const elementsCounts = interaction.axes[axis].intersect[intersect];
const points = [{x: outerEl.x, y: outerEl.y + outerEl.height / 2, el: outerEl},
{x: innerEl.x, y: innerEl.y + innerEl.height / 2, el: innerEl},
{x: innerEl.x + innerEl.width / 2, y: innerEl.y + innerEl.height / 2, el: innerEl},
{x: innerEl.x2 + 1, y: innerEl.y2 - innerEl.height / 2, el: innerEl},
{x: outerEl.x2 + 1, y: outerEl.y2 - outerEl.height / 2, el: outerEl},
{x: outerEl.x + 1, y: outerEl.y - 1, el: outerEl}];
for (let i = 0; i < points.length; i++) {
const point = points[i];
const elementsCount = elementsCounts[i];
const {x, y} = rotated(point, point.el.getCenterPoint(), rotation / 180 * Math.PI);
const els = window.getAnnotationInteractedElements(visible, {x, y}, interactionOpts, true);
expect(els.length).withContext(`with rotation ${rotation}, interaction mode ${mode}, axis ${axis}, intersect ${intersect}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}`).toEqual(elementsCount);
}
});
}
}
});
}
});
});