badge.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 { Marker } from '@antv/gui';
  13. import { createElement } from '../../utils/createElement';
  14. import { subObject } from '../../utils/helper';
  15. import { select } from '../../utils/selection';
  16. import { applyStyle, getShapeTheme } from '../../shape/utils';
  17. /**
  18. * Get the path to draw a built-in badge, which is like a balloon.
  19. */
  20. function getPath(r) {
  21. const offset = r / Math.sqrt(2);
  22. const dy = r * Math.sqrt(2);
  23. const [p0x, p0y] = [-offset, offset - dy];
  24. const [p1x, p1y] = [0, 0];
  25. const [p2x, p2y] = [offset, offset - dy];
  26. return [
  27. ['M', p0x, p0y],
  28. ['A', r, r, 0, 1, 1, p2x, p2y],
  29. ['L', p1x, p1y],
  30. ['Z'],
  31. ];
  32. }
  33. function inferTextPosition(shape) {
  34. const { min, max } = shape.getLocalBounds();
  35. return [(min[0] + max[0]) * 0.5, (min[1] + max[1]) * 0.5];
  36. }
  37. const BadgeShape = createElement((g) => {
  38. const _a = g.attributes, { class: className, x: x0, y: y0 } = _a, rest = __rest(_a, ["class", "x", "y"]);
  39. const markerStyle = subObject(rest, 'marker');
  40. const { size = 24 } = markerStyle;
  41. const symbol = () => getPath(size / 2);
  42. const bgShape = select(g)
  43. .maybeAppend('marker', () => new Marker({}))
  44. .call((selection) => selection.node().update(Object.assign({ symbol }, markerStyle)))
  45. .node();
  46. const [x, y] = inferTextPosition(bgShape);
  47. select(g)
  48. .maybeAppend('text', 'text')
  49. .style('x', x)
  50. .style('y', y)
  51. .call(applyStyle, rest);
  52. });
  53. export const Badge = (options) => {
  54. const style = __rest(options, []);
  55. return (points, value, coordinate, theme) => {
  56. const { mark, shape, defaultShape } = value;
  57. const _a = getShapeTheme(theme, mark, shape, defaultShape), { defaultColor } = _a, shapeTheme = __rest(_a, ["defaultColor"]);
  58. const { color, text = '' } = value;
  59. const textStyle = {
  60. text: String(text),
  61. stroke: color || defaultColor,
  62. fill: color || defaultColor,
  63. };
  64. const [[x0, y0]] = points;
  65. return select(new BadgeShape())
  66. .call(applyStyle, shapeTheme)
  67. .style('x', x0)
  68. .style('y', y0)
  69. .call(applyStyle, textStyle)
  70. .call(applyStyle, style)
  71. .node();
  72. };
  73. };
  74. Badge.props = {
  75. defaultMarker: 'point',
  76. defaultEnterAnimation: 'fadeIn',
  77. defaultUpdateAnimation: 'morphing',
  78. defaultExitAnimation: 'fadeOut',
  79. };
  80. //# sourceMappingURL=badge.js.map