首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vis.js - Place节点

vis.js - Place节点
EN

Stack Overflow用户
提问于 2015-10-02 07:52:34
回答 3查看 26.5K关注 0票数 15

如何在vis.js?中设置节点的位置

我想首先手动定位至少一个节点。

我知道节点有选项xy。我设置了这两个选项,并尝试了布局选项的变体(randomSeedrandomSeed)--节点从未放在我设置的位置。

下面是我定义的简单网络:

代码语言:javascript
复制
  nodes = new vis.DataSet([
    {id: 1,  shape: 'circularImage', image: DIR + '1_circle', label:"1", x: 200, y: 100},
    {id: 2,  shape: 'circularImage', image: DIR + '2_circle', label:"2"},
    {id: 3,  shape: 'circularImage', image: DIR + '3_circle', label:"3"},
  ]);

  edges = [
    {id: "01-03", from: 1, to: 3, length: 300, label: '1 - 3'},
    {id: "02-03", from: 2, to: 3},
  ];

  var container = document.getElementById('graphcontainer');
  var data = {
    nodes: nodes,
    edges: edges
  };
  var options = {
    nodes: {
      borderWidth: 4,
      size: 30,
      color: {
        border: '#222222',
        background: '#666666'
      },
      font:{
        color:'#000000'
      }
    },
    edges: {
      color: 'lightgray'
    },
    //layout: {randomSeed:0}
    //layout: {hierarchical: true}
    layout: {
      randomSeed: undefined,
      improvedLayout:true,
      hierarchical: {
        enabled:false,
        levelSeparation: 150,
        direction: 'UD',   // UD, DU, LR, RL
        sortMethod: 'hubsize' // hubsize, directed
      }
    }
  };
  network = new vis.Network(container, data, options);

节点被放置,但不是在我设置的点(200,100),而是在另一个位置。

我还没有找到在vis.js页面上显式设置节点位置的示例。有人能提供一个吗?谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-05 14:05:38

您确实可以通过设置节点的xy属性来为其设置固定位置,是的,此功能可以工作,而且不会中断。

节点的xy位置并不意味着屏幕上以像素为单位的位置,而是网络坐标系中的一个固定位置。当您移动和放大网络,固定的项目也将移动和缩放,但他们将始终保持相同的位置相对于对方。就像你的家乡在地球上有一个固定的位置(长,拉特),但是你仍然可以在谷歌地图中缩放和移动你的城镇。

编辑:为了实现您想要的,您可以修复缩放和移动,并调整视图端口,使其与HTML画布的像素匹配,下面是一个演示:

代码语言:javascript
复制
// create an array with nodes
var nodes = new vis.DataSet([
    {id: 1, label: 'x=200, y=200', x: 200, y: 200},
    {id: 2, label: 'node 2', x: 0, y: 0},
    {id: 3, label: 'node 3', x: 0, y: 400},
    {id: 4, label: 'node 4', x: 400, y: 400},
    {id: 5, label: 'node 5', x: 400, y: 0}
]);

// create an array with edges
var edges = new vis.DataSet([
    {from: 1, to: 2, label: 'to x=0, y=0'},
    {from: 1, to: 3, label: 'to x=0, y=400'},
    {from: 1, to: 4, label: 'to x=400, y=400'},
    {from: 1, to: 5, label: 'to x=400, y=0'}
]);

// create a network
var container = document.getElementById('mynetwork');
var data = {
    nodes: nodes,
    edges: edges
};
var width = 400;
var height = 400;
var options = {
    width: width + 'px',
    height: height + 'px',
    nodes: {
        shape: 'dot'
    },
    edges: {
        smooth: false
    },
    physics: false,
    interaction: {
        dragNodes: false,// do not allow dragging nodes
        zoomView: false, // do not allow zooming
        dragView: false  // do not allow dragging
    }
};
var network = new vis.Network(container, data, options);
  
// Set the coordinate system of Network such that it exactly
// matches the actual pixels of the HTML canvas on screen
// this must correspond with the width and height set for
// the networks container element.
network.moveTo({
    position: {x: 0, y: 0},
    offset: {x: -width/2, y: -height/2},
    scale: 1,
})
代码语言:javascript
复制
#mynetwork {
    border: 1px solid black;
    background: white;
    display: inline-block;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.9.0/vis.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.9.0/vis.min.css" rel="stylesheet" type="text/css" />

<p>The following network has a fixed scale and position, such that the networks viewport exactly matches the pixels of the HTML canvas.</p>
<div id="mynetwork"></div>

票数 29
EN

Stack Overflow用户

发布于 2015-10-05 11:46:24

文档显示由布局算法定位的节点

使用分层布局时,布局引擎将根据视图的类型设置x或y位置。

你可以把它们放到明确的点上,但我不建议这样做--这不是处理图表的正确方法--更好地回顾你的任务--也许你不需要图表(或者你不需要把分数精确地放在位置上)。

无论如何,如果您真的想进入某个位置,那么您需要使用随机布局,将固定选项设置为truephysic选项设置为false

代码语言:javascript
复制
var DIR = 'http://cupofting.com/wp-content/uploads/2013/10/';
nodes = new vis.DataSet([
    {id: 1,  shape: 'circularImage', image: DIR + '1-circle.jpg', label:"1", x:0, y:0},
    {id: 2,  shape: 'circularImage', image: DIR + '2-circle.jpg', label:"2", x:100, y:0},
    {id: 3,  shape: 'circularImage', image: DIR + '3-circle.jpg', label:"3", x:0, y:100},
  ]);

  edges = [
    {id: "01-03", from: 1, to: 3, length: 300, label: '1 - 3'},
    {id: "02-03", from: 2, to: 3},
  ];

  var container = document.getElementById('graphcontainer');
  var data = {
    nodes: nodes,
    edges: edges
  };
  var options = {
       physics:{
           stabilization: true,
       },
    nodes: {
      borderWidth: 4,
      size: 10,
      //fixed:true,
      physics:false,
      color: {
        border: '#222222',
        background: '#666666'
      },
      font:{
        color:'#000000'
      }
    },
    edges: {
      color: 'lightgray'
    },
    layout: {randomSeed:0}
  };
  network = new vis.Network(container, data, options);
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.9.0/vis.min.js"></script>
<div id="graphcontainer" style="border: 1px solid red; width:300px; height:300px; "> </div>

票数 5
EN

Stack Overflow用户

发布于 2015-11-04 23:59:06

)我今天早上第一次这么做。X=0和Y=0是以图形开始为中心的,而不是在画布的左上角。节点有一个固定的属性,对于x和y值设置的节点,可以将其设置为true,并让其他节点使用与其相关的物理属性。

查看页面http://visjs.org/docs/network/nodes.html#上的完整选项卡

你会看到这幅画:

代码语言:javascript
复制
fixed: {
  x:false,
  y:false
},
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32902720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档