@antv/layout
Version:
graph layout algorithm
27 lines (24 loc) • 725 B
text/typescript
import type { ID } from '../../../types';
import { DagreGraph } from '../graph';
/**
* TODO: The median method consistently performs better than the barycenter method and has a slight theoretical advantage
*/
export const barycenter = (g: DagreGraph, movable: ID[]) => {
return movable.map((v) => {
const inV = g.getRelatedEdges(v, 'in');
if (!inV?.length) {
return { v };
}
const result = { sum: 0, weight: 0 };
inV?.forEach((e) => {
const nodeU = g.getNode(e.source)!;
result.sum += e.data.weight! * nodeU.data.order!;
result.weight += e.data.weight!;
});
return {
v,
barycenter: result.sum / result.weight,
weight: result.weight,
};
});
};