pack.js 3.4 KB

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