polygon.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.Polygon = void 0;
  15. const g_1 = require("@antv/g");
  16. const d3_path_1 = require("d3-path");
  17. const coordinate_1 = require("../../utils/coordinate");
  18. const utils_1 = require("../utils");
  19. const selection_1 = require("../../utils/selection");
  20. const vector_1 = require("../../utils/vector");
  21. function getPolygonPath(points, coordinate) {
  22. const path = (0, d3_path_1.path)();
  23. // In polar, draw arc.
  24. if ((0, coordinate_1.isPolar)(coordinate)) {
  25. const center = coordinate.getCenter();
  26. const closedPoints = [...points, points[0]];
  27. // Calculate dist array for cache.
  28. const dists = closedPoints.map((p) => (0, vector_1.dist)(p, center));
  29. closedPoints.forEach((curr, idx) => {
  30. if (idx === 0) {
  31. path.moveTo(curr[0], curr[1]);
  32. return;
  33. }
  34. const currDist = dists[idx];
  35. const prev = points[idx - 1];
  36. const prevDist = dists[idx - 1];
  37. // When radius is equal, draw 2 point with arc.
  38. // todo: choose a minimum value.
  39. if (prevDist !== undefined && Math.abs(currDist - prevDist) < 1e-10) {
  40. (0, utils_1.appendArc)(path, prev, curr, center, currDist);
  41. }
  42. else {
  43. path.lineTo(curr[0], curr[1]);
  44. }
  45. });
  46. path.closePath();
  47. return path;
  48. }
  49. // In rect, draw polygon.
  50. return (0, utils_1.appendPolygon)(path, points);
  51. }
  52. const Polygon = (options) => {
  53. const style = __rest(options, []);
  54. return (points, value, coordinate, theme) => {
  55. const { mark, shape, defaultShape, color, transform } = value;
  56. const _a = (0, utils_1.getShapeTheme)(theme, mark, shape, defaultShape), { defaultColor } = _a, shapeTheme = __rest(_a, ["defaultColor"]);
  57. const path = getPolygonPath(points, coordinate);
  58. return (0, selection_1.select)(new g_1.Path())
  59. .call(utils_1.applyStyle, shapeTheme)
  60. .style('d', path.toString())
  61. .style('stroke', color || defaultColor)
  62. .style('fill', color || defaultColor)
  63. .style('transform', transform)
  64. .call(utils_1.applyStyle, style)
  65. .node();
  66. };
  67. };
  68. exports.Polygon = Polygon;
  69. exports.Polygon.props = {
  70. defaultMarker: 'square',
  71. defaultEnterAnimation: 'fadeIn',
  72. defaultUpdateAnimation: 'morphing',
  73. defaultExitAnimation: 'fadeOut',
  74. };
  75. //# sourceMappingURL=polygon.js.map