Skip to content

Commit c108d1f

Browse files
committed
fix(sankey): implemented suggested change by @Ovilia to follow sunburst.sort method. Only null and desc implemented as suggested
1 parent 6418ca2 commit c108d1f

3 files changed

Lines changed: 20 additions & 30 deletions

File tree

src/chart/sankey/SankeySeries.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ export interface SankeySeriesOption
137137
*/
138138
layoutIterations?: number
139139
/**
140-
* Whether to sort nodes by current coordinate when resolving collisions.
141-
* Set to false to preserve the original node order in each depth column.
140+
* Sorting method used when resolving node collisions within each depth column.
141+
* Set to null to preserve the original node order.
142142
*/
143-
sortNodes?: boolean
143+
sort?: 'desc' | null
144144

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

@@ -323,7 +323,7 @@ class SankeySeriesModel extends SeriesModel<SankeySeriesOption> {
323323
draggable: true,
324324

325325
layoutIterations: 32,
326-
sortNodes: true,
326+
sort: 'desc',
327327

328328
// true | false | 'move' | 'scale', see module:component/helper/RoamController.
329329
roam: false,

src/chart/sankey/sankeyLayout.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ export default function sankeyLayout(ecModel: GlobalModel, api: ExtensionAPI) {
5757
const orient = seriesModel.get('orient');
5858

5959
const nodeAlign = seriesModel.get('nodeAlign');
60-
const sortNodes = seriesModel.get('sortNodes');
60+
const sort = seriesModel.get('sort');
6161

62-
layoutSankey(nodes, edges, nodeWidth, nodeGap, width, height, iterations, orient, nodeAlign, sortNodes);
62+
layoutSankey(nodes, edges, nodeWidth, nodeGap, width, height, iterations, orient, nodeAlign, sort);
6363
});
6464
}
6565

@@ -73,10 +73,10 @@ function layoutSankey(
7373
iterations: number,
7474
orient: LayoutOrient,
7575
nodeAlign: SankeySeriesOption['nodeAlign'],
76-
sortNodes: boolean
76+
sort: SankeySeriesOption['sort']
7777
) {
7878
computeNodeBreadths(nodes, edges, nodeWidth, width, height, orient, nodeAlign);
79-
computeNodeDepths(nodes, edges, height, width, nodeGap, iterations, orient, sortNodes);
79+
computeNodeDepths(nodes, edges, height, width, nodeGap, iterations, orient, sort);
8080
computeEdgeDepths(nodes, orient);
8181
}
8282

@@ -259,7 +259,7 @@ function scaleNodeBreadths(nodes: GraphNode[], kx: number, orient: LayoutOrient)
259259
* @param nodeGap the vertical distance between two nodes
260260
* in the same column.
261261
* @param iterations the number of iterations for the algorithm
262-
* @param sortNodes whether to sort the nodes by y-position
262+
* @param sort sorting method used when resolving collisions within each column
263263
*/
264264
function computeNodeDepths(
265265
nodes: GraphNode[],
@@ -269,21 +269,21 @@ function computeNodeDepths(
269269
nodeGap: number,
270270
iterations: number,
271271
orient: LayoutOrient,
272-
sortNodes: boolean
272+
sort: SankeySeriesOption['sort']
273273
) {
274274
const nodesByBreadth = prepareNodesByBreadth(nodes, orient);
275275

276276
initializeNodeDepth(nodesByBreadth, edges, height, width, nodeGap, orient);
277-
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sortNodes);
277+
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sort);
278278

279279
for (let alpha = 1; iterations > 0; iterations--) {
280280
// 0.99 is a experience parameter, ensure that each iterations of
281281
// changes as small as possible.
282282
alpha *= 0.99;
283283
relaxRightToLeft(nodesByBreadth, alpha, orient);
284-
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sortNodes);
284+
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sort);
285285
relaxLeftToRight(nodesByBreadth, alpha, orient);
286-
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sortNodes);
286+
resolveCollisions(nodesByBreadth, nodeGap, height, width, orient, sort);
287287
}
288288
}
289289

@@ -360,11 +360,11 @@ function resolveCollisions(
360360
height: number,
361361
width: number,
362362
orient: LayoutOrient,
363-
sortNodes: boolean
363+
sort: SankeySeriesOption['sort']
364364
) {
365365
const keyAttr = orient === 'vertical' ? 'x' : 'y';
366366
zrUtil.each(nodesByBreadth, function (nodes) {
367-
if (sortNodes !== false) {
367+
if (sort !== null) {
368368
nodes.sort(function (a, b) {
369369
return a.getLayout()[keyAttr] - b.getLayout()[keyAttr];
370370
});
@@ -535,4 +535,4 @@ function computeEdgeDepths(nodes: GraphNode[], orient: LayoutOrient) {
535535
ty += edge.getLayout().dy;
536536
});
537537
});
538-
}
538+
}

test/sankey-node-sorting.html

Lines changed: 4 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)