How do I get X and Y coordinates of a Vertex Control?

Jan 5, 2010 at 1:32 AM

Hi,

I'm trying to get X and Y coordinates of the Vertex Control inside the graph canvas.  I tried  vertexControl.GetValue(GraphCanvas.XProperty) - vertexControl.GetValue(GraphCanvas.YProperty) but I get Nan values. Any ideas how to resolve this ?

Thanks

Jan 5, 2010 at 3:57 AM

I believe you use GraphCanvas.GetX(vertexControl) and GraphCanvas.GetY(vertexControl).

Jan 5, 2010 at 5:33 PM

Yes, I tried that one as well but did not work, I get Nan values.

Jan 29, 2010 at 1:21 PM

Hello

Same problem here, reading out the Values with GetX() just returns that there are no values assigned.

As a consequence, SetX() ist without any effect because obviously, Graph# does not care about it.

 

Feb 3, 2010 at 2:39 PM

Hi,

 

I have the same problem regarding getting AND setting the X and Y values of a vertex.

Do I need some dependencyproperty or something in my vertex class to do this?

 

Thanks

Feb 8, 2010 at 7:17 AM
Just a beginner, but this thankfully works fine for me after a layout:

GraphCanvas.GetX(vc)

where vc is the VertexControl instance of the vertex I am after.

The property is also set when I move the vertex on the canvas. I have not tried the setter, nor "drag-and-drop to add", yet - that's next.

 

Mar 30, 2010 at 1:00 PM
Edited Mar 30, 2010 at 2:49 PM

Hey, I still have this problem. I have the following:

 

Size size = new Size(graphLayout.ActualWidth, graphLayout.ActualHeight);

graphLayout.Measure(size);
graphLayout.Arrange(new Rect(size));

foreach (MyVertexBase vertex in graphLayout.Graph.Vertices)
{
double vertexX = GraphCanvas.GetX(vertex);
double vertexY = GraphCanvas.GetY(vertex);
//Do stuff here
}

 

but vertexX and vertexY is NaN.

Can anyone please debunk the GraphCanvas.GetX&Y and GraphCanvas.SetX&Y myth for me please. I am trying to draw a rectangle on screen(working) and then moving all the vertices in that area as a collective, instead of moving the vertices one by one. Any help is appreciated.

Thanks

Mar 30, 2010 at 5:16 PM

I've done a bit of playing in that area, and I think the GetX, GetY calls are what you want.  However, before  layout is done, I think you will get NaNs back.  Try doing a Relayout first just to see if you then get good values for the X and Y.

   -Jacob

Mar 31, 2010 at 7:56 AM
Edited Mar 31, 2010 at 9:26 AM

Hi Jacob.

After the Relayout() I still get NaN's.

Can anyone please help me?

Here is my Vertex if it helps:

 

public class MyVertexBase : DependencyObject
    {
        #region IMAGE

        public static readonly DependencyProperty IMAGE_PROPERTY =
           DependencyProperty.Register("IMAGE", typeof(Image), typeof(MyVertexBase));

        public Image Image
        {
            get { return (Image)GetValue(IMAGE_PROPERTY); }
            set { SetValue(IMAGE_PROPERTY, value); }
        }

        #endregion

        #region LABEL

        public static DependencyProperty LABEL_PROPERTY =
            DependencyProperty.Register("LABEL", typeof(string), typeof(MyVertexBase));

        public string LABEL
        {
            get { return (string)GetValue(LABEL_PROPERTY); }
            set { SetValue(LABEL_PROPERTY, value); }
        }

        #endregion

        #region SELECTED

        public static DependencyProperty SELECTED_PROPERTY =
            DependencyProperty.Register("SELECTED", typeof(bool), typeof(MyVertexBase));

        public bool SELECTED
        {
            get { return (bool)GetValue(SELECTED_PROPERTY); }
            set { SetValue(SELECTED_PROPERTY, value); }
        }

        #endregion

        #region UID

        public static DependencyProperty UID_PROPERTY =
            DependencyProperty.Register("UID", typeof(string), typeof(MyVertexBase));

        public string UID
        {
            get { return (string)GetValue(UID_PROPERTY); }
            set { SetValue(UID_PROPERTY, value); }
        }

        #endregion

        #region LEVEL

        public static DependencyProperty LEVEL_PROPERTY =
            DependencyProperty.Register("LEVEL", typeof(int), typeof(MyVertexBase));

        public int LEVEL
        {
            get { return (int)GetValue(LEVEL_PROPERTY); }
            set { SetValue(LEVEL_PROPERTY, value); }
        }

        #endregion

    }

