Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getPositions returns wrong coordinates #155

Closed
clem-41 opened this issue Oct 10, 2019 · 10 comments
Closed

getPositions returns wrong coordinates #155

clem-41 opened this issue Oct 10, 2019 · 10 comments

Comments

@clem-41
Copy link

clem-41 commented Oct 10, 2019

I want to make a popup appear at the position of a node when I click on the node.

I used the getPositions() method but the popup appears in the wrong place (too much on the left and top corner), as if the coordinates were incorrect (and it seems like the positions returned are not the same scale as the network since the offset is different for every node).

Here is the function I used:

network.on("click", function (params) {
	// Sélection du node cliqué
	var nodeId = params.nodes[0];
	if (nodeId) {
		// On récupère le title du node
		var popup = this.body.nodes[nodeId].options.title;

		// On prend les coordonnées du node
		var nodePosition = network.getPositions(nodeId);
		var nodeX = nodePosition[nodeId].x;
		var nodeY = nodePosition[nodeId].y;
			
		// On affiche le contenu du title dans une div de la page auquel on ajoute une icone qui permet de fermer le popup au clic
		document.getElementById('popup').innerHTML = popup+"<div class='close'><a href='#' onclick='this.parentNode.parentNode.style.display = \"none\"'>X</a></div>";
		// On affiche la div
		document.getElementById('popup').style.display = "block";
		// On place la div
		document.getElementById('popup').style.position = "absolute";
		document.getElementById('popup').style.top = nodeY+'px';
		document.getElementById('popup').style.left = nodeX+'px';
	}
});

I posted on Stack Overflow but no one was able to help me and they suggested it was a bug; is it?
How can I get the correct positions of the nodes?

@Thomaash
Copy link
Member

Thomaash commented Oct 10, 2019 via email

@clem-41
Copy link
Author

clem-41 commented Oct 10, 2019

No I haven't, I'm not sure how to use it?

@Thomaash
Copy link
Member

const domPosition = network.canvasToDOM(nodePositions[nodeId]);

For more details see the method reference on https://visjs.github.io/vis-network/docs/network/#methods.

@Thomaash
Copy link
Member

Hi @clem-41,

is there still in issue or was it solved by converting the coords? If it was solved close this issue please.

@clem-41
Copy link
Author

clem-41 commented Oct 14, 2019

I couldn’t do it so I kind of gave up and got around the problem by doing something different than what I initially planned.
But technically it’s still an issue.

@clem-41
Copy link
Author

clem-41 commented Oct 16, 2019

Turns out my plan B is not a good solution for me. So back to plan A, which is this.
Can anyone help me? I'm not very good at javascript and I did read the documentation (and the issue referenced by UnicornDeluxe) but often I don't know how to apply the methods to my code, so if anyone would be kind enough to maybe give me an example using my code it would be really helpful.

@Thomaash
Copy link
Member

It's really difficult to help if you supply no code.

@Thomaash
Copy link
Member

In case you meant the original code it should look similar to:

network.on("click", function(params) {
  // Sélection du node cliqué
  var nodeId = params.nodes[0];
  if (nodeId) {
    // On récupère le title du node
    var popup = this.body.nodes[nodeId].options.title;

    // On prend les coordonnées du node
    var { x: nodeX, y: nodeY } = network.canvasToDOM(
      network.getPositions([nodeId])[nodeId]
    );

    // On affiche le contenu du title dans une div de la page auquel on ajoute une icone qui permet de fermer le popup au clic
    document.getElementById("popup").innerHTML =
      popup +
      "<div class='close'><a href='#' onclick='this.parentNode.parentNode.style.display = \"none\"'>X</a></div>";
    // On affiche la div
    document.getElementById("popup").style.display = "block";
    // On place la div
    document.getElementById("popup").style.position = "absolute";
    document.getElementById("popup").style.top = nodeY + "px";
    document.getElementById("popup").style.left = nodeX + "px";
  }
});

@clem-41
Copy link
Author

clem-41 commented Oct 16, 2019

Yes I meant the original code!
Thanks for your answer, it does work on a basic network, the only problem is that I use the fit method to center and zoom on the clicked node. And with the code you provided, the popup appears at the inital position of the node, but then the network moves and the popup is no longer in the right place... is there any way I can adapt your code to apply it once the fit method has occured?

Here is my code for the fit method:

	network.on("click", function(e) {
		  // Si on clique ailleurs que sur un node, on recentre sur l'ensemble du network
		  if (e.nodes.length !== 1) {
			network.fit({
				animation: {
				duration: 600,
				easingFunction: "easeOutQuint" }
			});
			return;
		  }
		  var nodeId = e.nodes[0];
		  // ZOOM SUR UN NODE
		  // On centre la vue sur le node avec une animation
			network.fit({
			nodes: [nodeId],
			animation: {
			duration: 600,
			easingFunction: "easeOutQuint" }
		  });

@Thomaash
Copy link
Member

You're welcome.

You have to update the position of the popup whenever the canvas is moved, zoomed etc. You can just store the id of the node and reposition the popup using the same method as for the initial positioning. But this is a StackOverflow question not an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants