connector.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.Connector = void 0;
  15. const gui_1 = require("@antv/gui");
  16. const d3_shape_1 = require("d3-shape");
  17. const createElement_1 = require("../../utils/createElement");
  18. const coordinate_1 = require("../../utils/coordinate");
  19. const helper_1 = require("../../utils/helper");
  20. const selection_1 = require("../../utils/selection");
  21. const utils_1 = require("../utils");
  22. function inferSymbol(x, y, r) {
  23. return [['M', x, y], ['L', x + 2 * r, y - r], ['L', x + 2 * r, y + r], ['Z']];
  24. }
  25. /**
  26. * @todo support polar later.
  27. */
  28. function inferConnectorPath(points) {
  29. return (0, d3_shape_1.line)()
  30. .x((d) => d[0])
  31. .y((d) => d[1])(points);
  32. }
  33. const ConnectorPath = (0, createElement_1.createElement)((g) => {
  34. // Do not copy className to path.
  35. const _a = g.attributes, { points, class: className, endMarker = true, direction } = _a, rest = __rest(_a, ["points", "class", "endMarker", "direction"]);
  36. const markerStyle = (0, helper_1.subObject)(rest, 'endMarker');
  37. const path = inferConnectorPath(points);
  38. (0, selection_1.select)(g)
  39. .maybeAppend('connector', 'path')
  40. .style('path', path)
  41. .style('markerEnd', endMarker
  42. ? new gui_1.Marker({
  43. className: 'marker',
  44. style: Object.assign(Object.assign({}, markerStyle), { symbol: inferSymbol }),
  45. })
  46. : null)
  47. .call(utils_1.applyStyle, rest);
  48. });
  49. function getPoints(coordinate, points, offset, length1 = 0) {
  50. const [[x0, y0], [x1, y1]] = points;
  51. if ((0, coordinate_1.isTranspose)(coordinate)) {
  52. const OFFSET = offset;
  53. const X0 = x0 + OFFSET;
  54. const X1 = x1 + OFFSET;
  55. const X = X0 + length1;
  56. return [
  57. [X0, y0],
  58. [X, y0],
  59. [X, y1],
  60. [X1, y1],
  61. ];
  62. }
  63. const OFFSET = -offset;
  64. const Y0 = y0 + OFFSET;
  65. const Y1 = y1 + OFFSET;
  66. const Y = Y0 + -length1;
  67. return [
  68. [x0, Y0],
  69. [x0, Y],
  70. [x1, Y],
  71. [x1, Y1],
  72. ];
  73. }
  74. const Connector = (options) => {
  75. const { offset = 0, connectLength1: length1 } = options, style = __rest(options, ["offset", "connectLength1"]);
  76. return (points, value, coordinate, theme) => {
  77. const { mark, shape, defaultShape } = value;
  78. const _a = (0, utils_1.getShapeTheme)(theme, mark, shape, defaultShape), { defaultColor, connectLength1 = length1 } = _a, shapeTheme = __rest(_a, ["defaultColor", "connectLength1"]);
  79. const { color, transform } = value;
  80. const P = getPoints(coordinate, points, offset, connectLength1);
  81. return (0, selection_1.select)(new ConnectorPath())
  82. .call(utils_1.applyStyle, shapeTheme)
  83. .style('points', P)
  84. .style('stroke', color || defaultColor)
  85. .style('transform', transform)
  86. .call(utils_1.applyStyle, style)
  87. .node();
  88. };
  89. };
  90. exports.Connector = Connector;
  91. exports.Connector.props = {
  92. defaultMarker: 'line',
  93. defaultEnterAnimation: 'fadeIn',
  94. defaultUpdateAnimation: 'morphing',
  95. defaultExitAnimation: 'fadeOut',
  96. };
  97. //# sourceMappingURL=connector.js.map