badge.js 3.1 KB

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