ribbon.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Ribbon = void 0;
  15. const g_1 = require("@antv/g");
  16. const d3_path_1 = require("d3-path");
  17. const utils_1 = require("../utils");
  18. const selection_1 = require("../../utils/selection");
  19. const coordinate_1 = require("../../utils/coordinate");
  20. const vector_1 = require("../../utils/vector");
  21. function getRibbonPath(points, coordinate) {
  22. const [p0, p1, p2, p3] = points;
  23. const path = (0, d3_path_1.path)();
  24. // In polar, draw shape only for Chord.
  25. if ((0, coordinate_1.isPolar)(coordinate)) {
  26. const center = coordinate.getCenter();
  27. const radius = (0, vector_1.dist)(center, p0);
  28. path.moveTo(p0[0], p0[1]);
  29. // p0 -> p2
  30. path.quadraticCurveTo(center[0], center[1], p2[0], p2[1]);
  31. // p2 -> p3
  32. (0, utils_1.appendArc)(path, p2, p3, center, radius);
  33. // p3 -> p1
  34. path.quadraticCurveTo(center[0], center[1], p1[0], p1[1]);
  35. // p1 -> p0
  36. (0, utils_1.appendArc)(path, p1, p0, center, radius);
  37. path.closePath();
  38. return path;
  39. }
  40. // In Rect, draw shape for Sankey.
  41. path.moveTo(p0[0], p0[1]);
  42. path.bezierCurveTo(p0[0] / 2 + p2[0] / 2, p0[1], p0[0] / 2 + p2[0] / 2, p2[1], p2[0], p2[1]);
  43. path.lineTo(p3[0], p3[1]);
  44. path.bezierCurveTo(p3[0] / 2 + p1[0] / 2, p3[1], p3[0] / 2 + p1[0] / 2, p1[1], p1[0], p1[1]);
  45. path.lineTo(p0[0], p0[1]);
  46. path.closePath();
  47. return path;
  48. }
  49. /**
  50. * Connect points for 4 points:
  51. * - In rect, draw ribbon used in Sankey.
  52. * - In polar, draw arc used in Chord.
  53. */
  54. const Ribbon = (options) => {
  55. const style = __rest(options, []);
  56. return (points, value, coordinate, theme) => {
  57. const { mark, shape, defaultShape, color, transform } = value;
  58. const _a = (0, utils_1.getShapeTheme)(theme, mark, shape, defaultShape), { defaultColor } = _a, shapeTheme = __rest(_a, ["defaultColor"]);
  59. const path = getRibbonPath(points, coordinate);
  60. return (0, selection_1.select)(new g_1.Path())
  61. .call(utils_1.applyStyle, shapeTheme)
  62. .style('d', path.toString())
  63. .style('fill', color || defaultColor)
  64. .style('stroke', color || defaultColor)
  65. .style('transform', transform)
  66. .call(utils_1.applyStyle, style)
  67. .node();
  68. };
  69. };
  70. exports.Ribbon = Ribbon;
  71. exports.Ribbon.props = {
  72. defaultMarker: 'square',
  73. defaultEnterAnimation: 'fadeIn',
  74. defaultUpdateAnimation: 'morphing',
  75. defaultExitAnimation: 'fadeOut',
  76. };
  77. //# sourceMappingURL=ribbon.js.map