What caused the D3 link labels to suddenly cease functioning?

I've come across an issue with my code that previously displayed link names from json data on node links.

// Append text to Link lines
var linkText = vis.selectAll(".gLink")
.data(json.links)
.append("text")
.attr("class", "link")
.attr("x", function (d) {
    if (d.target.x > d.source.x) { return (d.source.x + (d.target.x - d.source.x) / 2); }
    else { return (d.target.x + (d.source.x - d.target.x) / 2); }
})
.attr("y", function (d) {
    if (d.target.y > d.source.y) { return (d.source.y + (d.target.y - d.source.y) / 2); }
    else { return (d.target.y + (d.source.y - d.target.y) / 2); }
})
.style("font", "normal 12px Arial")
.attr("dy", ".35em")
.text(function (d) { return d.linkName });

The initial json data is structured like this:

var json = {
"nodes": [
{ "name": "Fabby MONDESIR", "dob": "5.24.97", "ImageUrl": "http://172.18.215.101/MugImageAsp/MUGImageASP.ASP?WCI=RetrieveImage&WCE=FD635099", "ChartComments": "In Jail" },
{ "name": "ADNES D BRONSON", "dob": "5.24.97", "ImageUrl": "http://172.18.215.101/MugImageAsp/MUGImageASP.ASP?WCI=RetrieveImage&WCE=FD635199", "ChartComments": "Armed" }
],
"links": [
   { "source": 0, "target": 1, "linkName": "FCC", "value": 8 },
   { "source": 0, "target": 2, "linkName": "Arr", "value": 10 }
   ]
}

Now, I am attempting to fetch data dynamically from SQL Server using a stored procedure and utilizing the primary key in the database as the source and target in the links. The modified json structure looks like this:

var json = {
"nodes": [
{ "sId": "1", "name": "Fabby MONDESIR", "dob": "5.24.97", "ImageUrl": "http://172.18.215.101/MugImageAsp/MUGImageASP.ASP?WCI=RetrieveImage&WCE=FD635099", "chartComments": "Chart Comments"},
{ "sId": "2", "name": "ADNES D BRONSON", "dob": "5.24.97", "ImageUrl": "http://172.18.215.101/MugImageAsp/MUGImageASP.ASP?WCI=RetrieveImage&WCE=FD635098", "chartComments": "Chart Comments" }
],
   "links": [
   { "source": "1", "target": "2", "linkName": "FCC" },
   { "source": "1", "target": "3", "linkName": "Arr" }
   ]
 }

In order for the modifications to take effect, I had to incorporate the following piece of code:

var nodeMap = {};
   json.nodes.forEach(function(x) { nodeMap[x.sId] = x; });
   json.links = json.links.map(function(x) {
       return {
           source: nodeMap[x.source],
           target: nodeMap[x.target]
          };
   });

Although the linking now functions correctly, the link text no longer appears on the lines. Despite maintaining correct associations and line drawing with the json data, the reason behind the disappearance of link text after implementation of nodeMap remains unclear. Reverting back to the original json structure resolves the issue. Any insights would be greatly appreciated!

Thank you, Perry

Answer №1

The content is expected to be fetched from the linkName, however, it seems that this is not taken into account while parsing the json.links:

At present:

 json.links = json.links.map(function(x) {
       return {
           source: nodeMap[x.source],
           target: nodeMap[x.target]
          // remember to include linkName here//
          };
    });

Answer №2

In order to fix the issue, I also had to include mapping for the linkName along with the source and target. The updated code that is now functional is as follows:

