The issue regarding the hierarchical layout in vis.js Network stands out as a

I have come across a simple hierarchical vis.js network example, but it seems to be displaying in a single vertical line instead of from left to right.

Despite trying various options, I haven't been able to get it to show up correctly. Can you point out what might be going wrong?

// Here is an array of nodes
var nodes = new vis.DataSet(
  [
    { id: 1,  value: 21, label: 'GR' },
    { id: 12, value: 10, label: 'PL' },
    { id: 18, value: 28, label: 'MM' },
    { id: 22, value: 12, label: 'I_STORE_LOCATOR' },
    { id: 23, value: 18, label: 'SL' },
        { id: 29, value: 3,  label: 'I_PRICEMATCH' },
        { id: 30, value: 6,  label: 'PM' }
  ]
);

// Creating an array with edges
var edges = new vis.DataSet(
  [
   {id: 13, eLabel: 100, arrows: "to", from: 1, to: 12, },
     {id: 26, eLabel: 100, arrows: "to", from: 1, to: 18, },
     {id: 17, eLabel: 100, arrows: "to", from: 12, to: 1, },
     {id: 32, eLabel: 60, arrows: "to", from: 18, to: 22, },
     {id: 47, eLabel: 60, arrows: "to", from: 18, to: 23, },
     {id: 53, eLabel: 30, arrows: "to", from: 18, to: 29, },
     {id: 62, eLabel: 30, arrows: "to", from: 18, to: 30, },
   {id: 34, eLabel: 120, arrows: "to", from: 22, to: 23, },
     {id: 40, eLabel: 60, arrows: "to", from: 23, to: 22, },
     {id: 43, eLabel: 120, arrows: "to", from: 23, to: 18, },
     {id: 55, eLabel: 30, arrows: "to",  from: 29, to: 30, },
     {id: 58, eLabel: 60, arrows: "to", from: 30, to: 18, }
  ]
);

// Setting up the network
var container = document.getElementById("mynetwork");
var treeData = {
  nodes: nodes,
  edges: edges,
};

let options = {
    "layout": {
    "randomSeed": 2345,
    "improvedLayout":true,
    "clusterThreshold": 150,
    "hierarchical": {
      "enabled":true,
      "levelSeparation": 150,
      "nodeSpacing": 100,
      "treeSpacing": 200,
      "blockShifting": true,
      "edgeMinimization": true,
      "parentCentralization": true,
      "direction": 'LR',        // UD, DU, LR, RL
      "sortMethod": 'directed',  // hubsize, directed
      "shakeTowards": 'leaves'  // roots, leaves
    }
  }
};

var network = new vis.Network(container, treeData, options);

network.on('click', function (properties) {
  var ids = properties.nodes;

  // Loop through the items in the array
  for (let i = 0; i < ids.length; i++){
    treeData.nodes.update({id:ids[i],hidden:true})
  }      
});
#mynetwork {
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis-network/9.1.2/standalone/umd/vis-network.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis-network/9.1.2/dist/dist/vis-network.min.css" rel="stylesheet"/>
<div id="mynetwork"></div>

Answer №1

The network is facing challenges in determining the correct hierarchy positioning due to cycles present within it, where edges go in both directions. For more insights on this issue, refer to the official example at .

It's essential to recognize that trying to establish a "hierarchy" with cyclic dependencies leads to ambiguity (If node 1 is above node 2 and vice versa, which one takes precedence?)

To achieve an accurate display of the hierarchy structure, it's necessary to eliminate the looping edges that create cycles (specifically edges 17, 40, 43, and 58) as shown in the code snippet below. If these circular connections are crucial, utilizing hierarchical layout in vis-network might not be feasible.

