pack.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.Pack = void 0;
  15. const util_1 = require("@antv/util");
  16. const d3_hierarchy_1 = require("d3-hierarchy");
  17. const helper_1 = require("../utils/helper");
  18. const size_1 = require("../utils/size");
  19. const mark_1 = require("../utils/mark");
  20. const utils_1 = require("./utils");
  21. const dataTransform = (data, layout, encode) => {
  22. const { value } = encode;
  23. const root = (0, util_1.isArray)(data)
  24. ? (0, d3_hierarchy_1.stratify)().path(layout.path)(data)
  25. : (0, d3_hierarchy_1.hierarchy)(data);
  26. value ? root.sum((d) => (0, utils_1.field)(value)(d)).sort(layout.sort) : root.count();
  27. // @ts-ignore
  28. (0, d3_hierarchy_1.pack)().size(layout.size).padding(layout.padding)(root);
  29. return root.descendants();
  30. };
  31. const Pack = (markOptions) => {
  32. return (viewOptions) => {
  33. const { width, height } = (0, size_1.getBBoxSize)(viewOptions);
  34. const { data, encode = {}, scale = {}, style = {}, layout = {}, labels = [], tooltip = {} } = markOptions, resOptions = __rest(markOptions, ["data", "encode", "scale", "style", "layout", "labels", "tooltip"]);
  35. const DEFAULT_LAYOUT_OPTIONS = {
  36. size: [width, height],
  37. padding: 0,
  38. sort: (a, b) => b.value - a.value,
  39. };
  40. const DEFAULT_OPTIONS = {
  41. type: 'point',
  42. axis: false,
  43. legend: false,
  44. scale: {
  45. x: { domain: [0, width] },
  46. y: { domain: [0, height] },
  47. size: { type: 'identity' },
  48. },
  49. encode: {
  50. x: 'x',
  51. y: 'y',
  52. size: 'r',
  53. shape: 'point',
  54. },
  55. style: {
  56. fill: !encode.color
  57. ? (d) => (d.height === 0 ? '#ddd' : '#fff')
  58. : undefined,
  59. stroke: !encode.color
  60. ? (d) => (d.height === 0 ? '' : '#000')
  61. : undefined,
  62. },
  63. };
  64. const DEFAULT_LABEL_OPTIONS = {
  65. text: '',
  66. position: 'inside',
  67. textOverflow: 'clip',
  68. wordWrap: true,
  69. maxLines: 1,
  70. wordWrapWidth: (d) => d.r * 2,
  71. };
  72. const DEFAULT_TOOLTIP_OPTIONS = {
  73. title: (d) => d.data.name,
  74. items: [{ field: 'value' }],
  75. };
  76. const transformedData = dataTransform(data, (0, util_1.deepMix)({}, DEFAULT_LAYOUT_OPTIONS, layout), (0, util_1.deepMix)({}, DEFAULT_OPTIONS['encode'], encode));
  77. const labelStyle = (0, helper_1.subObject)(style, 'label');
  78. return [
  79. (0, util_1.deepMix)({}, DEFAULT_OPTIONS, Object.assign(Object.assign({ data: transformedData, encode,
  80. scale,
  81. style, labels: [
  82. Object.assign(Object.assign({}, DEFAULT_LABEL_OPTIONS), labelStyle),
  83. ...labels,
  84. ] }, resOptions), { tooltip: (0, mark_1.maybeTooltip)(tooltip, DEFAULT_TOOLTIP_OPTIONS), axis: false })),
  85. ];
  86. };
  87. };
  88. exports.Pack = Pack;
  89. exports.Pack.props = {};
  90. //# sourceMappingURL=pack.js.map