Should it inherit from VertexControl or something? Because everyone said: GraphCanvas.GetY(vertexControl)???

 

rootme

Apr 6, 2010 at 4:25 PM

rootme,

 

When I've been doing it I have inherited from VertexControl (actually a few different times so different types of vertices all have their own control templates).   I'm not sure it that will help you, but perhaps it will.   I'd have to dig in to try and figure out where I was doing something similar.

 

-Jacob

Apr 7, 2010 at 11:14 PM

Hi,

I'm having similar problem. I need to put vertices in proper places on the screen so that they will create sort of network (for example streets in the city). I'm beginner in G# and I don't know were to find this settings of positions of vertices.

Apr 15, 2010 at 1:30 PM

Hey guys',

I'm still stuck here. :( Please someone, just a short sample showing a working solution.

I think a lot of people are having this problem.

 

Apr 15, 2010 at 4:17 PM

I'll look into this *issue* tonight. I'll let you know!

Apr 16, 2010 at 8:03 AM

Ok thank you very much!

Eagerly awaiting your reply!

 

Apr 18, 2010 at 9:19 PM

Try GraphLayout.GetX(graphLayout.GetVertexControl(vertex))

where:

- vertex = the vertex object

- graphLayout = the GraphLayout control

 

Apr 28, 2010 at 8:12 AM
Edited Apr 28, 2010 at 8:15 AM
promontis you GENIUS! Thank you! It works! Just a small correction from your sample. The first "GraphLayout" should be "GraphCanvas". Here is a working sample using promontis' sample: double x = GraphCanvas.GetX(graphLayout.GetVertexControl(vertex)); double y = GraphCanvas.GetY(graphLayout.GetVertexControl(vertex)); GraphCanvas.SetX(graphLayout.GetVertexControl(vertex), 100);//The 100 can be whatever. GraphCanvas.SetY(graphLayout.GetVertexControl(vertex), 100); //The 100 can be whatever. Hope this helps anyone having the same problem in the future. *Edit: The post thing is broken. Sorry for the formatting.*
Apr 28, 2010 at 9:25 PM

I and my friend have a very big problem with this solutions. In ours computer there is no such thing as GraphCanvas. Simple "The name 'GraphCanvas' does not exist in the current context ". We have very similar problem with GraphLayout.GetX(graphLayout.GetVertexControl(vertex)). graphLayout exist but there is no such thing in graphLayout as GetX. Should we install some special librares ?This is the list of our librares

"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuickGraph;

"

If you can please help us :)

Press ENTER to look up in Wiktionary or CTRL+ENTER to look up in Wikipedia
<input id="QuickWikiInput" class="QuickWiki" type="text" />
Apr 28, 2010 at 9:32 PM

From your list, I can see that you're not using GraphSharp.

Apr 28, 2010 at 9:53 PM

ok I add GraphSharp and my list looks like

"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuickGraph;
using GraphSharp;"

but there is still the same error "The name 'GraphCanvas' does not exist in the current context ". Moreover compilator doesn't give any error connecting with library so probably GraphSharp was founded on my computer ;/

Apr 28, 2010 at 9:56 PM

Please try the tutorial and look for differences between that project and yours.

Apr 28, 2010 at 10:32 PM

1. I solve part of the problem because using GraphSharp is not enought. I must use "using GraphSharp.Controls;".

2. In tutorial

http://www.youtube.com/watch?v=VTbuvkaPGxE

