cluster.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Cluster = exports.hierarchyFunction = void 0;
  4. const d3_hierarchy_1 = require("d3-hierarchy");
  5. const hierarchyFunction = (layoutFunction) => (options) => {
  6. return (data) => {
  7. const { field = 'value', nodeSize, separation, sortBy, as = ['x', 'y'], } = options;
  8. const [x, y] = as;
  9. // Process root data.
  10. const root = (0, d3_hierarchy_1.hierarchy)(data, (d) => d.children)
  11. .sum((d) => d[field])
  12. .sort(sortBy);
  13. // Layout
  14. const c = layoutFunction();
  15. c.size([1, 1]);
  16. if (nodeSize)
  17. c.nodeSize(nodeSize);
  18. if (separation)
  19. c.separation(separation);
  20. c(root);
  21. const nodes = [];
  22. root.each((node) => {
  23. node[x] = node.x;
  24. node[y] = node.y;
  25. node.name = node.data.name;
  26. nodes.push(node);
  27. });
  28. const edges = root.links();
  29. edges.forEach((edge) => {
  30. edge[x] = [edge.source[x], edge.target[x]];
  31. edge[y] = [edge.source[y], edge.target[y]];
  32. });
  33. return { nodes, edges };
  34. };
  35. };
  36. exports.hierarchyFunction = hierarchyFunction;
  37. const Cluster = (options) => {
  38. return (0, exports.hierarchyFunction)(d3_hierarchy_1.cluster)(options);
  39. };
  40. exports.Cluster = Cluster;
  41. exports.Cluster.props = {};
  42. //# sourceMappingURL=cluster.js.map