// creating nodes array
var nodes = new vis.DataSet(
  [
    { id: 1,  value: 21, label: 'GR' },
    { id: 12, value: 10, label: 'PL' },
    { id: 18, value: 28, label: 'MM' },
    { id: 22, value: 12, label: 'I_STORE_LOCATOR' },
    { id: 23, value: 18, label: 'SL' },
    { id: 29, value: 3,  label: 'I_PRICEMATCH' },
    { id: 30, value: 6,  label: 'PM' }
  ]
);

// creating edges array
var edges = new vis.DataSet(
  [
   {id: 13, eLabel: 100, arrows: "to", from: 1, to: 12, },
     {id: 26, eLabel: 100, arrows: "to", from: 1, to: 18, },
     //{id: 17, eLabel: 100, arrows: "to", from: 12, to: 1, },
     {id: 32, eLabel: 60, arrows: "to", from: 18, to: 22, },
     {id: 47, eLabel: 60, arrows: "to", from: 18, to: 23, },
     {id: 53, eLabel: 30, arrows: "to", from: 18, to: 29, },
     {id: 62, eLabel: 30, arrows: "to", from: 18, to: 30, },
   {id: 34, eLabel: 120, arrows: "to", from: 22, to: 23, },
     //{id: 40, eLabel: 60, arrows: "to", from: 23, to: 22, },
     //{id: 43, eLabel: 120, arrows: "to", from: 23, to: 18, },
     {id: 55, eLabel: 30, arrows: "to",  from: 29, to: 30, },
     //{id: 58, eLabel: 60, arrows: "to", from: 30, to: 18, }
  ]
);

// creating the network
var container = document.getElementById("mynetwork");
var treeData = {
  nodes: nodes,
  edges: edges,
};

let options = {
    "layout": {
    "randomSeed": 2345,
    "improvedLayout":true,
    "clusterThreshold": 150,
    "hierarchical": {
      "enabled":true,
      "levelSeparation": 150,
      "nodeSpacing": 100,
      "treeSpacing": 200,
      "blockShifting": true,
      "edgeMinimization": true,
      "parentCentralization": true,
      "direction": 'LR',        // UD, DU, LR, RL
      "sortMethod": 'directed',  // hubsize, directed
      "shakeTowards": 'leaves'  // roots, leaves
    }
  }
};

var network = new vis.Network(container, treeData, options);

network.on('click', function (properties) {
  var ids = properties.nodes;

  // loop through the items in ids array
  for (let i = 0; i < ids.length; i++){
    treeData.nodes.update({id:ids[i],hidden:true})
  }      
});
#mynetwork {
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0px;
  bottom: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis-network/9.1.2/standalone/umd/vis-network.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis-network/9.1.2/dist/dist/vis-network.min.css" rel="stylesheet"/>
<div id="mynetwork"></div>

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

Is there a JavaScript function available that can enable the placeholder attribute to function properly in Internet Explorer?

I currently have numerous input tags in my project that utilize a placeholder attribute, such as the following: <input id="Name" name="Name" placeholder="Name Goes Here" type="text" value=""> Is there a JavaScript function that I can include in my ...

Slider-activated Pie Chart Controller

I have successfully created a pie chart using highchart and javascript, allowing me to adjust each slice individually using a slider. However, I am facing an issue where I want the maximum value of the pie to be 100%, with the other sliders contributing to ...

The Async/Await feature does not truly wait within a while loop

As I neared the completion of my project, I realized that the final component would require the use of Async, Await, and Promise to ensure that the program waits for an API call to finish before proceeding. Despite my understanding that there is no true "s ...

Do I need to include a callback in my AWS Lambda handler function?

