tree.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { deepMix } from '@antv/util';
  2. import { subObject } from '../utils/helper';
  3. import { Tree as TreeTransform, } from '../data/tree';
  4. import { maybeAnimation, subTooltip } from '../utils/mark';
  5. export const Tree = (options) => {
  6. const DEFAULT_LAYOUT_OPTIONS = {
  7. sortBy: (a, b) => b.value - a.value,
  8. };
  9. const DEFAULT_NODE_OPTIONS = {
  10. axis: false,
  11. legend: false,
  12. type: 'point',
  13. encode: {
  14. x: 'x',
  15. y: 'y',
  16. size: 2,
  17. shape: 'point',
  18. },
  19. };
  20. const DEFAULT_LINK_OPTIONS = {
  21. type: 'link',
  22. encode: {
  23. x: 'x',
  24. y: 'y',
  25. shape: 'smooth',
  26. },
  27. };
  28. const DEFAULT_LABEL_OPTIONS = {
  29. text: '',
  30. fontSize: 10,
  31. };
  32. return () => {
  33. const { data, encode = {}, scale = {}, style = {}, layout = {}, nodeLabels = [], linkLabels = [], animate = {}, tooltip = {}, } = options;
  34. const valueEncode = encode === null || encode === void 0 ? void 0 : encode.value;
  35. const { nodes, edges } = TreeTransform(Object.assign(Object.assign(Object.assign({}, DEFAULT_LAYOUT_OPTIONS), layout), { field: valueEncode }))(data);
  36. const nodeTooltip = subTooltip(tooltip, 'node', {
  37. title: 'name',
  38. items: ['value'],
  39. }, true);
  40. const linkTooltip = subTooltip(tooltip, 'link', {
  41. title: '',
  42. items: [
  43. (d) => ({ name: 'source', value: d.source.name }),
  44. (d) => ({ name: 'target', value: d.target.name }),
  45. ],
  46. });
  47. return [
  48. deepMix({}, DEFAULT_LINK_OPTIONS, {
  49. data: edges,
  50. encode: subObject(encode, 'link'),
  51. scale: subObject(scale, 'link'),
  52. labels: linkLabels,
  53. style: Object.assign({ stroke: '#999' }, subObject(style, 'link')),
  54. tooltip: linkTooltip,
  55. animate: maybeAnimation(animate, 'link'),
  56. }),
  57. deepMix({}, DEFAULT_NODE_OPTIONS, {
  58. data: nodes,
  59. scale: subObject(scale, 'node'),
  60. encode: subObject(encode, 'node'),
  61. labels: [
  62. Object.assign(Object.assign({}, DEFAULT_LABEL_OPTIONS), subObject(style, 'label')),
  63. ...nodeLabels,
  64. ],
  65. style: Object.assign({}, subObject(style, 'node')),
  66. tooltip: nodeTooltip,
  67. animate: maybeAnimation(animate, 'node'),
  68. }),
  69. ];
  70. };
  71. };
  72. Tree.props = {};
  73. //# sourceMappingURL=tree.js.map