ribbon.js 2.8 KB

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