What is cytoscape-fcose?
The cytoscape-fcose package is an extension for Cytoscape.js that provides a layout algorithm for graph visualization. It is designed to handle large-scale graphs efficiently and offers features like compound nodes, multi-level nesting, and more.
What are cytoscape-fcose's main functionalities?
Basic Layout
This code initializes a Cytoscape instance with a basic graph containing two nodes and one edge, and applies the fcose layout algorithm to it.
const cy = cytoscape({
container: document.getElementById('cy'),
elements: [
{ data: { id: 'a' } },
{ data: { id: 'b' } },
{ data: { id: 'ab', source: 'a', target: 'b' } }
],
style: [
{
selector: 'node',
style: {
'background-color': '#666',
'label': 'data(id)'
}
},
{
selector: 'edge',
style: {
'width': 3,
'line-color': '#ccc'
}
}
],
layout: {
name: 'fcose'
}
});
Compound Nodes
This code demonstrates the use of compound nodes, where nodes 'b' and 'c' are children of node 'a'. The fcose layout algorithm is applied to arrange the nodes.
const cy = cytoscape({
container: document.getElementById('cy'),
elements: [
{ data: { id: 'a' } },
{ data: { id: 'b', parent: 'a' } },
{ data: { id: 'c', parent: 'a' } },
{ data: { id: 'ab', source: 'b', target: 'c' } }
],
style: [
{
selector: 'node',
style: {
'background-color': '#666',
'label': 'data(id)'
}
},
{
selector: 'edge',
style: {
'width': 3,
'line-color': '#ccc'
}
}
],
layout: {
name: 'fcose'
}
});
Custom Layout Options
This code shows how to customize the fcose layout options, including setting the quality to 'proof', enabling randomization, and adding animation with a duration of 1000 milliseconds.
const cy = cytoscape({
container: document.getElementById('cy'),
elements: [
{ data: { id: 'a' } },
{ data: { id: 'b' } },
{ data: { id: 'ab', source: 'a', target: 'b' } }
],
style: [
{
selector: 'node',
style: {
'background-color': '#666',
'label': 'data(id)'
}
},
{
selector: 'edge',
style: {
'width': 3,
'line-color': '#ccc'
}
}
],
layout: {
name: 'fcose',
quality: 'proof',
randomize: true,
animate: true,
animationDuration: 1000
}
});
Other packages similar to cytoscape-fcose
cytoscape-cola
The cytoscape-cola package provides a layout algorithm based on the Cola.js library, which is also designed for handling large-scale graphs. It offers features like constraint-based layout and support for compound nodes. Compared to cytoscape-fcose, it focuses more on constraint-based layouts and may offer different performance characteristics.
cytoscape-dagre
The cytoscape-dagre package provides a layout algorithm based on the Dagre library, which is optimized for directed acyclic graphs (DAGs). It is particularly useful for hierarchical layouts. While cytoscape-fcose is more general-purpose, cytoscape-dagre excels in scenarios where a clear hierarchical structure is needed.
cytoscape-cose-bilkent
The cytoscape-cose-bilkent package offers a layout algorithm similar to fcose but with different optimization strategies and performance characteristics. It is also designed for large-scale graphs and supports compound nodes. The main difference lies in the specific heuristics and optimizations used in the layout process.
cytoscape-fcose
Description
fCoSE (pron. "f-cosay", fast Compound Spring Embedder), is a faster version of our earlier compound spring embedder algorithm named CoSE, implemented as a Cytoscape.js extension by i-Vis Lab in Bilkent University.
Here are some demos: simple, compound, and constraints, respectively:
fCoSE layout algorithm combines the speed of spectral layout with the aesthetics of force-directed layout. fCoSE runs up to 2 times as fast as CoSE while achieving similar aesthetics.
Furthermore, fCoSE also supports a fairly rich set of constraint types (i.e., fixed position, vertical/horizontal alignment and relative placement).
You can see constraint support in action in the following videos: fixed node, alignment, relative placement, hybrid, real life graphs. Constraints can also be added incrementally on a given layout.
Please cite the following when you use this layout:
H. Balci and U. Dogrusoz, "fCoSE: A Fast Compound Graph Layout Algorithm with Constraint Support," in IEEE Transactions on Visualization and Computer Graphics, 28(12), pp. 4582-4593, 2022.
U. Dogrusoz, E. Giral, A. Cetintas, A. Civril and E. Demir, "A Layout Algorithm For Undirected Compound Graphs", Information Sciences, 179, pp. 980-994, 2009.
Dependencies
- Cytoscape.js ^3.2.0
- cose-base ^2.0.0
- cytoscape-layout-utilities.js (optional for packing disconnected components) ^1.0.0
Documentation
fCoSE supports user-defined placement constraints as well as its full support for compound graphs. These constraints may be defined for simple nodes. Supported constraint types are:
-
Fixed node constraint: The user may provide exact desired positions for a set of nodes called fixed nodes. For example, in order to position node n1 to (x: 100, y: 200) and node n2 to (x: 200, y: -300) as a result of the layout, fixedNodeConstraint
option should be set as follows:
fixedNodeConstraint: [{nodeId: 'n1', position: {x: 100, y: 200}},
{nodeId: 'n2', position: {x: 200, y: -300}}],
-
Alignment constraint: This constraint aims to align two or more nodes (with respect to their centers) vertically or horizontally. For example, for the vertical alignment of nodes {n1, n2, n3} and {n4, n5}, and horizontal alignment of nodes {n2, n4} as a result of the layout, alignmentConstraint
option should be set as follows:
alignmentConstraint: {vertical: [['n1', 'n2', 'n3'], ['n4', 'n5']], horizontal: [['n2', 'n4']]},
Note: Alignment constraints in a direction must be given in most compact form. Example: ['n1', 'n2', 'n3']
instead of ['n1', 'n2'], ['n1', 'n3']
.
-
Relative placement constraint: The user may constrain the position of a node relative to another node in either vertical or horizontal direction. For example, in order to position node n1 to be above of node n2 by at least 100 pixels and position node n3 to be on the left of node n4 by at least 75 pixels as a result of the layout, relativePlacementConstraint
option should be set as follows:
relativePlacementConstraint: [{top: 'n1', bottom: 'n2', gap: 100},
{left: 'n3', right: 'n4', gap: 75}],
The gap
property is optional. If it is omitted, average idealEdgeLength
is used as the gap value.
Usage instructions
Download the library:
- via npm:
npm install cytoscape-fcose
, - via bower:
bower install cytoscape-fcose
, or - via direct download in the repository (probably from a tag).
Import the library as appropriate for your project:
ES import:
import cytoscape from 'cytoscape';
import fcose from 'cytoscape-fcose';
cytoscape.use( fcose );
CommonJS require:
let cytoscape = require('cytoscape');
let fcose = require('cytoscape-fcose');
cytoscape.use( fcose );
AMD:
require(['cytoscape', 'cytoscape-fcose'], function( cytoscape, fcose ){
fcose( cytoscape );
});
Plain HTML/JS has the extension registered for you automatically, because no require()
is needed. Just add the following files:
<script src="https://unpkg.com/layout-base/layout-base.js"></script>
<script src="https://unpkg.com/cose-base/cose-base.js"></script>
<script src="https://unpkg.com/cytoscape-fcose/cytoscape-fcose.js"></script>
API
When calling the layout, e.g. cy.layout({ name: 'fcose', ... })
, the following options are supported:
var defaultOptions = {
quality: "default",
randomize: true,
animate: true,
animationDuration: 1000,
animationEasing: undefined,
fit: true,
padding: 30,
nodeDimensionsIncludeLabels: false,
uniformNodeDimensions: false,
packComponents: true,
step: "all",
samplingType: true,
sampleSize: 25,
nodeSeparation: 75,
piTol: 0.0000001,
nodeRepulsion: node => 4500,
idealEdgeLength: edge => 50,
edgeElasticity: edge => 0.45,
nestingFactor: 0.1,
numIter: 2500,
tile: true,
tilingCompareBy: undefined,
tilingPaddingVertical: 10,
tilingPaddingHorizontal: 10,
gravity: 0.25,
gravityRangeCompound: 1.5,
gravityCompound: 1.0,
gravityRange: 3.8,
initialEnergyOnIncremental: 0.3,
fixedNodeConstraint: undefined,
alignmentConstraint: undefined,
relativePlacementConstraint: undefined,
ready: () => {},
stop: () => {}
};
To be able to use packComponents
option, cytoscape-layout-utilities
extension should also be registered in the application.
Packing related options should be set via cytoscape-layout-utilities
extension.
If they are not set, fCoSE uses default options.
Build targets
npm run test
: Run Mocha tests in ./test
npm run build
: Build ./src/**
into cytoscape-fcose.js
npm run watch
: Automatically build on changes with live reloading (N.b. you must already have an HTTP server running)npm run dev
: Automatically build on changes with live reloading with webpack dev servernpm run lint
: Run eslint on the source
N.b. all builds use babel, so modern ES features can be used in the src
.
Publishing instructions
This project is set up to automatically be published to npm and bower. To publish:
- Build the extension :
npm run build:release
- Commit the build :
git commit -am "Build for release"
- Bump the version number and tag:
npm version major|minor|patch
- Push to origin:
git push && git push --tags
- Publish to npm:
npm publish .
- If publishing to bower for the first time, you'll need to run
bower register cytoscape-fcose https://github.com/iVis-at-Bilkent/cytoscape.js-fcose.git
- Make a new release for Zenodo.
Team