cluster.js 1.2 KB

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