there is no "using GraphSharp;" at all . Only "using QuickGraph;" so this tutorial from my point of view is no so usefull

3. I include all librares

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuickGraph;
using QuickGraph.Algorithms;
using QuickGraph.Collections;
using QuickGraph.Contracts;
using QuickGraph.Petri;
using QuickGraph.Predicates;
using QuickGraph.Serialization;
using GraphSharp;
using GraphSharp.Controls;
using GraphSharp.Algorithms;
using GraphSharp.Contracts;
using GraphSharp.Converters;
using GraphSharp.Helpers;

but still have a problem

"\ 'GraphSharp.Controls.GraphLayout' does not contain a definition for 'GetVertexControl' and no extension method 'GetVertexControl' accepting a first argument of type 'GraphSharp.Controls.GraphLayout' could be found (are you missing a using directive or an assembly reference?)   "

so program finds GraphCanvas.GetX but in graphLayout cannot find GetVertexControl.

I made my graph in XAML exaclty like in tutorial so it is looks like

"<graphsharp:GraphLayout x:Name="graphLayout" Graph="{Binding ElementName=root ,Path=GraphToVisualize}"
                                LayoutAlgorithmType="FR"
                                OverlapRemovalAlgorithmType="FSA"
                                HighlightAlgorithmType="Simple">"

 

Apr 29, 2010 at 8:48 AM
Have you added the references to the GraphSharp, GraphSharp.Controls?
Apr 29, 2010 at 9:26 AM

Yes I have references to the GraphSharp, GraphSharp.Controls. Here is all my project. Maybe you find what is wrong ?

http://www.vjdw.michalin.pl/MaximalFlowNetwork.rar

Press ENTER to look up in Wiktionary or CTRL+ENTER to look up in Wikipedia
<input id="QuickWikiInput" class="QuickWiki" type="text" />
May 1, 2010 at 12:17 PM

Please guys help me. Option with set constant value of X and Y vertex position is necessary in my work. If you don't help me with this little thing I will be forced to write everything on my own. I have deadline in thursday and in this case propably I don't make whole project on time.


http://www.vjdw.michalin.pl/MaximalFlowNetwork.rar

Here is my project. If you cannot find why it isn't working maybe you give me a little working project containing only set X and set Y vertex positions.

regards

May 1, 2010 at 12:42 PM

If you download the source code, you'll find a GraphSharpTutorial01 folder with a working sample. That's your starting point. Next, look at this post on how to set positions.

May 1, 2010 at 2:06 PM

are you talking about this

http://graphsharp.codeplex.com/SourceControl/changeset/changes/24813

because there is a floder GraphSharp Tutorial01 and there is tutorial which I find long time ago but of course it is not working. I add "using GraphSharp" and "GraphSharp.controls"and there is still the same problem. "GraphSharp.Controls.GraphLayout' does not contain a definition for 'GetVertexControl' and no extension method 'GetVertexControl' accepting a first argument of type 'GraphSharp.Controls.GraphLayout' could be found (are you missing a using directive or an assembly reference?) ".

Coordinator
May 2, 2010 at 8:11 AM

