Enhancing Your Cytoscape Graph Node with an HTML Label

Currently, I am utilizing cytoscape.js to visually represent relationships between nodes. Specifically, I am trying to generate unique and stylish labels for a single node. My goal is to create more sophisticated label styles than those demonstrated in the official example on cytoscape.org.

Is there a way to achieve this level of customization?

Attached below is an image exemplifying the issue I am facing:

Answer №1

I successfully resolved my issue by using an extension to create HTML labels for cytoscape nodes.

Check out the extension on github: cytoscape-node-html-label

You can also see a demo of the extension here: demo

cy.nodeHtmlLabel(
[
    {
        query: 'node',
        tpl: function(data){
        return '<p class="line1">line 1</p><p class="line1">line 2</p>'}
    }
]
    );
.line1{
font-size: 10px;
}
.line1{
font-size: 12px;
}

Answer №2

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 -

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating elements with wrapping capabilities in ExtJS

I'm puzzled by what should be a straightforward issue. I am trying to place items (buttons, specifically) on a panel so that they are displayed side-by-side and wrap once they reach the end of the panel... Appreciate any insights, JJ ...

Image expanded on a spherical surface

How can I resolve the issue of my image being stretched out and pixelated when using three.js to cover a sphere with a photo? Here's the code snippet causing the problem: moonSurface = new THREE.Mesh( new THREE.SphereGeometry(moonRadius -.1, 50, ...

Navigating - Utilizing dot-notation to reach the top-level function in Express

If we want to use express in a basic javascript file, all we need to do is add the following two lines of code at the beginning (after installing it through npm): var foo = require('express'); var app = foo(); According to the express API guide ...

What steps can be taken to transform example.com/blog.html into example.com/blog?

www.awesomenewgames.com/articles I need to change the URL of this page from ending with /articles to simply /blog. So the desired result should be: www.awesomenewgames.com/blog What steps do I need to take to make this change? Any guidance on this matte ...

Choosing the following choice using the Material-UI Select tool

Looking for a way to enhance my dropdown select from MUI in a Nextjs application by adding two arrows for navigating to the next/previous option. Here's what my code currently looks like: <StyledFormControl> <Select value={cu ...

Safari making menu mysteriously disappear

Currently experiencing an issue on this website: specifically in Safari. When I click on "KUNDEN" in the navigation for the first time, the entire header disappears. I checked the dev console and noticed that the header content is loaded, even in the sourc ...

What is the most efficient way to export data from Excel to a paginated gridview on Yii2?

Good evening to all. I am currently utilizing the Yii2 Framework and facing a challenge with exporting filters to csv using the GridView component from yii2\kartik or the ExportMenu also from yii2\kartik. The issue lies in the fact that my data ...

Obtaining serverTime from a webpageWould you like to learn

Is it possible to retrieve the serverTime using jquery ajax functions like $.get() or $.post()? I am looking to store this serverTime in a variable that can be utilized later on in javascript. You can access the webpage at: To use the get and post functi ...

Discovering the quantity of image matches for a specific search term with VBA

I have been experimenting with HTML within Excel, attempting to estimate the prevalence of images at different resolutions. My goal is to create a dynamic system where users input a search term and the code cycles through predetermined image resolutions, r ...

Conceal the input field prior to making a selection from the dropdown menu

I have implemented a drop-down menu for selecting dependencies, similar to how it functions in JSFiddle. $('#user_time_zone').on('change', function() { dateCalender = $(this).val(); if (dateCalender == 'Single Date') { ...

Show button image beyond the limits of the hyperlink

Is it possible to create a button with a background image that changes on hover and when active, while also having the active state display an image that extends beyond the anchor's bounds? Check out my sprite here: The top half of the sprite represe ...

Is it possible to automatically move text to the next line once it reaches a specific character limit using HTML/CSS/PHP?

Is there a way to handle text when it reaches the ceiling or exceeds a certain number of characters so that it goes to the next line instead of showing a scrollbar? I have a div element where I display text retrieved from a MySQL database, and sometimes th ...

"Discrepancy in column display across various resolutions in Bootstrap 5.3 causing issues

My goal is to have 4 divs that change in width at different resolutions, but for some reason it's not working. I am looking for: When the resolution is ≥992px, the divs should be in the same line (3 columns) When the resolution is ≥768px, the di ...

BlockUI - Uncaught error: The object does not contain the method 'blockUI'

Trying to implement the blockUI jQuery Plugin, I have added the necessary libraries in this way; <script src="http://malsup.com/jquery/block/jquery.blockUI.1.33.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery ...

What is the process of implementing session storage in an AngularJS directive?

I am trying to save values in Angular session storage within an Angular directive, but I am facing an issue where I only receive NULL. Can anyone provide assistance? app.directive('myDirective', function (httpPostFactory) { return { restrict ...

Is there a way to ensure that an image stays pinned to the bottom of the screen, regardless of the device or screen

Looking for a solution to keep an image centered at the bottom of a website page regardless of screen size? I've tried different recommendations from Stack Overflow without success. position:fixed; bottom:0px; left:50%; Still struggling to make it wo ...

The sum is being treated as a concatenation instead of an addition in this case

Why is the somma value showing the concatenation of totaleEnergetico and totaleStrutturale instead of a sum? RiepilogoCombinatoStComponent.ts export class RiepilogoCombinatoStComponent implements OnInit { constructor() { } interventi: AssociazioneI ...

How can images from a dynamic table be converted to base64 format in Vue.js and then sent in a JSON file?

Hello everyone, I'm facing an issue with a dynamic table where I need to add multiple rows, each containing a different image. I attempted to convert these images to base64 format and save them in a JSON file along with the table data. However, the pr ...

External JavaScript files are also subject to the same origin policy

Suppose a website http://www.mysite.com contains an external JavaScript file added like this: <script src="http://www.yoursite.com/new.js"></script> Within the http://www.yoursite.com/new.js JavaScript file, there is an AJAX call to a script ...

Determine various percentages through a single function

I am presented with a table of values displaying percentages for different elements in asteroids: astroid a nickel: 20% water: 25% cobalt: 55% astroid b nickel: 30% water: 35% cobalt: 45% astroid c nickel: 240% water: 45% cobalt: 65% To determi ...