line.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Line = void 0;
  15. const g_1 = require("@antv/g");
  16. const d3_shape_1 = require("d3-shape");
  17. const coordinate_1 = require("../../utils/coordinate");
  18. const selection_1 = require("../../utils/selection");
  19. const vector_1 = require("../../utils/vector");
  20. const helper_1 = require("../../utils/helper");
  21. const utils_1 = require("../utils");
  22. function getArrowMarker(arrowSize, arrowStyle) {
  23. const arrowMarker = new g_1.Path({
  24. style: Object.assign({ path: `M ${arrowSize},${arrowSize} L -${arrowSize},0 L ${arrowSize},-${arrowSize} L 0,0 Z`, anchor: '0.5 0.5', transformOrigin: 'center' }, arrowStyle),
  25. });
  26. return arrowMarker;
  27. }
  28. function getPath(points, coordinate) {
  29. if (!(0, coordinate_1.isPolar)(coordinate))
  30. return (0, d3_shape_1.line)()
  31. .x((d) => d[0])
  32. .y((d) => d[1])(points);
  33. const center = coordinate.getCenter();
  34. return (0, d3_shape_1.arc)()({
  35. startAngle: 0,
  36. endAngle: Math.PI * 2,
  37. outerRadius: (0, vector_1.dist)(points[0], center),
  38. innerRadius: (0, vector_1.dist)(points[1], center),
  39. });
  40. }
  41. function getTransform(coordinate, transform) {
  42. if (!(0, coordinate_1.isPolar)(coordinate))
  43. return transform;
  44. const [cx, cy] = coordinate.getCenter();
  45. return `translate(${cx}, ${cy}) ${transform || ''}`;
  46. }
  47. const Line = (options) => {
  48. const { arrow, arrowSize = 4 } = options, style = __rest(options, ["arrow", "arrowSize"]);
  49. return (points, value, coordinate, theme) => {
  50. const { mark, shape, defaultShape } = value;
  51. const _a = (0, utils_1.getShapeTheme)(theme, mark, shape, defaultShape), { defaultColor, lineWidth } = _a, shapeTheme = __rest(_a, ["defaultColor", "lineWidth"]);
  52. const { color = defaultColor, size = lineWidth } = value;
  53. const arrowMarker = arrow
  54. ? getArrowMarker(arrowSize, Object.assign({ fill: style.stroke || color, stroke: style.stroke || color }, (0, helper_1.subObject)(style, 'arrow')))
  55. : null;
  56. const path = getPath(points, coordinate);
  57. const transform = getTransform(coordinate, value.transform);
  58. return (0, selection_1.select)(new g_1.Path({}))
  59. .call(utils_1.applyStyle, shapeTheme)
  60. .style('d', path)
  61. .style('stroke', color)
  62. .style('lineWidth', size)
  63. .style('transform', transform)
  64. .style('markerEnd', arrowMarker)
  65. .call(utils_1.applyStyle, style)
  66. .node();
  67. };
  68. };
  69. exports.Line = Line;
  70. exports.Line.props = {
  71. defaultMarker: 'line',
  72. defaultEnterAnimation: 'fadeIn',
  73. defaultUpdateAnimation: 'morphing',
  74. defaultExitAnimation: 'fadeOut',
  75. };
  76. //# sourceMappingURL=line.js.map