Introduction to Cytoscape.js: Visualizing Graphs in the Browser

Gili Yaniv
3 min readSep 4, 2023

--

Photo by Firmbee.com on Unsplash

In the world of data representation, graphs provide a powerful way to model relationships and connections between entities. Cytoscape.js is a JavaScript library that allows developers to create interactive and customizable graph visualizations directly within web browsers. Whether you’re building a social network visualization, a biological pathway representation, or any other scenario that involves nodes and edges, Cytoscape.js can be an invaluable tool. In this article, we’ll explore the basics of Cytoscape.js and provide code examples to help you get started.

What is Cytoscape.js?

Cytoscape.js is an open-source graph theory library that enables the creation of complex, customizable, and interactive network visualizations in web browsers. It is built using JavaScript and provides a rich set of features for creating, styling, and interacting with graphs.

With Cytoscape.js, you can easily:

1. Create Graphs: Generate graphs by adding nodes and edges programmatically or by loading data from various sources, such as JSON or CSV files.

2. Customize Styles: Apply custom styles to nodes and edges, including colors, shapes, sizes, and labels, to enhance the visualization’s aesthetics and clarity.

3. Layout Graphs: Arrange nodes using various layout algorithms, such as grid, circle, force-directed, and more, to optimize the presentation of your data.

4. Interactivity: Enable user interactions like zooming, panning, dragging nodes, and highlighting connections to provide a dynamic experience for users.

5. Extensions and Plugins: Extend the library’s functionality by using a wide range of extensions and plugins available in the Cytoscape ecosystem.

Now, let’s dive into some practical examples to demonstrate how to use Cytoscape.js to create engaging graph visualizations.

Example 1: Creating a Simple Graph

In this example, we’ll create a basic graph with a few nodes and edges.

`html
<!DOCTYPE html>
<html>
<head>
<title>Cytoscape.js Example</title>
<script src="https://unpkg.com/cytoscape@3.20.0/dist/cytoscape.min.js"></script>
</head>
<body>
<div id="cy"></div>
<script>
var cy = cytoscape({
container: document.getElementById('cy'),
elements: [
{ data: { id: 'node1', label: 'Node 1' }, position: { x: 100, y: 100 } },
{ data: { id: 'node2', label: 'Node 2' }, position: { x: 300, y: 100 } },
{ data: { id: 'edge1', source: 'node1', target: 'node2' } }
],
style: [
{
selector: 'node',
style: {
'background-color': '#3498db',
'label': 'data(label)'
}
},
{
selector: 'edge',
style: {
'line-color': '#e74c3c'
}
}
]
});
</script>
</body>
</html>

In this example, we first include the Cytoscape.js library using a script tag. Then, within the script tag, we create a new Cytoscape instance inside the `cy` div. We define the elements (nodes and edges) and their positions, and we also specify styles for nodes and edges using CSS-like syntax.

Example 2: Using Layouts and Interactivity

In this example, we’ll create a graph with more nodes and edges and apply a layout to organize them. We’ll also add interactivity features.

<!DOCTYPE html>
<html>
<head>
<title>Cytoscape.js Layout Example</title>
<script src="https://unpkg.com/cytoscape@3.20.0/dist/cytoscape.min.js"></script>
</head>
<body>
<div id="cy"></div>
<script>
var cy = cytoscape({
container: document.getElementById('cy'),
elements: [
{ data: { id: 'a' } },
{ data: { id: 'b' } },
{ data: { id: 'c' } },
{ data: { id: 'ab', source: 'a', target: 'b' } },
{ data: { id: 'bc', source: 'b', target: 'c' } },
{ data: { id: 'ca', source: 'c', target: 'a' } }
],
style: [
{
selector: 'node',
style: {
'background-color': '#2ecc71',
'label': 'data(id)'
}
},
{
selector: 'edge',
style: {
'line-color': '#95a5a6'
}
}
],
layout: {
name: 'circle'
},
userZoomingEnabled: false
});
</script>
</body>
</html>

In this example, we introduce the concept of layouts by specifying the ‘circle’ layout algorithm. This organizes the nodes in a circular arrangement. Additionally, we disable user zooming to prevent users from zooming in and out of the visualization.

Conclusion

Cytoscape.js is a versatile library that empowers developers to create interactive and engaging graph visualizations directly within web browsers. In this article, we covered the basics of Cytoscape.js, including creating graphs, customizing styles, applying layouts, and enabling interactivity. As you delve deeper into Cytoscape.js, you’ll discover its vast potential for representing complex data relationships in an intuitive and visually appealing manner. Whether you’re visualizing social networks, biological pathways, or any other network-based data, Cytoscape.js can be an invaluable tool in your web development toolkit.

Follow me on Twitter, Medium, and Linkedin to read more!

--

--

No responses yet