【问题标题】:Constant Layout Graph with VivaGraphJS使用 VivaGraphJS 的常量布局图
【发布时间】:2013-07-14 21:17:00
【问题描述】:

我用 VivaGraphJS 创建了一个图表,一切都很好,但图表正在移动。有一个关于 constantLayout 的示例,但您应该为每个节点指定一个位置。

代码如下:

<!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>

这是关于常量布局的代码:

<!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>

有什么建议!?

我希望我能很好地解释我的问题。谢谢

【问题讨论】:

    标签: javascript graph vivagraphjs


    【解决方案1】:

    如果你想要动态布局,但想在某个时刻停止动画,请使用renderer.pasue() 方法。

    如果你想有一个恒定的布局,你可以在你的代码中添加一些东西来告诉它你的节点的位置。这是您需要的代码:

    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();
    }
    

    【讨论】:

    • 感谢您的回答,但如果我想添加 200 个节点?我觉得加200个职位不切实际??
    • 只需使用相对布局,并在一定超时后将呈现布局设置为 false。它将停止计算布局,并且您的图表将保持在值更改时的位置不变。
    • 你好,我试着照你说的做,但我做不到,请你写一个例子,提前谢谢。
    • 给我一个 jsFiddle 来重现问题
    猜你喜欢
    • 2013-08-06
    • 2017-01-02
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多