GraphLayout is a partial class. Look at the code in the GraphLayout.GraphElements.cs file, there is a GetVertexControl method ( http://graphsharp.codeplex.com/SourceControl/changeset/view/31744#529203 )

        public VertexControl GetVertexControl(TVertex vertex)
        {
            VertexControl vc = null;
            _vertexControls.TryGetValue(vertex, out vc);
            return vc;
        }

Using this method you can get the VertexControl for a vertex, after that with the GraphCanvas.SetX , SetY methods you can set the positions. Sorry, but it's not in the latest release, you need compile the lib from the latest source code.

palesz

May 3, 2010 at 9:44 AM

Thank you very much . Problem is solved. The solution is to download 31744 file build libriaries and change them with libriaries from tutorial.

By the way I have some other question.There is a posibility to make that vertex will have the same position after every adding or reamoving ? Of course i can create a loop and using getX and setX do this on my own

but i guess that there is a simple possibility to turn off function which change position during adding..

 

Press ENTER to look up in Wiktionary or CTRL+ENTER to look up in Wikipedia
<input id="QuickWikiInput" class="QuickWiki" type="text" />
May 6, 2010 at 9:16 AM

Hi, it's me once again.

I have very strange situation with adding vertex and changing position. I have code in button_Click this:

BidirectionalGraph<object>> g = (BidirectionalGraph<object>>)_graphToVisualize.ToBidirectionalGraph<object>>();
            VertexControl vs;
            int k;

            g.AddVertex(textBox3.Text);

            for (int i = 0; i < g.Vertices.Count(); i++)
            {
                int.TryParse(g.Vertices.ElementAt(i).ToString(), out k);
                vs = graphLayout.GetVertexControl(g.Vertices.ElementAt(i));
                if (vs != null)
                {
                    GraphCanvas.SetX(vs, position[k, 0]);
                    GraphCanvas.SetY(vs, position[k, 0]);
                }

            }</object></object></object>

The problem is that,, when I click "add vertex" all vertices totally change theirs positions. Moreover if I click once again (and 

g.AddVertex is not working because textBox3.Text has value existing vertex) everything works perfectly and come back to proper positions. It looks like
that when g.AddVertex(textBox3.Text) works program ignore GraphCanvas.SetX(vs, position[k, 0]) and when g.AddVertex(textBox3.Text) doesn't work
program set GraphCanvas.SetX(vs, position[k, 0]) properly.  Do you have any idea why I have some strange problem ?
May 11, 2010 at 12:14 AM
Edited May 11, 2010 at 12:14 AM

Ok I change in xaml to "LayoutAlgorithmType="KK" " and now is better . When I add vertex the vertices doesn't dance so much. But they are still moving. Where I can find some information about LayoutAlgorithmType ?

How and what I should set that after adding vertex the rest of verticies will not move at all.


May 13, 2010 at 10:53 AM

So I change "KK "to "FreeFR" and everything working perfectly ! So cannot understand why nobody said it to me ! This is so facking easy to write if you know that and so fucking timeconsuming if you have to make it up on your own.This is only

5 letters !!!

Press ENTER to look up in Wiktionary or CTRL+ENTER to look up in Wikipedia
<input id="QuickWikiInput" class="QuickWiki" type="text" />
Aug 31, 2010 at 11:31 PM

I want to restore my vertex positions (not run the algorithm).  When I call graphLayout.GetVertexControl(xxx), it returns empty :(  But I did call AddVertex.  My guess the controls aren't created yet. 

Do you when I set the vertex positions without running the algorithm? 

Any ideas?  Thanks.

Sep 10, 2010 at 10:15 PM

@poprostuJanek Would you post your code so that others can benefit from what you learned?

Oct 7, 2011 at 11:39 AM
Edited Oct 7, 2011 at 11:56 AM

I will thank that code

Oct 7, 2011 at 12:36 PM

 

Hi, I'm having some strange trouble with this thing. When i add a vertex, the vertex count in data object(GraphToVisualize) increase but it doesn`t in Xaml object(graphsharplayout) and because of that i can´t set the position of vertex. I can see the vertex, but graphsharplayout.Children.Count is not updated. All i want to do is when i add a new vertex to the graph that it appears in a different position than previous vertex, so i can see all vertex in the graph and draw the connections between them as i wish.

This is what i am trying to do:

when i add a new vertex, i try to change its position, but i receive the error NullReferenceException, v Vertex is not in the graph. 

C# code 

GraphToVisualize.AddVertex(v);

                              
if (GraphToVisualize.VertexCount > 1)
{
        double x = GraphCanvas.GetX(graphsharplayout.GetVertexControl(v));
        double y = GraphCanvas.GetY(graphsharplayout.GetVertexControl(v));
        GraphCanvas.SetX(graphsharplayout.GetVertexControl(v), 100);
        GraphCanvas.SetY(graphsharplayout.GetVertexControl(v), 100);
}

 This is de xaml part:

<graphsharp:GraphLayout x:Name="graphsharplayout"
       Graph="{Binding ElementName=root,Path=GraphToVisualize, NotifyOnSourceUpdated=True}"
       LayoutAlgorithmType="FreeFR"
       OverlapRemovalAlgorithmType="FSA"
       HighlightAlgorithmType="Simple" />

 

Thanks for any help.

Dec 16, 2011 at 11:50 PM

Sorry to further extend this thread (I hope it's still alive) but I have a related question.

I am trying to get the graph coordinates of each node as soon as possible after the graph loads. I can do this using the approach outlined above (eventually - the current release package DLLs don't seem to support the GetVertexControl method so a build is required) but only in response to a mouse event.

If I handle the 'loaded' event on my GraphLayout object all vertex ordinates come back NaN. If I let the graph load, then mouseover and handle the 'MouseEnter' event I get useful numbers.

I thought maybe this was related to the startup animation, but that seems kind of weird.

Does anyone know at what point I can reliably retrieve coordinates?

Any help much appreciated.

Jan 3, 2012 at 7:22 PM

I figured out my issue by handling both the Loaded and LayoutUpdated events from the GraphLayout.

LayoutUpdated is fired several times before Loaded, but within the Loaded event I call graphLayout.UpdateLayout() and the next time LayoutUpdated is fired the coordinates are available (not NaN) as expected.

I really like Graph# but the lack of documentation beyond a single video on Youtube and the tumbleweeds rolling through this forum are so frustrating - it seems crazy to put so much effort into writing the code but then discourage people from using it.

Jun 30 at 2:15 PM
Edited Jun 30 at 2:17 PM
Hi, I would like to do the same thing, but according to 'rootme', I have to do something like :
double x = GraphCanvas.GetX(graphLayout.GetVertexControl(vertex));

My problem is that my graphLayout haven't got any method called GetVertexControl.

There is my code :
var g = new BidirectionalGraph<VertexControl, IEdge<VertexControl>>();
// some code building the graph
var graphLayout = new GraphLayout<VertexControl, IEdge<VertexControl>, BidirectionalGraph<VertexControl, IEdge<VertexControl>>>();
graphLayout.Graph = g;
double x = GraphCanvas.GetX(graphLayout.GetVertexControl(g.Vertices.ElementAt(0))); 
Thank you !!
Jun 30 at 3:37 PM
If you want that method you should get latest sources and build them by yourself.
Jul 1 at 3:47 PM
Edited Jul 2 at 4:55 PM
Oh Nice ! Thank you @panthernet :) Now I can use the GetVertexControl method !
The value that I get is still NaN. I guess that it is not normal ? I probably forgot something.
Do you see something wrong ?
(I'm not using WPF, I just want to get coordinates dynamically)
var g = new BidirectionalGraph<VertexControl, IEdge<VertexControl>>();
// some code building the graph

var graphLayout = new GraphLayout<VertexControl, IEdge<VertexControl>, BidirectionalGraph<VertexControl, IEdge<VertexControl>>>();
graphLayout.Graph = g;
graphLayout.LayoutAlgorithmType = "FreeFR";

LayeredTopologicalSortAlgorithm<VertexControl, IEdge<VertexControl>> algo = new LayeredTopologicalSortAlgorithm<VertexControl, IEdge<VertexControl>>(g);
algo.Compute();

VertexControl vertex = g.Vertices.ElementAt(1);
double x = GraphCanvas.GetX(graphLayout.GetVertexControl(vertex)); // NaN
double y = GraphCanvas.GetY(graphLayout.GetVertexControl(vertex)); // NaN
Jul 2 at 7:44 PM
Edited Jul 2 at 7:44 PM
Problem solved
this.algo = new FRLayoutAlgorithm<VertexControl, IEdge<VertexControl>, BidirectionalGraph<VertexControl, IEdge<VertexControl>>>(g);
algo.Compute();

//positions here -> this.algo.VertexPositions