Skip to content

Commit f7d2bbd

Browse files
authored
Utilize _angleBetween in Arc (#7080)
* Utilize _angleBetween in Arc * More ES6 chrore on Arc
1 parent 8828438 commit f7d2bbd

File tree

1 file changed

+25
-40
lines changed

1 file changed

+25
-40
lines changed

src/elements/element.arc.js

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import defaults from '../core/core.defaults';
44
import Element from '../core/core.element';
55
import {extend} from '../helpers/helpers.core';
6-
import {getAngleFromPoint} from '../helpers/helpers.math';
6+
import {_angleBetween, getAngleFromPoint} from '../helpers/helpers.math';
77
const TAU = Math.PI * 2;
88

99
defaults.set('elements', {
@@ -16,12 +16,8 @@ defaults.set('elements', {
1616
});
1717

1818
function clipArc(ctx, arc) {
19-
var startAngle = arc.startAngle;
20-
var endAngle = arc.endAngle;
21-
var pixelMargin = arc.pixelMargin;
22-
var angleMargin = pixelMargin / arc.outerRadius;
23-
var x = arc.x;
24-
var y = arc.y;
19+
const {startAngle, endAngle, pixelMargin, x, y} = arc;
20+
let angleMargin = pixelMargin / arc.outerRadius;
2521

2622
// Draw an inner border by cliping the arc and drawing a double-width border
2723
// Enlarge the clipping arc by 0.33 pixels to eliminate glitches between borders
@@ -38,8 +34,8 @@ function clipArc(ctx, arc) {
3834
}
3935

4036
function drawFullCircleBorders(ctx, vm, arc, inner) {
41-
var endAngle = arc.endAngle;
42-
var i;
37+
const endAngle = arc.endAngle;
38+
let i;
4339

4440
if (inner) {
4541
arc.endAngle = arc.startAngle + TAU;
@@ -66,7 +62,7 @@ function drawFullCircleBorders(ctx, vm, arc, inner) {
6662

6763
function drawBorder(ctx, vm, arc) {
6864
const options = vm.options;
69-
var inner = options.borderAlign === 'inner';
65+
const inner = options.borderAlign === 'inner';
7066

7167
if (inner) {
7268
ctx.lineWidth = options.borderWidth * 2;
@@ -108,47 +104,36 @@ class Arc extends Element {
108104
}
109105
}
110106

107+
/**
108+
* @param {number} chartX
109+
* @param {number} chartY
110+
*/
111111
inRange(chartX, chartY) {
112-
var me = this;
112+
const me = this;
113113

114-
var pointRelativePosition = getAngleFromPoint(me, {x: chartX, y: chartY});
115-
var angle = pointRelativePosition.angle;
116-
var distance = pointRelativePosition.distance;
117-
118-
// Sanitise angle range
119-
var startAngle = me.startAngle;
120-
var endAngle = me.endAngle;
121-
while (endAngle < startAngle) {
122-
endAngle += TAU;
123-
}
124-
while (angle > endAngle) {
125-
angle -= TAU;
126-
}
127-
while (angle < startAngle) {
128-
angle += TAU;
129-
}
114+
const {angle, distance} = getAngleFromPoint(me, {x: chartX, y: chartY});
130115

131116
// Check if within the range of the open/close angle
132-
var betweenAngles = (angle >= startAngle && angle <= endAngle);
133-
var withinRadius = (distance >= me.innerRadius && distance <= me.outerRadius);
117+
const betweenAngles = _angleBetween(angle, me.startAngle, me.endAngle);
118+
const withinRadius = (distance >= me.innerRadius && distance <= me.outerRadius);
134119

135120
return (betweenAngles && withinRadius);
136121
}
137122

138123
getCenterPoint() {
139-
var me = this;
140-
var halfAngle = (me.startAngle + me.endAngle) / 2;
141-
var halfRadius = (me.innerRadius + me.outerRadius) / 2;
124+
const me = this;
125+
const halfAngle = (me.startAngle + me.endAngle) / 2;
126+
const halfRadius = (me.innerRadius + me.outerRadius) / 2;
142127
return {
143128
x: me.x + Math.cos(halfAngle) * halfRadius,
144129
y: me.y + Math.sin(halfAngle) * halfRadius
145130
};
146131
}
147132

148133
tooltipPosition() {
149-
var me = this;
150-
var centreAngle = me.startAngle + ((me.endAngle - me.startAngle) / 2);
151-
var rangeFromCentre = (me.outerRadius - me.innerRadius) / 2 + me.innerRadius;
134+
const me = this;
135+
const centreAngle = me.startAngle + ((me.endAngle - me.startAngle) / 2);
136+
const rangeFromCentre = (me.outerRadius - me.innerRadius) / 2 + me.innerRadius;
152137

153138
return {
154139
x: me.x + (Math.cos(centreAngle) * rangeFromCentre),
@@ -157,10 +142,10 @@ class Arc extends Element {
157142
}
158143

159144
draw(ctx) {
160-
var me = this;
161-
var options = me.options;
162-
var pixelMargin = (options.borderAlign === 'inner') ? 0.33 : 0;
163-
var arc = {
145+
const me = this;
146+
const options = me.options;
147+
const pixelMargin = (options.borderAlign === 'inner') ? 0.33 : 0;
148+
const arc = {
164149
x: me.x,
165150
y: me.y,
166151
innerRadius: me.innerRadius,
@@ -170,7 +155,7 @@ class Arc extends Element {
170155
endAngle: me.endAngle,
171156
fullCircles: Math.floor(me.circumference / TAU)
172157
};
173-
var i;
158+
let i;
174159

175160
ctx.save();
176161

0 commit comments

Comments
 (0)