gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:33:36 25 4
gpt4 key购买 nike

我正在尝试将网络 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>

最佳答案

您的 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')

关于javascript - 从 json 导入时,vis.js 无法更改边缘颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50519652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com