Zoomable network graph in AngularJS

社会主义新天地 提交于 2019-12-04 07:44:22

问题


I would like to visualize a network graph in an AngularJS application. The nodes and edges are stored as a JSON object, and nodes will be added and modified later on (say once every 30 seconds). I want to use Angular data binding to automatically update the graph when the JSON object changes. The graph will have 10-1000 nodes. The nodes will be rectangular text nodes containing about a sentence each. I would like the graph to be zoom- and pan-able.

I know about the following options so far:

  • ArborJS

    It is easy to make dynamic updating work with Angular (using ParticleSystem.merge). However, Arbor does not seem to support zoomable behavior, and it does not seem to be well-supported. For example, the single-node bug is still unresolved.

  • D3

    There is a zoomable force layout demo, and various places have information on using d3 with Angular. D3 is well-supported, but it seems lower-level than the options below. For example, creating a network graph with good-looking rectangular node labels seems nontrivial.

  • VisJS

    VisJS supports zoomable network graphs, and there is a work-in-progress Angular library, but I don't know how reliable both VisJS and its Angular library are.

  • SigmaJS

    SigmaJS also supports zoomable network graphs, but I don't know whether it plays nicely with Angular.

  • CytoscapeJS

  • kmap

Are there other relevant libraries? What is the best library to use for this project, and how can I implement such a zoomable dynamic network graph given the library?


回答1:


I've been experimenting in VisJs in angular, and I'm really liking it so far. Their network is both pannable and zoomable, and you can select nodes. The documentation has been easy to follow and there are a lot of examples on their site. Since vis's networks can dynamically update, I found it easy to integrate it in my angular app. For example, I created this directive:

app.directive('visNetwork', function() {
    return {
        restrict: 'E',
        require: '^ngModel',
        scope: {
            ngModel: '=',
            onSelect: '&',
            options: '='
        },
        link: function($scope, $element, $attrs, ngModel) {
            var network = new vis.Network($element[0], $scope.ngModel, $scope.options || {});

            var onSelect = $scope.onSelect() || function(prop) {};
            network.on('select', function(properties) {
                onSelect(properties);
            });

        }

    }
});

Which I use in my html like so:

<vis-network ng-model="network_data" options="network_options" on-select="onNodeSelect" id="previewNetwork">
</vis-network>

Then in my controller I have the following:

    $scope.nodes = new vis.DataSet();
    $scope.edges = new vis.DataSet();
    $scope.network_data = {
        nodes: $scope.nodes,
        edges: $scope.edges
    };
    $scope.network_options = {
        hierarchicalLayout: {
            direction: "UD"
        }

    };

   $scope.onNodeSelect = function(properties) {
        var selected = $scope.task_nodes.get(properties.nodes[0]);
        console.log(selected);
    };

    $scope.nodes.add([
        {id: 1, label: 'Node 1'},
        {id: 2, label: 'Node 2'},
        {id: 3, label: 'Node 3'},
        {id: 4, label: 'Node 4'},
        {id: 5, label: 'Node 5'}]);

    $scope.edges.add([
        {id: 1, from: 1, to: 2},
        {id: 2, from: 3, to: 2}
    ]);



回答2:


This should really be on Software Recommendation StackExchange but I can't vote to close because of the bounty.

GoJS supports all of your requirements and works alongside Angular (simple demo here). (JSON for Model storage, data-binding, zoom and pan built in)




回答3:


There is a good demo/example of a network map with sourcecode in D3: http://christophergandrud.github.io/d3Network/ The functionality is all there, and D3 seems to play nice with JSON. From my research, this is a strong choice for a visualization library. Many other libraries (graphite, etc.) also support the same functionality but are harder to implement and aren't extremely active.

NVD3 is a variation of D3 designed for AngularJS that could also work. Implementing graphs and charts from within NVD3 is easier in AngularJS than D3 would be.




回答4:


In a commercial context you should also consider yFiles for HTML as a library for bringing high-quality graph visualization to Angular (and AngularJS) powered-apps.

It's a full-featured graph drawing and editing software library that provides solutions for all your graphing and diagramming needs.

Specifically 1000 nodes are not a problem, at least if this is not on low-end, older mobile devices, in which case only simple visualizations will provide good performance. But even then, with the unique hybrid rendering engine that can leverage all of SVG, Canvas, and WebGL at the same time in a diagram even that should work.

For a thousand nodes with each one line of text on them, on low-end devices it will be problematic to display all of them on the screen at the same time, however virtualization also helps here.

There are some live, online demos that show different levels of Angular(2+) and AngularJS integrations, but if you really want to play with the library on a programming level, you should download it and check out the non-minified sources for those demos. For Angular2+ development a complete set of TypeScript bindings is available and the samples show how to bind angular-data to the graph visualization as well as how to optionally use angular for the templating of the SVG visualizations. Of course they also include the core graph visualization Angular component.

Disclosure: I work for the company that provides that library, however I do not represent my employer on SO.



来源:https://stackoverflow.com/questions/24396708/zoomable-network-graph-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!