Skip to content

Commit fcad0db

Browse files
committed
Cherry pick to master: Merge pull request #3396 from cytoscape/fix/single-node-cose-bb
Single element cose layout has no display #3379 : patch & test
1 parent 4df41a3 commit fcad0db

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

playwright-tests/renderer.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,4 +568,43 @@ test.describe('Renderer', () => {
568568

569569
}); // Rounded edges
570570

571+
test.describe('with layout', () => {
572+
573+
test('single node cose layout with bounding box', async ({ page }) => {
574+
const pos = await page.evaluate(async () => {
575+
const cy = window.cy;
576+
577+
// remove all eles
578+
cy.elements().remove();
579+
580+
// add one node
581+
let node = cy.add({ data: { id: 'a' } });
582+
583+
// run layout
584+
let layout = cy.layout({
585+
name: 'cose',
586+
boundingBox: {
587+
x1: 0,
588+
y1: 0,
589+
x2: 100,
590+
y2: 100
591+
},
592+
});
593+
594+
let layoutstop = layout.promiseOn('layoutstop');
595+
596+
layout.run();
597+
598+
await layoutstop;
599+
600+
return node.position();
601+
});
602+
603+
expect(pos.x).not.toBeNaN();
604+
expect(pos.y).not.toBeNaN();
605+
606+
}); // single node cose layout
607+
608+
}); // with layout
609+
571610
}); // renderer

src/extensions/layout/cose.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,9 @@ var getScaleInBoundsFn = function( layoutInfo, options, nodes ){
644644
var lnode = layoutInfo.layoutNodes[ layoutInfo.idToIndex[ ele.data( 'id' ) ] ];
645645

646646
if( options.boundingBox ){ // then add extra bounding box constraint
647-
var pctX = (lnode.positionX - coseBB.x1) / coseBB.w;
648-
var pctY = (lnode.positionY - coseBB.y1) / coseBB.h;
647+
// Handle single node case where coseBB.w or coseBB.h is 0
648+
var pctX = coseBB.w === 0 ? 0.5 : (lnode.positionX - coseBB.x1) / coseBB.w;
649+
var pctY = coseBB.h === 0 ? 0.5 : (lnode.positionY - coseBB.y1) / coseBB.h;
649650

650651
return {
651652
x: bb.x1 + pctX * bb.w,

0 commit comments

Comments
 (0)