Skip to content

Commit 4adcf35

Browse files
committed
Fix: Hierarchical layout recenter logic
1 parent 69ee355 commit 4adcf35

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

src/renderer/canvas/canvas-renderer.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,19 @@ export class CanvasRenderer<N extends INodeBase, E extends IEdgeBase> extends Em
290290
// Graph view is a bounding box of the graph nodes that takes into
291291
// account node positions (x, y) and node sizes (style: size + border width)
292292
const graphView = graph.getBoundingBox();
293-
const graphMiddleX = options?.anchorX === 'center' ? graphView.x + graphView.width / 2 : 0;
294-
const graphMiddleY = options?.anchorY === 'center' ? graphView.y + graphView.height / 2 : 0;
293+
const graphMiddleX =
294+
options?.anchorX === 'center'
295+
? graphView.x + graphView.width / 2
296+
: options?.anchorX === 'end'
297+
? graphView.x + graphView.width
298+
: 0;
299+
300+
const graphMiddleY =
301+
options?.anchorY === 'center'
302+
? graphView.y + graphView.height / 2
303+
: options?.anchorY === 'end'
304+
? graphView.y + graphView.height
305+
: 0;
295306

296307
// Simulation view is actually a renderer view (canvas) but in the coordinate system of
297308
// the simulator: node position (x, y). We want to fit a graph view into a simulation view.

src/renderer/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export interface IRendererSettingsInit extends IRendererSettings {
3737
}
3838

3939
export interface IFitZoomTransformOptions {
40-
anchorX?: 'center' | 'start';
41-
anchorY?: 'center' | 'start';
40+
anchorX?: 'start' | 'center' | 'end';
41+
anchorY?: 'start' | 'center' | 'end';
4242
}
4343

4444
export type RendererEvents = {

src/views/orb-view.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,14 @@ export class OrbView<N extends INodeBase, E extends IEdgeBase> implements IOrbVi
293293

294294
recenter(onRendered?: () => void) {
295295
const layout = this._settings.layout;
296+
const isHorizontal =
297+
layout.type === 'hierarchical' && (layout.options as IHierarchicalLayoutOptions).orientation === 'horizontal';
298+
const isVertical =
299+
layout.type === 'hierarchical' && (layout.options as IHierarchicalLayoutOptions).orientation === 'vertical';
300+
const reversed = (layout.options as IHierarchicalLayoutOptions).reversed;
296301
const recenterOptions: IFitZoomTransformOptions = {
297-
anchorX:
298-
layout.type === 'hierarchical' && (layout.options as IHierarchicalLayoutOptions).orientation === 'horizontal'
299-
? 'start'
300-
: 'center',
301-
anchorY:
302-
layout.type === 'hierarchical' && (layout.options as IHierarchicalLayoutOptions).orientation === 'vertical'
303-
? 'start'
304-
: 'center',
302+
anchorX: isHorizontal ? (reversed ? 'end' : 'start') : 'center',
303+
anchorY: isVertical ? (reversed ? 'end' : 'start') : 'center',
305304
};
306305
const fitZoomTransform = this._renderer.getFitZoomTransform(this._graph, recenterOptions);
307306

0 commit comments

Comments
 (0)