Include a unique identifier in the SVG nodes generated by Graphviz for enhanced manipulation using JavaScript

I am currently utilizing pydotplus for generating graphs and storing them in SVG using graphviz:
For example, here's how I add a node (without specifying a node ID attribute):

    g.add_node(pydotplus.Node(fn_name, 
                              style="filled", 
                              fillcolor='cornflowerblue', 
                              shape='Mrecord', 
                              fontname="Consolas", 
                              fontsize=8.0))

The resulting SVG file can be found here:
https://github.com/tarun27sh/gdb_graphs/edit/master/gallery/test.svg

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
 -->
<!-- Title: Created by: Tarun Sharma (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb8f9a898e95c9cc8893bb9c969a9297d5989496">[email protected]</a>) Pages: 1 -->
...

...// Truncated for brevity

...

The generated SVG nodes are created with id=nodex, where x is an incrementing number:

<g id="node1" class="node"><title>main</title>

Is there a way to relate the id of a graph node to its title? JavaScript does not allow me to read the title of an SVG node directly.

To incorporate Javascript, I'm including <script> tags within the <svg> tags:

<svg> .....
  <script type="text/javascript">
        window.addEventListener('load',function(){
            alert('Hi')
        })
        function nodeClick() {
            alert('You have clicked a node');
            var node1 = document.getElementById('node1');
            alert(node1.id);
            alert(node1.title);     <= error
        }
    </script> 

Edit:

Based on the suggested answer, I can now pass an id attribute while creating a node or edge, and utilize them in JavaScript (pydotplus allows all attributes that graphviz supports):

g.add_node(pydotplus.Node(elem,
                          id=elem,
                          style="filled",
                          fillcolor='cornflowerblue',
                          shape='box',
                          fontname="Consolas",
                          fontsize=12.0))

g.add_edge(pydotplus.Edge(edge_tuple,id="{}__{}".format(
                                                  edge_tuple[0],
                                                  edge_tuple[1])))

Answer №1

While I may be unfamiliar with pydotplus, I do know that within the Graphviz language, there exists an id attribute for nodes, edges, clusters, and graphs.

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

Learn how to choose multiple rows in a table using a combination of ctrl+click and Shift+click commands within react js

I'm struggling with a problem and need some assistance. I'm trying to implement a feature where a user can highlight a table row by pressing ctrl + left click, and when they press shift + left click, it should highlight all the row data from one ...

Looking to toggle visibility on multiple divs with an onclick button in Jquery?

Lately, I've been struggling to make my code display questions in a specific order. My goal is to have the next question show up once the submit button is pressed, while hiding the previous one. Currently, I'm developing a game similar to Family ...

Assistance in tackling AJAX and HTML issues

My HTML code is structured in a way that includes a menu with various options. In JavaScript, I iterate through each item and assign a click event listener to them. However, I am facing an issue with determining what data needs to be fetched via AJAX whe ...

Toggle the visibility of divs using jQuery for easy filtering

I am looking to enhance my filtering system by allowing users to apply and view multiple filters simultaneously. For example, I want to be able to select 'High' and 'pizza' filters and see the results that match both criteria. Currently ...

Passing arrow functions for input validation rules to the Stancil component in Vue

I am attempting to implement an arrow function as input rules, similar to the setup in Vuetify at https://vuetifyjs.com/en/api/v-input/#rules. My approach involves passing rules within an array using the following code: <body> <div id="ap ...

ngClass binding fails to update when using directives to communicate

I am looking to combine two directives in a nested structure. The 'inner directive' includes an ng-class attribute that is bound to a function taking parameters from both inner and outer scopes, and returning a Boolean value. This is the HTML co ...

Using jQuery to establish a canvas element and define its dimensions by adjusting its width and height attributes

My attempt at using jQuery to create a canvas element was not quite as expected. Here is the code I tried: var newCanvas = $('<canvas/>',{'width':100,'height':200,'class':'radHuh'}); $(body).append(n ...

Troubleshooting a Vue.js formatting problem in Visual Studio 2019

Encountering an issue with VS2019 while attempting to format this code section. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="milestone.ascx.cs" Inherits="uc.dms.milestone" %> <section class="content-header"> <h1> ...

Tug and release technique

If you visit this website, you will find a detailed explanation of a drag-and-drop algorithm. Focusing on the initial code snippet provided in the article, it emphasizes the importance of using document in document.addEventListener('mousemove', ...

Reduce the noise from different versions by utilizing package-lock.json

There may not be a clear-cut answer, but I'm curious to hear how others handle the issue of package-lock.json causing problems when committing to their node repository. Many opinions lean towards committing package-lock.json - ensuring consistent dep ...

How can you prevent videos from constantly reloading when the same source is used in multiple components or pages?

How can we enhance loading performance in Javascript, React, or Next JS when utilizing the same video resource across various components on different pages of the website to prevent unnecessary reloading? Is there a method to store loaded videos in memor ...

Adding an item to a JSON array with a variable as the key in a multi-dimensional structure

A JSON file contains an array like this: { "general_array":[ {"key_1":["a","b","c"]} ] } My goal is to add a new element to the array, for example: {"key_2":["d","e","f"]} The value of the new key comes from a variable: var newKey = &a ...

Angular 1.5 component using HTTP GET

Trying to utilize a 1.5 component with AngularJS has presented some challenges for me. I have a service that fetches my JSON file using $HTTP and returns a promise. In the controller of my component, I resolve the promise and assign it to a value using thi ...

Why does this asynchronous function initially return nothing, only to suddenly return all results on subsequent tries?

My current task involves inserting data into my database within a map loop. To ensure that the loop completes before proceeding, I am utilizing an async function to store all results in the variable "practicasAgregadas." This is how I invoke the function: ...

What is the best way for a server to set up middleware chaining?

Currently, I am facing challenges in grasping the concept behind the ExpressJS module, particularly focusing on the execution of middleware chains. My main goal is to comprehend how a server can listen for incoming requests, and once received, pass the re ...

Go to the child router using the specified outlet

I am attempting to display the expanded item within a grid of components that are being rendered inside a named outlet. In order to achieve this, I have added children to my named outlet path. Currently using Angular 7, I have followed various guides from ...

What steps should I take to set up jQuery in such a way that external scripts (src="...") are maintained as external when adding markup with .html()?

Through my experimentation, I have noticed that when I make use of jQuery's .html('...') function to insert markup that includes an external <script src="..."></script> tag, jQuery does not directly insert the script ta ...

The selected value from a dropdown list may occasionally come back as text

I am facing an issue with a dropdown list on my form that has Integer Values set to display text. The problem arises when I run the code to show the value and associated text, as the text is being displayed as the value itself. Is there any workaround avai ...

Set a NodeJS variable equal to a specific value based on an array within a MongoDB document

Within my MongoDB collection, I currently have the following documents: > db.mycol.find() { "_id" : ObjectId("5ec6506171ae442136aa97d2"), "uname" : "mail1", "port" : 1000, "abc" : "test1" } { "_id" : ObjectId("5ec659e6c0b1cc11370d8378"), "uname" : "mai ...

Ways to group and consolidate data based on a shared property?

I am working with a dataset of people, each person having a group attribute assigned to them. The group value is a random string that I do not know in advance when making queries. It is possible for multiple people to have the same group value. My goal is ...