2

I created a graph with VivaGraphJS and everything is well but the graph is moving .there is an example about constantLayout but the problem you should specify for every node a position.

here is the code:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="../js/vivagraph.js"></script>
    <script type="text/javascript">
        function main () {
            // create a graph object.
            var graph = Viva.Graph.graph();

            add nodes and edges to the graph:
            graph.addLink(1, 2);
            graph.addLink(1, 3);
            graph.addLink(1, 4);
            var renderer = Viva.Graph.View.renderer(graph);
            renderer.run();
        }
    </script>

    <style type="text/css" media="screen">
        html, body, svg { width: 100%; height: 100%;}
    </style>
</head>
<body onload='main()'>
</body>
</html>

and here is the code about the constant Layout :

<!DOCTYPE html>
<html>
    <head>
        <title>VivaGraphs constant layout demo page</title>
        <script src="../../dist/vivagraph.js"></script>

        <script type='text/javascript'>

            function onLoad() {
                var graph = Viva.Graph.graph(),
                    nodePositions = [{x : -50, y: 0}, {x : 0, y: -50}, {x : 50, y: 0}], // predefined node positions
                    layout = Viva.Graph.Layout.constant(graph),
                    renderer = Viva.Graph.View.renderer(graph, {
                                   layout     : layout, // use our custom 'constant' layout
                               }),

                    i, nodesCount = nodePositions.length; // convinience variables.

                // Add nodes
                for(i = 0; i < nodesCount; ++i) {
                    graph.addNode(i, nodePositions[i]);
                }

                // and make them connected in cycle:
                for (i = 0; i < nodesCount; ++i) {
                    graph.addLink(i % nodesCount, (i + 1) % nodesCount);
                }

                // set custom node placement callback for layout.
                // if you don't do this, constant layout performs random positioning.
                layout.placeNode(function(node) {
                    // node.id - points to its position but you can do your 
                    // random logic here. E.g. read from specific node.data
                    // attributes. This callback is expected to return object {x : .. , y : .. }
                    return nodePositions[node.id]; 
                });

                renderer.run();
            }
        </script>

        <style type="text/css" media="screen">
            body, html, svg { width: 100%; height: 100%; overflow: hidden; }
        </style>
    </head>
    <body onload="onLoad()">

    </body>

Any suggestion !?

I wish I explain well my problem. Thanks

VividD
  • 10,456
  • 6
  • 64
  • 111
dardar.moh
  • 5,987
  • 3
  • 24
  • 33

1 Answers1

3

If you want to have dynamic layout, but want to stop animation at some point use renderer.pasue() method.

If you want to have a constant layout, you can add some things in your code to tell it the locations of your nodes. Here's the code you'll need:

function main () {
    // create a graph object.
    var graph = Viva.Graph.graph(),
        nodePositions = [{x : -50, y: 0}, {x : 0, y: -50}, {x : 50, y: 0}, {x: 60, y: 20}], 
        layout = Viva.Graph.Layout.constant(graph);

    for(var i = 1; i < 5; i++) {
       graph.addNode(i, nodePositions[i-1]);
    }

    graph.addLink(1, 2);
    graph.addLink(1, 3);
    graph.addLink(1, 4);
    layout.placeNode(function(node) {
       return nodePositions[node.id-1]; 
    });
    var renderer = Viva.Graph.View.renderer(graph, { layout:layout });
    renderer.run();
}
Anvaka
  • 15,658
  • 2
  • 47
  • 56
shashankg77
  • 2,206
  • 4
  • 18
  • 34
  • Thanks for your answer, but if I want add 200 nodes ?? I think it's not practical to add 200 positions ?? – dardar.moh Jul 14 '13 at 21:39
  • 1
    Just use a relative layout and after a certain timeout set the rendering Layout to false. It'll stop calculating the layout and your graph will remain constant in the position it was when the value was changed. – shashankg77 Jul 14 '13 at 22:05
  • Hi again, I try to do what you say to me, but I can't do it could you please write an example and thanks in advance. – dardar.moh Jul 16 '13 at 12:40
  • Give me a jsFiddle to reproduce the problem – shashankg77 Jul 17 '13 at 14:01