polygon.js 2.8 KB

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