var nodeMap = {};
json.nodes.forEach(function (x) { nodeMap[x.SubjectID] = x; });
json.links = json.links.map(function (x) {
    return {
        source: nodeMap[x.source],
        target: nodeMap[x.target],
        linkName: x.linkName

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

The proper DateTime format to use with DataContractJsonSerializerSettings

Using version 4.5 As per the requirements of a previously built project, I am using Newtonsoft.Json on one side and DataContractJsonSerializer on the other side of the server. I have already attempted two different configurations found here to parse the D ...

Vue FullCalendar and triggering a click event

I'm currently utilizing the Vue FullCalendar library for my project and I have successfully implemented a calendar. However, I am facing an issue with getting the date/calendar events to trigger on click. My calendar is set up in the template like th ...

Triggering the body onunload event

I am currently developing a HTA that needs to make final modifications on the onunload event. However, I am facing an issue as the event does not appear to be triggered. Can someone confirm if this event is still supported? Is there an equivalent event in ...

Looping through a JavaScript array of objects

Question: I am dealing with a JavaScript array of objects. Here's how it looks: $scope.todos = [ { face : imagePath, what: 'Das', who: 'Sophia', when: '3:08PM', notes: " Descr ...

Saving a Coordinated Universal Time and showcasing it in the corresponding local timezone

In my upcoming MVC4 application that will have a global audience, one of the features involves recording the date and time when a transaction is added or modified. Since I am storing the transaction datetime in UTC format, what would be the most effective ...

Associate the URL with the object to retrieve the corresponding object

https://i.sstatic.net/cj5N4.png When iterating through this array, I currently loop through it in the following manner: {props.choosenMovie.characters.map((characters) => ( <p>{characters}</p> /* This displays the URL of course */ ))} ...

Preserve the visibility of a text input form while the checkbox is selected

Within my HTML code, there is a form that includes a checkbox labeled "other." When this checkbox is selected, a textbox will appear. If the user types in text and submits the form, the textbox disappears, but the checkbox remains checked (as saved in loca ...

Executing a JavaScript function when a column in a Fusion Chart is clicked

I have two div elements in HTML and I would like to have div2 load when div1 is clicked. Additionally, whenever div2 loads, a back button should also appear to return to div1. Is there a way to achieve this using HTML? <td background="images/bgTd.gif ...

Is it possible to retrieve the timestamp for each call in a callstack?

I'm interested in utilizing the callstack of a function call to create an accurate timeline when an exception is thrown. While I know that I can retrieve the name, caller name, line number, and file for each call from the callstack, I am wondering if ...

Pass information between two forms using the Post method and Ajax communication

For my project, I attempted to retrieve data using the POST method in the processing file but encountered difficulties. I need to be able to receive the data through the POST method and display it on process.php. It's worth noting that my inputs are n ...

Tips for maximizing the efficiency of a callback when utilizing the filter function in Primefaces for 'myDataTable'

Currently using Primefaces 5.1, and I've encountered a situation where I want to hide a table until after the filter is applied in Javascript. My initial thought was to simply set the css of the table to visibility:hidden;, followed by running the fol ...

Using jQuery to replace the value of a button with a variable string that has been concatenated

I am facing a dilemma with a button on my webpage. The value displayed on the button is a combination of variables and strings, like so: $('#btn1').attr('value','question'+currid+'ans1').button('refresh'); ...

How can I save a PDF file using React?

My webpages contain several tables, and I am looking to add a dropdown feature that allows users to choose between saving the table in either PDF or Excel format. These tables are dynamically generated with the header structured in array form and data sto ...

Error while retrieving reference from mongoDB in NodeJS

I am currently working on a small website that needs to query my local mongodb. Everything works perfectly fine on localhost. That's why I decided to begin with NodeJS. While all JavaScript functions work seamlessly when run separately, I encounter a ...

What could be causing my form to malfunction when sending? Comparing [(ngModel)] to form submission in Angular 2+

I recently made changes to the form implementation on my login screen. Instead of binding to the inputs [(ngModel)] in the backend, I now pass the form itself. Although this change allowed the original tests to pass, it caused issues with the actual app f ...

Easy Registration Page using HTML, CSS, and JavaScript

In the process of creating a basic login form using HTML and CSS, I'm incorporating Javascript to handle empty field validations. To view my current progress, you can find my code on jsfiddle My goal is to implement validation for empty text fields ...

What approach would you take to create a navigation system similar to this?

Could you provide some advice on creating a navigation with similar features? I'm encountering some obstacles and would appreciate any suggestions on how to tackle this challenge. Attached is a transparent PNG showcasing one of the hover states. ...

Trying out a template component with a dynamic function for disabling

Here's an interesting scenario I encountered: A template disables a certain element based on the input of another element. <input id="email" type="text" [(ngModel)]="login.username" name="username" <button type="button" id="login-button" [disab ...

What can I do to prevent cookies from being deleted when I refresh the page or change the window location?

Struggling to maintain session in the frontend, I discovered that my cookie keeps disappearing whenever I refresh the browser or change the window.location. angular .module('myservices') .factory('myAuthentication&apos ...

Adjusting the Page Number Displayed in the Pagination Bar of DataTables

I'm in the process of customizing the pagination bar using DataTables. I've successfully added images for the Previous/Next buttons, but I want to change the appearance of the page numbers. https://i.sstatic.net/WsLf5.jpg Here is what I have ac ...