Sector.js
10.7 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Sector = void 0;
var _react = _interopRequireDefault(require("react"));
var _clsx = _interopRequireDefault(require("clsx"));
var _ReactUtils = require("../util/ReactUtils");
var _PolarUtils = require("../util/PolarUtils");
var _DataUtils = require("../util/DataUtils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /**
* @fileOverview Sector
*/
var getDeltaAngle = function getDeltaAngle(startAngle, endAngle) {
var sign = (0, _DataUtils.mathSign)(endAngle - startAngle);
var deltaAngle = Math.min(Math.abs(endAngle - startAngle), 359.999);
return sign * deltaAngle;
};
var getTangentCircle = function getTangentCircle(_ref) {
var cx = _ref.cx,
cy = _ref.cy,
radius = _ref.radius,
angle = _ref.angle,
sign = _ref.sign,
isExternal = _ref.isExternal,
cornerRadius = _ref.cornerRadius,
cornerIsExternal = _ref.cornerIsExternal;
var centerRadius = cornerRadius * (isExternal ? 1 : -1) + radius;
var theta = Math.asin(cornerRadius / centerRadius) / _PolarUtils.RADIAN;
var centerAngle = cornerIsExternal ? angle : angle + sign * theta;
var center = (0, _PolarUtils.polarToCartesian)(cx, cy, centerRadius, centerAngle);
// The coordinate of point which is tangent to the circle
var circleTangency = (0, _PolarUtils.polarToCartesian)(cx, cy, radius, centerAngle);
// The coordinate of point which is tangent to the radius line
var lineTangencyAngle = cornerIsExternal ? angle - sign * theta : angle;
var lineTangency = (0, _PolarUtils.polarToCartesian)(cx, cy, centerRadius * Math.cos(theta * _PolarUtils.RADIAN), lineTangencyAngle);
return {
center: center,
circleTangency: circleTangency,
lineTangency: lineTangency,
theta: theta
};
};
var getSectorPath = function getSectorPath(_ref2) {
var cx = _ref2.cx,
cy = _ref2.cy,
innerRadius = _ref2.innerRadius,
outerRadius = _ref2.outerRadius,
startAngle = _ref2.startAngle,
endAngle = _ref2.endAngle;
var angle = getDeltaAngle(startAngle, endAngle);
// When the angle of sector equals to 360, star point and end point coincide
var tempEndAngle = startAngle + angle;
var outerStartPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, outerRadius, startAngle);
var outerEndPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, outerRadius, tempEndAngle);
var path = "M ".concat(outerStartPoint.x, ",").concat(outerStartPoint.y, "\n A ").concat(outerRadius, ",").concat(outerRadius, ",0,\n ").concat(+(Math.abs(angle) > 180), ",").concat(+(startAngle > tempEndAngle), ",\n ").concat(outerEndPoint.x, ",").concat(outerEndPoint.y, "\n ");
if (innerRadius > 0) {
var innerStartPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, innerRadius, startAngle);
var innerEndPoint = (0, _PolarUtils.polarToCartesian)(cx, cy, innerRadius, tempEndAngle);
path += "L ".concat(innerEndPoint.x, ",").concat(innerEndPoint.y, "\n A ").concat(innerRadius, ",").concat(innerRadius, ",0,\n ").concat(+(Math.abs(angle) > 180), ",").concat(+(startAngle <= tempEndAngle), ",\n ").concat(innerStartPoint.x, ",").concat(innerStartPoint.y, " Z");
} else {
path += "L ".concat(cx, ",").concat(cy, " Z");
}
return path;
};
var getSectorWithCorner = function getSectorWithCorner(_ref3) {
var cx = _ref3.cx,
cy = _ref3.cy,
innerRadius = _ref3.innerRadius,
outerRadius = _ref3.outerRadius,
cornerRadius = _ref3.cornerRadius,
forceCornerRadius = _ref3.forceCornerRadius,
cornerIsExternal = _ref3.cornerIsExternal,
startAngle = _ref3.startAngle,
endAngle = _ref3.endAngle;
var sign = (0, _DataUtils.mathSign)(endAngle - startAngle);
var _getTangentCircle = getTangentCircle({
cx: cx,
cy: cy,
radius: outerRadius,
angle: startAngle,
sign: sign,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
soct = _getTangentCircle.circleTangency,
solt = _getTangentCircle.lineTangency,
sot = _getTangentCircle.theta;
var _getTangentCircle2 = getTangentCircle({
cx: cx,
cy: cy,
radius: outerRadius,
angle: endAngle,
sign: -sign,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
eoct = _getTangentCircle2.circleTangency,
eolt = _getTangentCircle2.lineTangency,
eot = _getTangentCircle2.theta;
var outerArcAngle = cornerIsExternal ? Math.abs(startAngle - endAngle) : Math.abs(startAngle - endAngle) - sot - eot;
if (outerArcAngle < 0) {
if (forceCornerRadius) {
return "M ".concat(solt.x, ",").concat(solt.y, "\n a").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,1,").concat(cornerRadius * 2, ",0\n a").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,1,").concat(-cornerRadius * 2, ",0\n ");
}
return getSectorPath({
cx: cx,
cy: cy,
innerRadius: innerRadius,
outerRadius: outerRadius,
startAngle: startAngle,
endAngle: endAngle
});
}
var path = "M ".concat(solt.x, ",").concat(solt.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(soct.x, ",").concat(soct.y, "\n A").concat(outerRadius, ",").concat(outerRadius, ",0,").concat(+(outerArcAngle > 180), ",").concat(+(sign < 0), ",").concat(eoct.x, ",").concat(eoct.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(eolt.x, ",").concat(eolt.y, "\n ");
if (innerRadius > 0) {
var _getTangentCircle3 = getTangentCircle({
cx: cx,
cy: cy,
radius: innerRadius,
angle: startAngle,
sign: sign,
isExternal: true,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
sict = _getTangentCircle3.circleTangency,
silt = _getTangentCircle3.lineTangency,
sit = _getTangentCircle3.theta;
var _getTangentCircle4 = getTangentCircle({
cx: cx,
cy: cy,
radius: innerRadius,
angle: endAngle,
sign: -sign,
isExternal: true,
cornerRadius: cornerRadius,
cornerIsExternal: cornerIsExternal
}),
eict = _getTangentCircle4.circleTangency,
eilt = _getTangentCircle4.lineTangency,
eit = _getTangentCircle4.theta;
var innerArcAngle = cornerIsExternal ? Math.abs(startAngle - endAngle) : Math.abs(startAngle - endAngle) - sit - eit;
if (innerArcAngle < 0 && cornerRadius === 0) {
return "".concat(path, "L").concat(cx, ",").concat(cy, "Z");
}
path += "L".concat(eilt.x, ",").concat(eilt.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(eict.x, ",").concat(eict.y, "\n A").concat(innerRadius, ",").concat(innerRadius, ",0,").concat(+(innerArcAngle > 180), ",").concat(+(sign > 0), ",").concat(sict.x, ",").concat(sict.y, "\n A").concat(cornerRadius, ",").concat(cornerRadius, ",0,0,").concat(+(sign < 0), ",").concat(silt.x, ",").concat(silt.y, "Z");
} else {
path += "L".concat(cx, ",").concat(cy, "Z");
}
return path;
};
var defaultProps = {
cx: 0,
cy: 0,
innerRadius: 0,
outerRadius: 0,
startAngle: 0,
endAngle: 0,
cornerRadius: 0,
forceCornerRadius: false,
cornerIsExternal: false
};
var Sector = exports.Sector = function Sector(sectorProps) {
var props = _objectSpread(_objectSpread({}, defaultProps), sectorProps);
var cx = props.cx,
cy = props.cy,
innerRadius = props.innerRadius,
outerRadius = props.outerRadius,
cornerRadius = props.cornerRadius,
forceCornerRadius = props.forceCornerRadius,
cornerIsExternal = props.cornerIsExternal,
startAngle = props.startAngle,
endAngle = props.endAngle,
className = props.className;
if (outerRadius < innerRadius || startAngle === endAngle) {
return null;
}
var layerClass = (0, _clsx["default"])('recharts-sector', className);
var deltaRadius = outerRadius - innerRadius;
var cr = (0, _DataUtils.getPercentValue)(cornerRadius, deltaRadius, 0, true);
var path;
if (cr > 0 && Math.abs(startAngle - endAngle) < 360) {
path = getSectorWithCorner({
cx: cx,
cy: cy,
innerRadius: innerRadius,
outerRadius: outerRadius,
cornerRadius: Math.min(cr, deltaRadius / 2),
forceCornerRadius: forceCornerRadius,
cornerIsExternal: cornerIsExternal,
startAngle: startAngle,
endAngle: endAngle
});
} else {
path = getSectorPath({
cx: cx,
cy: cy,
innerRadius: innerRadius,
outerRadius: outerRadius,
startAngle: startAngle,
endAngle: endAngle
});
}
return /*#__PURE__*/_react["default"].createElement("path", _extends({}, (0, _ReactUtils.filterProps)(props, true), {
className: layerClass,
d: path,
role: "img"
}));
};