Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/chart/sankey/SankeySeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ export interface SankeySeriesOption
* The number of iterations to change the position of the node
*/
layoutIterations?: number
/**
* Sorting method used when resolving node collisions within each depth column.
* Set to null to preserve the original node order.
*/
sort?: 'desc' | null

nodeAlign?: 'justify' | 'left' | 'right' // TODO justify should be auto

Expand Down Expand Up @@ -318,6 +323,7 @@ class SankeySeriesModel extends SeriesModel<SankeySeriesOption> {
draggable: true,

layoutIterations: 32,
sort: 'desc',

// true | false | 'move' | 'scale', see module:component/helper/RoamController.
roam: false,
Expand Down
31 changes: 19 additions & 12 deletions src/chart/sankey/sankeyLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ export default function sankeyLayout(ecModel: GlobalModel, api: ExtensionAPI) {
const orient = seriesModel.get('orient');

const nodeAlign = seriesModel.get('nodeAlign');
const sort = seriesModel.get('sort');

layoutSankey(nodes, edges, nodeWidth, nodeGap, width, height, iterations, orient, nodeAlign);
layoutSankey(nodes, edges, nodeWidth, nodeGap, width, height, iterations, orient, nodeAlign, sort);
});
}

Expand All @@ -71,10 +72,11 @@ function layoutSankey(
height: number,
iterations: number,
orient: LayoutOrient,
nodeAlign: SankeySeriesOption['nodeAlign']
nodeAlign: SankeySeriesOption['nodeAlign'],
sort: SankeySeriesOption['sort']
) {
computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nodeAlign);
computeNodeDepths(nodes, edges, height, width, nodeGap, iterations, orient);
computeNodeDepths(nodes, edges, height, width, nodeGap, iterations, orient, sort);
computeEdgeDepths(nodes, orient);
}

Expand Down Expand Up @@ -257,6 +259,7 @@ function scaleNodeBreadths(nodes: GraphNode[], kx: number, orient: LayoutOrient)
* @param nodeGap the vertical distance between two nodes
* in the same column.
* @param iterations the number of iterations for the algorithm
* @param sort sorting method used when resolving collisions within each column
*/
function computeNodeDepths(
nodes: GraphNode[],
Expand All @@ -265,21 +268,22 @@ function computeNodeDepths(
width: number,
nodeGap: number,
iterations: number,
orient: LayoutOrient
orient: LayoutOrient,
sort: SankeySeriesOption['sort']
) {
const nodesByBreadth = prepareNodesByBreadth(nodes, orient);

initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orient);
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient);
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sort);

for (let alpha = 1; iterations > 0; iterations--) {
// 0.99 is a experience parameter, ensure that each iterations of
// changes as small as possible.
alpha *= 0.99;
relaxRightToLeft(nodesByBreadth, alpha, orient);
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient);
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sort);
relaxLeftToRight(nodesByBreadth, alpha, orient);
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient);
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sort);
}
}

Expand Down Expand Up @@ -355,13 +359,16 @@ function resolveCollisions(
nodeGap: number,
height: number,
width: number,
orient: LayoutOrient
orient: LayoutOrient,
sort: SankeySeriesOption['sort']
) {
const keyAttr = orient === 'vertical' ? 'x' : 'y';
zrUtil.each(nodesByBreadth, function (nodes) {
nodes.sort(function (a, b) {
return a.getLayout()[keyAttr] - b.getLayout()[keyAttr];
});
if (sort !== null) {
nodes.sort(function (a, b) {
return a.getLayout()[keyAttr] - b.getLayout()[keyAttr];
});
}
let nodeX;
let node;
let dy;
Expand Down Expand Up @@ -528,4 +535,4 @@ function computeEdgeDepths(nodes: GraphNode[], orient: LayoutOrient) {
ty += edge.getLayout().dy;
});
});
}
}
87 changes: 87 additions & 0 deletions test/sankey-node-sorting.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.