UNPKG

recharts

Version:
85 lines (81 loc) • 3.93 kB
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } 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); } var TAU = 2 * Math.PI; var EPSILON = 1e-6; /** * Rounds to 3 decimal places, the same as d3 symbols do. * @param value the number to round */ function round(value) { return Math.round(value * 1000) / 1000; } /** * A drawing context for d3 symbols that writes SVG path data, moved by an offset. * d3 draws symbols around the point (0, 0). Patterns need them inside their tile. */ class OffsetPathContext { constructor(dx, dy) { _defineProperty(this, "path", ''); this.dx = dx; this.dy = dy; } moveTo(x, y) { this.path += "M".concat(round(x + this.dx), ",").concat(round(y + this.dy)); } lineTo(x, y) { this.path += "L".concat(round(x + this.dx), ",").concat(round(y + this.dy)); } closePath() { this.path += 'Z'; } quadraticCurveTo(cpx, cpy, x, y) { this.path += "Q".concat(round(cpx + this.dx), ",").concat(round(cpy + this.dy), ",").concat(round(x + this.dx), ",").concat(round(y + this.dy)); } bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) { this.path += "C".concat(round(cp1x + this.dx), ",").concat(round(cp1y + this.dy), ",").concat(round(cp2x + this.dx), ",").concat(round(cp2y + this.dy), ",").concat(round(x + this.dx), ",").concat(round(y + this.dy)); } rect(x, y, w, h) { this.path += "M".concat(round(x + this.dx), ",").concat(round(y + this.dy), "h").concat(round(w), "v").concat(round(h), "h").concat(round(-w), "Z"); } arc(x, y, r, startAngle, endAngle) { var anticlockwise = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false; var sweep = anticlockwise ? 0 : 1; var x0 = x + this.dx + r * Math.cos(startAngle); var y0 = y + this.dy + r * Math.sin(startAngle); if (Math.abs(endAngle - startAngle) >= TAU - EPSILON) { // SVG can't draw a full circle in one arc: draw two halves var _x = x + this.dx - r * Math.cos(startAngle); var _y = y + this.dy - r * Math.sin(startAngle); this.path += "A".concat(round(r), ",").concat(round(r), ",0,1,").concat(sweep, ",").concat(round(_x), ",").concat(round(_y), "A").concat(round(r), ",").concat(round(r), ",0,1,").concat(sweep, ",").concat(round(x0), ",").concat(round(y0)); return; } var delta = anticlockwise ? startAngle - endAngle : endAngle - startAngle; var largeArc = (delta % TAU + TAU) % TAU > Math.PI ? 1 : 0; var x1 = x + this.dx + r * Math.cos(endAngle); var y1 = y + this.dy + r * Math.sin(endAngle); this.path += "A".concat(round(r), ",").concat(round(r), ",0,").concat(largeArc, ",").concat(sweep, ",").concat(round(x1), ",").concat(round(y1)); } // No d3 symbol draws with these, and they don't map to SVG path commands directly. arcTo() { throw new Error('arcTo is not supported in pattern symbols'); } ellipse() { throw new Error('ellipse is not supported in pattern symbols'); } toString() { return this.path; } } /** * Returns SVG path data of a d3 symbol, centered at the given point. * @param symbolType d3 symbol type, such as `symbolStar` * @param size area of the symbol, in square pixels * @param cx x coordinate of the center * @param cy y coordinate of the center */ export function getSymbolPath(symbolType, size, cx, cy) { var context = new OffsetPathContext(cx, cy); symbolType.draw(context, size); return context.toString(); }