Cytoscape.js: The Swiss Army Knife of Graphical Data Visualization
Cytoscape.js is a powerful, open-source JavaScript library for creating and displaying graph data as an interactive, web-based visualization. It allows developers to create and manipulate data visualizations, including nodes (representing entities) and edges (representing relationships between entities), and apply various layout, styling, and interaction options.
But why do we need a data visualization platform?
As per tech jury, we generate 1.145 trillion MB of data daily, and its complexity increases too!
One way of simplifying data is to visualize them after grouping them into the correct categories, and that’s when data visualization tools come into the picture.
So, if you want to bring your data to life, Cytoscape.js is the perfect option. But don’t just take our word for it; look at the platform's versatility while visualizing the data.
What is Cytoscape.js?
Cytoscape.js is an open-source JavaScript library for building interactive graph theory (network) visualizations in a web browser.
It allows developers to create complex, customizable, and responsive visual representations of graph data and can be integrated into web applications for data exploration, analysis, and visualization.
It uses web standards such as HTML, CSS, and JavaScript and can use various programming languages and frameworks.
Companies like Amazon, Fujitsu, Google, HP Enterprise, and IBM use Cytoscape.js for data visualization.
Fun Fact: Cytoscape was originally designed for biological research. Now it is a general platform for complex network analysis and visualization.
The library also has an extensive collection of plugins that can be used to add additional functionality, such as support for various file formats, layout algorithms, and interaction options.
This JavaScript library provides a robust API that allows developers to create, modify, and interact with the graph in multiple ways.
For example, you can create a graph by specifying the nodes and edges, apply various layout algorithms to position the nodes, and style the graph using CSS-like styles. Developers can also create custom interactions by binding event handlers to elements in the graph.
Here is what the output screen looks like for the data we feed in Cytoscape.js:
Architecture of Cytoscape.js
Cytoscape.js is a client-side JavaScript library, meaning it runs in the user’s web browser rather than on a server.
The architecture consists of three main components:
1. The Cytoscape.js core
It is responsible for the underlying graph data model and layout engine. It stores the graph data and handles operations such as adding and removing nodes and edges, querying the graph structure, and calculating node positions for visualization.
The core is implemented in pure JavaScript and has no dependencies on external libraries.
2. The renderer
It is responsible for displaying the visualization elements on the screen. Cytoscape.js uses web technologies such as HTML, CSS, and SVG to render the visualization, which allows for a high degree of flexibility and customization.
3. The layout engine
It is responsible for determining the position of the elements in the visualization. Cytoscape.js supports a variety of layout algorithms, and it also allows developers to create their custom layouts.
Cytoscape.js features built on top of core components
Here’s the list of a few yet essential features built on the core three components of the platform:
1. Interaction
Cytoscape.js provides various interaction options, such as panning, zooming, and selecting elements. Additionally, developers can customize nodes’ interactions and create custom outcomes.
2. Styling
It allows developers to customize the visual appearance of the elements in the visualization using CSS-like styles. This can change the elements’ colour, size, shape, and other attributes.
3. Data loading
Cytoscape.js supports loading data from various sources, such as JSON, CSV, and GraphML. Additionally, developers can create custom data-loading methods to support other formats.
4. Plugins
It has a large community of developers who have created many plugins that extend the library’s functionality. Examples include plugins for adding new layout algorithms, import/export options, and visualizations.
5. Visualizations
Built on top of the Cytoscape core, they use the graph data and layout information provided by the core to render the visual representations of the graph.
The visualizations are implemented using the web standard Canvas element and can be customized using HTML, CSS, and JavaScript.
Additionally, Cytoscape.js can be integrated with other libraries and frameworks, such as D3.js, to provide additional visualization capabilities, such as data binding and transition animations.
How to visualize data using Cytoscape.js?
To visualize data using Cytoscape.js, you will need to prepare your data in the correct format and then use the Cytoscape.js library to visualize the data.
1. Prepare your data
Cytoscape.js expects the data to be a graph, with nodes and edges representing the data points and relationships between them. The data can be in a JSON or CSV file or be passed directly to Cytoscape.js as a JavaScript object.
2. Include the Cytoscape.js library in your HTML file
To use Cytoscape.js, you must include the library in your HTML file by adding a script tag to the head of the document that links to the Cytoscape.js library file.
3. Create a container for the visualization
Create a container element (such as a div) in the HTML file to hold the visualization.
4. Initialize Cytoscape.js and load the data
In your JavaScript code, initialize Cytoscape.js by creating a new Cytoscape object and passing it the container element and the data. You can also specify the layout and styling options for the visualization at this step.
5. Render the visualization
Call the .render() method on the Cytoscape object to render the visualization.
Interact with the visualization (optional)
Cytoscape.js provides a variety of methods and events that allow you to interact with the visualization, such as panning, zooming, and selecting nodes and edges.
Here’s an example of a simple graph visualization using Cytoscape.js:
The code depicted in the above image is a JavaScript script that creates a simple Cytoscape graph with two nodes, ‘a’ and ‘b’, and one edge connecting them. The graph is created by calling the cytoscape() function and passing in an options object. The options object contains various properties that define the properties of the graph.
The ‘container’ property is used to specify the <div> element that will serve as the container for the graph. In this case, it specifies the <div> element with an ID of “cy”.
The elements property defines the nodes and edges of the graph. Here, it defines two nodes, ‘a’ and ‘b’, and one edge connecting them.
The style property is used to define the visual style of the graph. This code sets the nodes’ background colour to “#666” and displays the node’s ID as a label.
It also sets the width of edges to 3, the line colour and target arrow colour to “#ccc”, and the target arrow shapes to a triangle.
The layout property is used to define the layout of the graph. Here it specifies the ‘grid’ layout with one row.
Once the graph is created, it is displayed inside the specified <div> element and is styled according to the provided styles.
Note: For this script to run correctly, it must be included in an HTML file with the necessary dependencies. One of those dependencies is an <div> element with an ID of “cy.” This <div> element serves as a container for the Cytoscape graph, and the script uses the document.getElementById(‘cy’) method to access it.
It is also essential to check whether the version of cytoscape.js included in the code is compatible with the cytoscape.js you’re using in your project.
Closing lines
Because of its domain independence, Cytoscape.js has proven to be a valuable tool for creating interactive graph theory visualizations.
In addition, its open-source nature, client-side architecture, and extensive customization options make it a valuable tool for data exploration and analysis in any field.
The visualization tool brings data to life and enables the user to derive bottlenecks and decisions regarding where the process is failing and helping.
In this article, we have covered the essential nodes of Cytoscape.js that may have interested coders, programmers, researchers and readers to identify the utility of a platform that can help them while dealing with so much data.
While the applications, utilities, and feature list seem never-ending, we hope we’ve cleared your primary queries around this data visualization platform that is underrated yet widely used but not talked about much!
This article was initially published at https://bit.ly/3m06um0