To begin creating your graph, you need to set up the drawing area first. Start by adding a <div>
tag in your index.html file. Within the body section, insert a div element with the id "graphArea". This will serve as the canvas for your graph, allowing easy access and customization using Cytoscape.js.
Your updated index.html should resemble the following code:
<!doctype html>
<html>
<head>
<title>Graph Tutorial: Getting Started</title>
<script src='cytoscape.js'></script>
</head>
<body>
<div id="graphArea"></div>
</body>
</html>
Next, enhance the appearance of the graph area by applying some CSS styles. Placing a graph inside an empty div won't be visually appealing, so include the following CSS snippet between the <style> tags:
<style>
#graphArea {
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
}
</style>
Now, let's make the graph visually pleasing! Customize the default styling options provided by Cytoscape.js when initializing the graph:
var cy = cytoscape({
container: document.getElementById('graphArea'),
elements: [
{ data: { id: 'nodeA' } },
{ data: { id: 'nodeB' } },
{
data: {
id: 'edgeAB',
source: 'nodeA',
target: 'nodeB'
}
}],
style: [
{
selector: 'node',
style: {
shape: 'rectangle',
'background-color': 'blue'
}
}]
});
Don't forget to label your nodes for identification purposes. Add labels using the 'label' property in the styling section. Utilize existing data properties such as id to display node labels:
style: [
{
selector: 'node',
style: {
shape: 'rectangle',
'background-color': 'blue',
label: 'data(id)'
}
}]
Lastly, specify the layout of your graph. Include the layout configuration within the cy object after defining the elements:
layout: {
name: 'circle'
}
For more guidance, visit -