从json导入时,vis.js无法更改边缘颜色

时间:2018-05-24 23:44:34

标签: javascript json vis.js vis.js-network

我正在尝试将网络json文件导入vis.js,并希望所有边缘都以各自的颜色显示。

它适用于节点但不适用于边缘,有没有办法独立于各自的节点显示边缘?任何指导表示赞赏

这是json文件

{ 
  "nodes":
  [
    {
      "id": "1",
      "label": "A",
      "color": "rgb(128,128,128)"
    },
    {
      "id": "2", 
      "label": "B",
      "color": "rgb(0,255,0)"
    },
    {
      "id": "3", 
      "label": "C",
      "color": "rgb(0,0,255)"
    }
  ],
  "edges": 
  [
    {
      "source": "1",
      "target": "2", 
      "id": "1", 
      "color": "rgb(255,0,0)"
    },
    { 
      "source": "1",
      "target": "3",
      "id": "2",
      "color": "rgb(0,0,0)"
     }
  ]
}

这是HTML页面



<!DOCTYPE html>
<!-- saved from url=(0044)http://kenedict.com/networks/worldcup14/vis/ , thanks Andre!-->
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF8">
  <title>Dynamic Data - Importing from Gephi (JSON)</title>
  <script type="text/javascript" src="../exampleUtil.js"></script>

  <script type="text/javascript" src="../../../dist/vis.js"></script>
  <link type="text/css" rel="stylesheet" href="../../../dist/vis-network.min.css">

  <style type="text/css">
    #mynetwork {
      width: 800px;
      height: 800px;
      border: 1px solid lightgray;
    }

    div.nodeContent {
      position: relative;
      border: 1px solid lightgray;
      width: 480px;
      height: 780px;
      margin-top: -802px;
      margin-left: 810px;
      padding: 10px;
    }

    pre {
      padding: 5px;
      margin: 5px;
    }

    .string {
      color: green;
    }

    .number {
      color: darkorange;
    }

    .boolean {
      color: blue;
    }

    .null {
      color: magenta;
    }

    .key {
      color: red;
    }
  </style>
  
</head>

<body>

<h2>Importing from Gephi (JSON)</h2>


<div id="mynetwork"></div>
<div class="edgeContent"><h4>Node Content:</h4>
  <pre id="edgeContent"></pre>
</div>

<script type="text/javascript">
   var network;

  var nodes = new vis.DataSet();
  var edges = new vis.DataSet();
  var gephiImported;
 
  var nodeContent = document.getElementById('nodeContent');

  var parserOptions = {
    edges: {
      inheritColors: false
    },
    nodes: {
      fixed: true,
      parseColor: false
    }
  }

  var gephiJSON = loadJSON('../datasources/test.json', redrawAll, function(err) {console.log('error')});

  var container = document.getElementById('mynetwork');
  var data = {
    nodes: nodes,
    edges: edges
  };

  var options = {
    edges: {
      color: {
        inherit: false,
      }
    },
    physics: {
      stabilization: false,
    }
  };

  network = new vis.Network(container, data, options);

  /**
   * This function fills the DataSets. These DataSets will update the network.
   */
  function redrawAll(gephiJSON) {

    nodes.clear();
    edges.clear();

    var parsed = vis.network.gephiParser.parseGephi(gephiJSON, parserOptions);


    // add the parsed data to the DataSets.
    nodes.add(parsed.nodes);
    edges.add(parsed.edges);
    console.log(parsed.edges)

    network.fit(); // zoom to fit
  }

</script>
</script>

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的JSON数据结构看起来像vis.js所期望的。可能是您将边缘颜色选项“继承”设置为false吗?在文档示例中,它将网络边缘选项的字段设置为“ from”,如果要调整解析器选项,则将键更改为“ inheritColors”:https://almende.github.io/vis/docs/network/edges.html; https://almende.github.io/vis/docs/network/index.html?keywords=network(控制F代表“ inheritColors”)