fCoSE Layout for Cytoscape.js

2.2.0 · active · verified Tue Apr 21

cytoscape-fcose is a high-performance layout extension for Cytoscape.js, implementing the fCoSE (fast Compound Spring Embedder) algorithm. Developed by the i-Vis Lab at Bilkent University, it is significantly faster than its predecessor, CoSE, offering up to 2x speed improvement while maintaining similar aesthetic quality for graph visualizations. The current stable version is 2.2.0, with releases occurring a few times a year, indicating active maintenance. A key differentiator is its robust support for compound graphs and an extensive set of user-defined constraints including fixed node positions, alignment (vertical/horizontal), and relative placement, which can also be applied incrementally. It achieves this by combining spectral layout techniques with force-directed methods, making it suitable for complex network visualizations where both speed and structural clarity are paramount.

Common errors

Warnings

Install

Imports

Quickstart

This code initializes a Cytoscape.js instance, registers the `fcose` layout extension, defines a sample graph with compound nodes, and applies the fCoSE layout with animation and custom constraints for fixed node positions and alignment.

import cytoscape from 'cytoscape';
import fcose from 'cytoscape-fcose';

cytoscape.use(fcose);

const cy = cytoscape({
  container: document.getElementById('cy'),
  elements: [
    { data: { id: 'a', parent: 'c' } },
    { data: { id: 'b', parent: 'c' } },
    { data: { id: 'c' } },
    { data: { id: 'd' } },
    { data: { id: 'e' } },
    { data: { id: 'ab', source: 'a', target: 'b' } },
    { data: { id: 'ad', source: 'a', target: 'd' } },
    { data: { id: 'de', source: 'd', target: 'e' } }
  ],
  style: [
    { selector: 'node', style: { 'background-color': '#666', label: 'data(id)' } },
    { selector: '$node > node', style: { 'background-color': '#ccc', 'padding': '10px' } },
    { selector: 'edge', style: { 'width': 3, 'line-color': '#ccc', 'target-arrow-color': '#ccc', 'target-arrow-shape': 'triangle', 'curve-style': 'bezier' } }
  ]
});

const layoutOptions = {
  name: 'fcose',
  animate: true,
  animationDuration: 500,
  // Constraints example: fix node 'd' at a position
  fixedNodeConstraint: [{
    nodeId: 'd',
    position: { x: 300, y: 100 }
  }],
  // Alignment constraint example: align 'a' and 'b' vertically
  alignmentConstraint: {
    vertical: [['a', 'b']],
    horizontal: []
  },
  nodeRepulsion: 4500,
  idealEdgeLength: 50,
  edgeElasticity: 0.45,
  nestingFactor: 0.1,
  numIter: 2500,
  initialEnergyOnIncremental: 0.3 // For incremental layout
};

cy.layout(layoutOptions).run();

view raw JSON →