6

我有一个大约 5000 个节点和 5000 个链接的图表,我可以在 Chrome 中可视化,这要归功于vivagraph javascript 库(webgl 比 svg 快 - 例如在 d3 中)。

我的工作流程是:

问题是渲染具有良好定位节点的布局需要时间。

例如,我的方法是预先计算networkx中的节点位置。这种方法的真正好处是它可以最大限度地减少客户端在浏览器上的工作。但我无法在网页上获得良好的位置。我需要这一步的帮助。

节点位置计算的相关python代码是:

    ## positionning
    try:
        # Position nodes using Fruchterman-Reingold force-directed algorithm.
        pos=nx.spring_layout(G)
        for k,v in pos.iteritems():
            # scaling tentative
            # from small float like 0.5555 to higher values
            # casting to int because precision is not important
            pos[k] = [ int(i*1000) for i in v.tolist() ]
    except Exception, e:
        print "positionning failed"
        raise

    ## setting positions
    try:
        # set position of nodes as a node attribute
        # that will be used with the js library
        nx.set_node_attributes(G,'pos', pos)
    except Exception, e:
        print "getting positions failed"
        raise e

    # output all the stuff
    d = json_graph.node_link_data(G)
    with open(args.output,'w') as f:
        json.dump(d,f)

然后在我的页面中,在 javascript 中:

/*global Viva*/
function graph(file){
  var file = file;

  $.getJSON(file, function(data) {
      var graphGenerator = Viva.Graph.generator();
      graph = Viva.Graph.graph();

      # building the graph with the json data :

      data.nodes.forEach(function(n,i) {
         var node = graph.addNode(n.id,{d: n.d});

         # node position is defined in the json element attribute 'pos'
         node.position = {
             x : n.pos[0],
             y : n.pos[1]
         };
      })

      # adding links between nodes

      data.links.forEach(function(l,i) {
          graph.addLink(data.nodes[l.source].id, data.nodes[l.target].id);
      })


        var max_link = 55
        var min_link = 1

        var colors = d3.scale.linear().domain([min_link,max_link]).range(['#F0F0F0','#252525']);

      var layout = Viva.Graph.Layout.forceDirected(graph, {
         springLength : 80,
         springCoeff : 0.0008,
         dragCoeff : 0.001,
         gravity : -5.0,
         theta : 0.8
      });

      var graphics = Viva.Graph.View.webglGraphics();
      graphics
      .node(function(node){

        # color and size of nodes

        color = colors(node.links.length)
        if(node.id == "root"){
          // pin node on canvas, so no position update
          node.isPinned = true;
          size = 60;
        } else {
          size = 20+(7-node.id.length)*(7-node.id.length);
        }
        return Viva.Graph.View.webglSquare(size,color);
      })
      .link(function(link) {

        # color on links 

        fromId = link.fromId;
        toId = link.toId;
        if(toId == "root" || fromId == "root"){
          return Viva.Graph.View.webglLine("#252525");
        } else {
          if( fromId[0] == toId[0]){
            linkcolor = linkcolors(fromId[0])
            return Viva.Graph.View.webglLine(linkcolor);
          } else {
            linkcolor = averageRGB(linkcolors(fromId[0]),linkcolors(toId[0]))
            return Viva.Graph.View.webglLine('#'+linkcolor);
          }
        }
      }); 

      renderer = Viva.Graph.View.renderer(graph,
          {
              layout     : layout,
              graphics   : graphics,
              enableBlending: false,
              renderLinks : true,
              prerender  : true
          });

      renderer.run();
  });
}

我现在正在尝试Gephi,但我不想使用gephi 工具包,因为我不习惯 java。

如果有人对此有一些提示,请避免我进行数百次试验,甚至可能失败;)

4

1 回答 1

1

Spring Layout 假设边缘权重支持度量属性,即 Weight(A,B)+Weight(A,C) > Weight(B,C)。如果不是这种情况,则 networkx 会尝试将它们放置得尽可能真实。

您可以尝试通过以下方式进行调整

    pos=nx.spring_layout(G,k=\alpha, iterations=\beta)
    # where 0.0<\alpha<1.0 and \beta>0 
    # k is the minimum distance between the nodes
    # iterations specify the simulated annealing runs
    # This code works only on Networkx 1.8 and not earlier versions
于 2013-06-26T06:06:51.270 回答