Issue with nodes not holding position properly in d3 Force Directed Graph fixed

My approach involves setting the nodes as fixed:

let link = svg.append("g")
  .attr("class", "links")
  .selectAll("line")
  .data(graph.links)
  .enter().append("line")
  .attr("stroke-width",  () => 4)

let node = svg.append('g')
  .attr("class", "nodes")
  .selectAll(".node")
  .data(graph.nodes)
  .enter().append("g")
  .attr("class", "node")
  .call(
    d3.drag()
    .on("start", Node.dragstarted)
    .on("drag", Node.dragged)
    .on("end", Node.dragended))

node.append("title")
  .text(d => d.country)

node.append('image')
    .attr('xlink:href', d => 'https://rawgit.com/hjnilsson/country-flags/master/svg/' + d.code + '.svg')
    .attr('height', d => 2.5 * (area[d.code]||5))
    .attr('width', d => 4 * (area[d.code])||5)
simulation
  .nodes(graph.nodes.map(c => {
    if(latlong[c.code] === undefined) {
      console.log(c,'missing lat/long data')
      return c
    }
    c.x = (+latlong[c.code][0])
    c.y = (+latlong[c.code][1])
    c.fixed = true
    return c
  }))
  .on("tick", ticked)

While my code effectively displays images in different locations, there seems to be an issue with the fixed property not functioning as intended.

You can view the code here: codepen

If anyone has insights on how to configure the canvas so that elements do not escape from the top or bottom, I'd appreciate any guidance on that matter as well.

Answer №1

One thing to note is that in d3.js v4, the fixed property no longer exists. Instead, you must set the nodes' fx and fy attributes. It's worth mentioning that your drag functions already handle this.

.nodes(graph.nodes.map(c => {
if(latlong[c.code] === undefined) {
  console.log(c,'missing lat/long data')
  return c
}
c.fx = c.x = (+latlong[c.code][0])
c.fy = c.y = (+latlong[c.code][1])
return c
}))

Check out this link for more information.

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

Incorporate the Google Maps API into a React application

I've recently started learning react and I'm currently trying to integrate the Google Maps API into my React application. Within my app, I have a search input field and a designated area for the map located at http://localhost:8080/. My main qu ...

Steps for integrating Stanford's NLP into a web application

I have successfully developed a project utilizing Stanford's NLP API and models. Now, I am looking to integrate this Java-based project onto the web. After seeing the demo provided by Stanford-NLP, I am curious about the process they use to achieve th ...

Using jQuery to send single value arrays through AJAX requests

I've come across numerous discussions regarding how to send arrays via ajax in jquery. However, my query is slightly different. I am unsure if what I am seeing is normal behavior or if there is an error in my approach. Here is a simple jquery snippet ...

Preserve user-inputted text from jQuery within a div container

With the help of the knowledgeable individuals here, I successfully created a prototype to address an issue I had encountered. The problem involved using a textbox input with a password requirement to update an HTML element. Although everything is functio ...

What led to the decision for the two distinct chart elements to merge into a single container?

In the process of creating a dashboard using React.js and d3.js, I encountered an interesting issue that perplexed me for quite some time. Below is the Scatterplot.js code written in d3.js: import React, { Component } from "react" import * as d3 from "d3 ...

Cross domain request in a simple HTML document

I'm currently working on an app that is strictly in plain HTML files without a server. I'm facing difficulties with cross domain requests from JavaScript. Whenever I try to make a request, the browser displays this error message: XMLHttpRequest ...

It never fails to function automatically, no matter which script is being executed

By default, the script will always be executed regardless of its environment. Check out my code snippet: import { Pool } from 'pg'; import config from './../config'; const connectionString = () => { switch (process.env.NODE_EN ...

How can I dynamically import a Vue instance?

Is there a way to dynamically import an instance in Vue while using a parameter? I am interested in dynamic importing the language into flatpickr-vue. import { de } from 'flatpickr/dist/l10n/de.js'; How can I make the "locale" parameter part of ...

Not all comparisons between two tables are guaranteed to be successful

Looking to compare records within two arrays and apply a style if they are equal. Here is my approach: var json = ["VIP Section","Master Section","Press Section","God Section"]; var sections = ["VIP Section","Master Section"]; if ( sections.length &g ...

At initial glance on iOS, the login page remains hidden, but as I tilt the phone, the page fields slowly start to appear

Encountering a strange issue when accessing my application on iOS 11. Initially, the page fails to load completely with missing background and field labels. However, I am able to click on objects. Oddly enough, if I tilt my iPhone or zoom in, the page load ...

What is the method to convert a JSON object into a JavaScript array?

Looking to convert a JSON object from {"0":"sometext","1":"someothertext"} into a JavaScript array where the index corresponds to the integer and the data at that index is the string. Attempting to use JSON.parse() did not yield the desired result, as sh ...

Traverse through an array or object using recursive render functions in JavaScript

Here is an example of the issue I am currently trying to solve: https://codepen.io/wombsplitter/pen/KyWKod This is the structure of my array: [{ //obj 1 link: [{ //obj 2 link: [{ //obj 3 }] }] }] The ...

Node.js can be used to easily set the values of HTML elements

After successfully setting up a node.js connection to my mysql database and being able to retrieve and input data using connection.query(), I now want to display the database information on my HTML elements. Is there an equivalent of getElementById for t ...

unable to access MySQL table data accurately using Node.js

Below is the specific query to be entered into the browser: /line?l1=##&l2=##&l3=## This is how I have incorporated it using JS: app.get('/line', (req, res) => { let sql = `UPDATE car_info SET l1 = ${parseInt(req.query.lineone)} ...

Why is the background image not loading properly on first hover with CSS?

Whenever we load a page with a large image set as the background for a div, there seems to be a delay in displaying the image when we hover over the div. Is there a solution to this issue? I would prefer not to use a sprite image because I need to make alt ...

Having trouble showing JSON data in a for loop

$.getJSON("https://example.com//json/", function (data) { var count = Object.keys(data).length; for (var i = 0; i < count; i++) { var lat = data[i].LATITUDE; var lng = data[i].LONGITUDE; ...

Function is unmodifiable

How do I modify the function length in a different JavaScript file? jQuery(function ($) { $.fn.custom_text = function (text) { $.fn.custom_text.length = function(){ // Code here } } ...

Eliminate item from array based on certain criteria

In certain scenarios, I must update the filters. Under specific data conditions, I am required to eliminate 1 item from an array. Here is the code snippet: useEffect(() => { let canceled = false; const filters = [ new C ...

Create a way to allow users to download html2canvas with a customized filename

I have a div where I want to use html2canvas to save a PNG file of its content. The issue is that the name of the saved file is static in my code and does not change unless I manually edit it. Is there a way to dynamically set the filename based on user i ...

What's the issue with ng-click not functioning properly?

My goal is to create a directive called "myDisabled" in AngularJS version 1.1.5 since the ng-disabled functionality is not available in this version. Here is the code for the directive: tableApp.directive('myDisabled', function($compile) { retur ...