What is the function of the callback in the lambda handler? It appears to be utilizing the sns variable and I am looking to make some modifications to the variables. exports.handler = function(event, context, callback) { console.log("AWS lambda and ...

I'm having trouble with accessing an object by index in a knockout observablearray. It seems like the binding process is encountering difficulties

I'm completely new to stackoverflow and knockout, and I have a query. I want to access a JSON object by index. It works fine when I insert dummy data in my code, but it throws an error when I try to add data from JSON. Error Uncaught TypeError: Unab ...

Display previous messages in React JS chat when scrolling upwards

https://i.sstatic.net/mcJUp.png I am currently working on a chat application, as depicted in the image. Once the chat is initiated, it automatically scrolls down to display the most recent messages. My goal is to implement a feature where when a user sc ...

What are the steps to crafting a personalized message for the valid() method in the JOI library?

To validate the property subscription, I use the following method: Joi.object({ subscription: Joi.string() .valid('starter', 'pro', 'business') .required() .messages({ 'string.base': `{{#label}} s ...

Delete JavaScript data array

I've been working on a project involving data manipulation in a JSON file. Although I can successfully add new names to the file, I'm facing an issue when trying to delete them. Instead of deleting, the inputted name gets added to the file again ...

Saving information using Socket.io server during the 'connect' event

Currently, I am working with socket.io along with MongoDB and promises. My goal is to save some data in Mongo when the session is established so that further socket events cannot be properly handled until this data is successfully saved. // Below is my si ...

Is it possible to utilize JavaScript for transmitting and storing data on a server?

Consider this scenario: When you submit a query on stackoverflow, the data you provide is entered into a text field. This information is then transmitted to the server for storage and eventual display to the user. Is it possible to code the functionality ...

Assign the path parameter to the component key in the Route

One of the routes in my application is defined as shown below: <Route exact path="/licenses/:type?" component={Licenses} /> I want the component to re-render every time the type parameter changes. According to react-router documentation, I ...

Accessing MEANJS Mongoose model data from the request object

I've recently started using meanjs. While looking at the server-side module user profile controller, I noticed that the mongoose model User is available in the req object. How was it added to the req object? Take a look at the code snippet below. I& ...

CodePen experiencing issues with JS/jQuery coding functionality

Experimenting on CodePen, I am practicing using JS and jQuery with HTML. HTML: <button>asdasads</button> JS: $('button').on('click', function() { alert('woot'); }); Visit this pen, unfortunately it's ...

Guide on emphasizing a div when the page's validity is not true

Is there a way to highlight a specific div when the page.isvalid=false? I can display the error message on page load, but struggling to highlight the required control. I have a JavaScript function that applies an error class to the div, which works fine w ...

Tips for changing images when hovering over them

Currently engaged in the development of an e-commerce application, where I am looking to accomplish a specific task... https://i.sstatic.net/sYoG8.png The objective is to display the corresponding large image on the big box when hovering over smaller ima ...

What is the best way to display items within a table using React?

I'm just starting to learn React. Can someone show me how to use the "map" function to list elements from two different arrays in two columns? state = { dates: ["2000", "2001", "2002"], cases: ["1", "2", "3"] } render() { return ( <thea ...

What tips do you have for creating a tool that requires automatic updates using ASP.NET MVC Razor, including Views, Controllers, Models, and Ajax?

Hello everyone! I need some advice from those familiar with asp.NET MVC and Ajax. Currently, I am in the process of creating a wishlist application and I want to enhance it by incorporating Ajax functionality to make it more interactive. My idea is that u ...

Struggling to make marker clicks on Google Maps trigger any action

I'm struggling with implementing a simple Google Maps feature. Currently, I have managed to display the map and iterate through a list of projects using PHP (specifically ExpressionEngine). Each project's latitude and longitude are added to a Ja ...

Divide and conquer - meteorjs

I am currently utilizing the most recent version of Meteor. Meteor is designed to keep everything within the same directory structure, like this: MeteorProject -- .meteor -- client -- imports -- server -- test -- node_modules -- packa ...

Exploring the integration of query parameters in Postman using mongoose

In my code, I have a driver.js file that holds a driver schema, and a driverController.js file with REST methods including GET, POST, DELETE, and PUT. What I want to achieve is to send a GET request to http://localhost:3000/drivers?available=true This re ...