Vis.js使用

Vis.js的使用

vis.js 基于浏览器的动态可视化库。该库被设计为易于使用,处理大量的动态数据,并支持对数据的操作和交互。该库由组件DataSet,Timeline,Network,Graph2d和Graph3d组成。这里主要用到Network(网状图)

network实例请参考官网:http://visjs.org/network_examples.html。

network是一种可视化的网络和网络组成的nodes节点和edges边缘。可视化易于使用,并支持自定义形状、样式、颜色、大小、图像等。网络可视化工作在任何现代浏览器上都能达到几千个节点和边缘。为了处理更大数量的节点,网络具有集群支持。网络使用HTML画布进行渲染。

Network创建:

Network创建

创建一个Network类型的vis是很容易的。要求您包括vis.js和css文件,这些文件可以在这里得到。你所需要一个容器div来告诉你在哪里放置你的网络,在代码中,这看起来像:

----在head标签中

<head>

        //最重要的两个源文件的引入vis.js和vis-network.min.css注意路径不要错

    <script type="text/javascript" src="../../dist/vis.js"></script>

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

 

    <style type="text/css">

        #mynetwork {

            width: 600px;

            height: 400px;

            border: 1px solid lightgray;

        }

    </style>

</head>

 

 

---在body标签中

<body>

<div id="mynetwork"></div>  //创建画布网状图区域

 

<script type="text/javascript">

 //创建节点个数

var nodes = new vis.DataSet([

    {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'}

    ]);

 

   //创建关系线,节点与哪个节点相连接

 

    var edges = new vis.DataSet([

        {from: 1, to: 3},

        {from: 1, to: 2},

        {from: 2, to: 4},

        {from: 2, to: 5}

    ]);

 

    // 创建一个网状图

    var container = document.getElementById('mynetwork');

 

//vis格式使用数据

    var data = {

        nodes: nodes,

        edges: edges

};

 

    var options = {};//数据设置(可对创建的节点,关系线进行设置),此处暂无设置

 

    // 初始化网状图(把网状图实例化)

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

</script>

</body>

</html>

效果如下:

 

 

接下来是对数据设置中数据的操作

在var options = {}中进行编辑,对接点和边进行设置:

var options = {
  
 nodes:{//节点控制
        borderWidth: 5,//节点边框宽度设置
       
borderWidthSelected: 10,//点击选中时的宽度设置、
       
color: {
            border: 'red',//
边框颜色的设置
       
},

    },
    edges:{//
关系线控制
       
width:2,//关系线宽度
       
arrows:{//箭头
           
to:{enabled:true,//箭头是否显示、开启
               
scaleFactor:0.5,//箭头的大小
               
type:'arrow',//箭头的类型:circle,bar,arrows
           
},
        },
        font:{
            size:5,
            color:'green',
            align: 'horizontal',
        },
        length:50,//
关系线线长设置,数字较大最好以100位单位修改可看出差异
       
dashes:false,//关系线虚线,false不是,true是
       
arrowStrikethrough: true,//关系线与节点处无缝隙
       
color: {
            color:'red',//
关系线颜色设置
           
highlight:'black',//点击关系线时的颜色
       
},
    },

};

 

效果如下:

 

 

 

 

节点的形状是可控制的,默认是椭圆形,可指定形状可以有正方形,圆形,三角形,倒三角形,星形,正六边形,点(圆点),图片,图标等,注意:没有矩形(长方形)

 

 

//修改创建节点个数

var nodes = new vis.DataSet([

   {id:1, label: '1',shape: 'dot',size:5, color:'red',},

    {id: 2, label: 'Node 2'},

        {id: 3, label: 'Node 3'},

        {id: 4, label: 'Node 4'},

        {id: 5, label: 'Node 5'}

    ]);

 

Shape形状属性,指定是点,如图,1节点成一个点

 

 

也可指定位square正方形

 

也可指定图片,图片要在当先代码路径下

var nodes = new vis.DataSet([
    {
id: 1, label: '',shape: 'image', image:'xt.png',size:30,physics:false,
       
fixed: {
           
x:false,
           
y:false,
        },
    },
    {
id: 2, label: 'Node 2'},
    {
id: 3, label: 'Node 3',shape:'circle',size:20,color:'gold',},
    {
id: 4, label: 'Node 4'},
    {
id: 5, label: 'Node 5'}
]);

 

 

创建节点时配置fixed属性true,默认使false,固定,使节点1无法拖动。

var nodes = new vis.DataSet([
    {
id:1, label: '1',shape: 'square', color:'red',
       
fixed: {
           
x:true,
           
y:true,
        },},
    {
id: 2, label: 'Node 2'},
    {
id: 3, label: 'Node 3'},
    {
id: 4, label: 'Node 4'},
    {
id: 5, label: 'Node 5'}
]);

 

 

 

创建节点配置xy属性固定节点绝对位置,每次刷新啊节点位置不变

var nodes = new vis.DataSet([
    {
id:1, label: '1',shape: 'square', color:'red',
       
x:1000,
       
y:800
   
},
    {
id: 2, label: 'Node 2'},
    {
id: 3, label: 'Node 3'},
    {
id: 4, label: 'Node 4'},
    {
id: 5, label: 'Node 5'}
]);

 

 

创建节点配置physics属性,物理属性true节点受重力影响,拖动后会自动向下滑动,false时不会向下滑动

属性固定节点绝对位置,每次刷新啊节点位置不变

var nodes = new vis.DataSet([

    {id:1, label: '1',shape: 'square', color:'red', 
       physics:true,


        x:1000,
       
y:800
   
},
    {
id: 2, label: 'Node 2'},
    {
id: 3, label: 'Node 3'},
    {
id: 4, label: 'Node 4'},
    {
id: 5, label: 'Node 5'}
]);

 

network.on函数可以覆盖叠加节点形状

节点1的创建

{id: 1, label: '',shape: 'dot',title:'',size:20, color:'#0066FF',size:30,physics:false,
   
fixed: {
       
x:false,
       
y:false,
    },
},

 

注意network.on一定要放到var network = new vis.Network(container, data, options);代码之后否则无法加载。

 

network.on("afterDrawing", function (ctx) {//正方形嵌入圆形中
   
var nodeId =1;
   
var nodePosition = network.getPositions([nodeId]);
    ctx.
strokeStyle = 'white';
    ctx.
fillStyle = 'white';
    ctx.
square(nodePosition[nodeId].x, nodePosition[nodeId].y,19);
    ctx.
fill();
    ctx.
stroke();
});

 

效果如图:

 

单个节点的控制可以在单个创建节点时进行设置:

例如对节点3进行设置,形状,颜色,大小

{id: 3, label: 'Node 3',shape:'circle',size:20,color:'gold',},

 

关系线单个的配置:长度(加长200,缩短-200),颜色,宽度(粗细)

var edges = new vis.DataSet([
    {
from: 1, to: 2,length:200,color:{color:'Chocolate',},width:4},
    {
from: 2, to:3},
    {
from: 3, to: 4},
    {
from: 4, to: 5}
]);

 

 

伪矩形(长方形)的绘制,找一条关系线,定义好合适的长度,加宽,并设置弯曲。

节点:增加节点6,7点形,尺寸最小

关系线:4----5箭头状态关闭,5---6,长度30,使关系线平滑,箭头状态关闭

 

var nodes = new vis.DataSet([
    {
id: 1, label: '',shape: 'image',title:'',image:'xt.png',size:30,physics:false,
       
fixed: {
            
x:false,
           
y:false,
        },
    },
    {
id: 2, label: 'Node 3'},
    {
id: 3, label: 'Node 3',shape:'circle',size:20,color:'gold',},
    {
id: 4, label: 'Node 4',},
    {
id: 5, label: 'Node 5',shape:'dot',size:0.1, },
    {
id: 6, label: 'Node 6',shape:'dot',size:0.1,},
    {
id: 7, label: 'Node 7',shape:'dot',size:0.1, },
]);


//创建关系线,节点与哪个节点相连接

var edges = new vis.DataSet([
    {
from: 1, to: 2,title: '',length:200,color:{color:'Chocolate',},width:4},
    {
from: 2, to:3},
    {
from: 3, to: 4},
    {
from: 4, to: 5, arrows:{to:{enabled:false,},}},
    {
from: 5, to: 6,width:30,length:'30',
       
smooth:{//使关系线平滑

            enabled:true,//状态开启
           
type:'continuous',
           
forceDirection:'none',
           
roundness:'0',
                },
       
arrows:{to:{enabled:false,},
        },
    },
    {
from: 6, to: 7},
]);

添加进度条:

第二个script标签

<script type="text/javascript">

 

function draw() {

 

所有的代码:节点创建,关系线创建,创建一个网状图,以vis格式使用数据
,节点控制,关系线控制………
 

 

 

}

 

<script>

<body>标签中加<body  οnlοad="draw()">
注意network.on,network.once一定要放到var network = new vis.Network(container, data, options);代码之后否则无法加载。

 

network.on("stabilizationProgress", function(params) {
   
var maxWidth = 496;
   
var minWidth = 20;
   
var widthFactor = params.iterations/params.total;
   
var width = Math.max(minWidth,maxWidth * widthFactor);

   
document.getElementById('bar').style.width = width + 'px';
   
document.getElementById('text').innerHTML = Math.round(widthFactor*100) + '%';
});


network.once("stabilizationIterationsDone", function() {
   
document.getElementById('text').innerHTML = '100%';
   
document.getElementById('bar').style.width = '496px';
   
document.getElementById('loadingBar').style.opacity = 0;
   
// really clean the dom element
   
setTimeout(function () {document.getElementById('loadingBar').style.display = 'none';}, 500);
});}

全部代码如下:

<!doctype html>
<
html>
<
head>
    <
title>Network | Basic usage</title>

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

    <
style type="text/css">
       
#mynetwork {
            width:
1100px;
            height:
610px;
            border:
1px solid lightgray;
        }
       
#he{
            position:
absolute;
            right:
120px;
            top:
20px;
            z-index:
99;
            margin:
0;
        }
       
#loadingBar {
            position:
absolute;
            top:
0px;
            left:
0px;
            width:
1100px;
            height:
610px;
            background-color:
rgba(200,200,200,0.8);
            -webkit-transition:
all 0.5s ease;
            -moz-transition:
all 0.5s ease;
            -ms-transition:
all 0.5s ease;
            -o-transition:
all 0.5s ease;
            transition:
all 0.5s ease;
            opacity:
1;
        }
       
#wrapper {
            position:
relative;
            width:
900px;
            height:
900px;
        }

        
#text {
            position:
absolute;
            top:
8px;
            left:
530px;
            width:
30px;
            height:
50px;
            margin:
auto auto auto auto;
            font-size:
22px;
            color:
#000000;
        }


       
div.outerBorder {
            position:
relative;
            top:
400px;
            width:
600px;
            height:
44px;
            margin:
auto auto auto auto;
            border:
8px solid rgba(0,0,0,0.1);
            background:
rgb(252,252,252); /* Old browsers */
           
background: -moz-linear-gradient(toprgba(252,252,252,1) 0%, rgba(237,237,237,1) 100%); /* FF3.6+ */
           
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(252,252,252,1)), color-stop(100%,rgba(237,237,237,1))); /* Chrome,Safari4+ */
           
background: -webkit-linear-gradient(toprgba(252,252,252,1) 0%,rgba(237,237,237,1) 100%); /* Chrome10+,Safari5.1+ */
           
background: -o-linear-gradient(toprgba(252,252,252,1) 0%,rgba(237,237,237,1) 100%); /* Opera 11.10+ */
           
background: -ms-linear-gradient(toprgba(252,252,252,1) 0%,rgba(237,237,237,1) 100%); /* IE10+ */
           
background: linear-gradient(to bottomrgba(252,252,252,1) 0%,rgba(237,237,237,1) 100%); /* W3C */
           
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#ededed',GradientType=0 ); /* IE6-9 */
           
border-radius:72px;
            box-shadow:
0px 0px 10px rgba(0,0,0,0.2);
        }

       
#border {
            position:
absolute;
            top:
10px;
            left:
10px;
            width:
500px;
            height:
23px;
            margin:
auto auto auto auto;
            box-shadow:
0px 0px 4px rgba(0,0,0,0.2);
            border-radius:
10px;
        }

       
#bar {
            position:
absolute;
            top:
0px;
            left:
0px;
            width:
20px;
            height:
20px;
            margin:
auto auto auto auto;
            border-radius:
11px;
            border:
2px solid rgba(30,30,30,0.05);
            background:
rgb(0, 173, 246); /* Old browsers */
           
box-shadow: 2px 0px 4px rgba(0,0,0,0.4);
        }
    </
style>
    <
script type="text/javascript">
       
//创建节点个数
       
function draw() {
       
var nodes = new vis.DataSet([
            {
id: 1, label: '',shape: 'image',title:'',image:'xt.png',size:30,physics:false,
               
fixed: {
                   
x:false,
                   
y:false,
                },
            },
            {
id: 2, label: 'Node 3'},
            {
id: 3, label: 'Node 3',shape:'circle',size:20,color:'gold',},
            {
id: 4, label: 'Node 4',},
            {
id: 5, label: 'Node 5',shape:'dot',size:0.1,color:'red',},
            {
id: 6, label: 'Node 6',shape:'dot',size:0.1,color:'red',},
            {
id: 7, label: 'Node 7',shape:'dot',size:0.1,color:'red',},
        ]);

       
//创建关系线,节点与哪个节点相连接

       
var edges = new vis.DataSet([
            {
from: 1, to: 2,title: '',length:200,color:{color:'Chocolate',},width:4},
            {
from: 2, to:3},
            {
from: 3, to: 4},
            {
from: 4, to: 5, arrows:{to:{enabled:false,},}},
            {
from: 5, to: 6,width:30,length:'30',
               
smooth:{//使关系线无弯曲
                   
enabled:true,//状态开启
                   
type:'continuous',
                   
forceDirection:'none',
                   
roundness:'0',
                },
               
arrows:{to:{enabled:false,},
                },
            },
            {
from: 6, to: 7},
        ]);

       
// 创建一个网状图
        
var container = document.getElementById('mynetwork');

       
//以vis格式使用数据
       
var data = {
           
nodes: nodes,
           
edges: edges
       
};
       
var options = {

           
nodes:{//节点控制
               
borderWidth: 5,//节点边框宽度设置
               
borderWidthSelected: 10,//点击选中时的宽度设置、
               
color: {
                   
border: 'red',//边框颜色的设置
               
},

            },

           
edges:{//关系线控制
               
width:2,//关系线宽度
               
arrows:{//箭头
                    
to:{enabled:true,//箭头是否显示、开启
                       
scaleFactor:0.5,//箭头的大小
                       
type:'arrow',//箭头的类型
                   
},
                },
               
font:{
                   
size:5,
                   
color:'green',
                   
align: 'horizontal',
                },
               
length:50,//关系线线长设置,数字较大最好以100位单位修改可看出差异
               
dashes:false,//关系线虚线
               
arrowStrikethrough: true,//关系线与节点处无缝隙


               
color: {
                    
color:'red',//关系线颜色设置
                   
highlight:'black',//点击关系线时的颜色
               
},

            },


        };


       
// 初始化网状图(把网状图实例化)
       
var network = new vis.Network(container, data, options);

       
network.on("stabilizationProgress", function(params) {
           
var maxWidth = 496;
           
var minWidth = 20;
           
var widthFactor = params.iterations/params.total;
           
var width = Math.max(minWidth,maxWidth * widthFactor);

           
document.getElementById('bar').style.width = width + 'px';
           
document.getElementById('text').innerHTML = Math.round(widthFactor*100) + '%';
        });

       
network.once("stabilizationIterationsDone", function() {
           
document.getElementById('text').innerHTML = '100%';
           
document.getElementById('bar').style.width = '496px';
           
document.getElementById('loadingBar').style.opacity = 0;
           
// really clean the dom element
           
setTimeout(function () {document.getElementById('loadingBar').style.display = 'none';}, 500);
        });}

       
//    network.on("afterDrawing", function (ctx) {//正方形嵌入圆形中
        //        var nodeId =1;
        //        var nodePosition = network.getPositions([nodeId]);
        //        ctx.strokeStyle = 'white';
        //        ctx.fillStyle = 'white';
        //        ctx.square(nodePosition[nodeId].x, nodePosition[nodeId].y,19);
        //        ctx.fill();
        //        ctx.stroke();
        //    });
   
</script>

</
head>


<
body οnlοad="draw()">


<
div id="wrapper">
    <
div id="mynetwork"></div>
    <
div id="loadingBar">
        <
div class="outerBorder">
            <
div id="text">0%</div>
            <
div id="border">
                <
div id="bar"></div>
            </
div>
        </
div>
    </
div>
</
div>
</
body>
</
html>

 

 

 

 

  • 12
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Visio浏览器是一个免费打开,阅读和打印任何Microsoft Office Visio(VSD)文。 这是一个快速,便捷的方式来阅读您电脑上的Visio文件。         Visio浏览器来自于FoxPDF软件公司。该浏览器能打开,显示文档Visio (VSD)和Rtf文件,而且还能显示TXT文件它完全不需要Microsoft Visio软件。它可以运行在Windows的桌面应用程序。它提供了多种查看选项。在您的常用应用程序中Visio浏览器能打印文档.         Visio浏览器不但能完全显示Visio文档而且突出之处是显示图像清晰,脆。Visio浏览器显示的菜单上,可以让我们平移和缩放,适合所有或宽度,旋转和镜像。         如果您需要查看Visio文件没有微软Visio软件,Visio浏览器是最好的选择。如果您的需求超过了它的功能,还有FoxPDF系列软让你选择。但这不是免费的,但它是最为便宜。 Visio浏览器关键功能 打开,阅读和打印Visio文档来自于FoxPDF Visio浏览器; 它完全不需要Microsoft软件。Visio浏览器能显示高品质的Visio文档(VSD, VSDX)等; 独立软件, 它不要Microsoft软件和Microsoft Visio; Visio浏览器支持可以打开,查看和打印高速; 支持的操作系统有 Windows 2000/xp/2003/Vista/2008/7/8等; 同时支持32位和64位系统; Visio浏览器易于使用,只需拖放打开,查看和打印Visio文件; 支持英语,法语,德语,意大利语,中文简体,中文繁体,日文等语言;
vis.js是一个用于可视化网络、图表和数据的JavaScript库。它提供了许多功能,包括节点移动和位置设置。以下是vis.js节点移动相关的教程。 1. 开始使用vis.js 在开始使用vis.js之前,需要引入vis.js库和vis.css样式文件。可以从vis.js官方网站上下载最新版本的vis.js库和vis.css样式文件,然后将它们添加到HTML页面中。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vis.js Node Move Tutorial</title> <link rel="stylesheet" type="text/css" href="vis.css"> <script type="text/javascript" src="vis.js"></script> </head> <body> <div id="network"></div> <script type="text/javascript"> // your code here </script> </body> </html> ``` 2. 创建一个简单的网络 在vis.js中,可以使用vis.Network对象来创建网络。首先,需要创建一个HTML元素,用于显示网络。然后,可以创建一个vis.Network对象,将其绑定到该HTML元素,并设置节点和边的数据。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vis.js Node Move Tutorial</title> <link rel="stylesheet" type="text/css" href="vis.css"> <script type="text/javascript" src="vis.js"></script> </head> <body> <div id="network"></div> <script type="text/javascript"> // create an array with nodes var nodes = new vis.DataSet([ {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'} ]); // create an array with edges var edges = new vis.DataSet([ {from: 1, to: 2}, {from: 1, to: 3}, {from: 2, to: 4}, {from: 2, to: 5} ]); // create a network var container = document.getElementById('network'); var data = {nodes: nodes, edges: edges}; var options = {}; var network = new vis.Network(container, data, options); </script> </body> </html> ``` 3. 启用节点移动 要启用节点移动,需要在创建vis.Network对象时,将moveable选项设置为true。可以通过options对象来设置该选项。 ```javascript var options = {moveable: true}; var network = new vis.Network(container, data, options); ``` 现在,可以在网络中拖动节点并放置到新的位置。 4. 监听节点移动事件 如果希望在节点移动时执行一些代码,可以监听节点移动事件。可以通过vis.Network对象的on方法来注册事件处理程序。 ```javascript network.on('dragEnd', function(params) { console.log(params.nodes[0] + ' was moved to x:' + params.pointer.canvas.x + ', y:' + params.pointer.canvas.y); }); ``` 上面的代码将在节点移动结束时,将节点ID和新位置的坐标输出到控制台。 5. 限制节点移动范围 有时,希望限制节点移动的范围,以保持网络的视觉效果。可以通过设置节点位置的边界来实现。可以在options对象中设置nodes选项,将其设置为一个对象,该对象包含每个节点的初始位置和位置边界。 ```javascript var options = { moveable: true, nodes: { 1: {x: 100, y: 100, fixed: true, borderWidth: 1, shape: 'circle', color: {background:'red', border:'black'}}, 2: {x: 300, y: 100, borderWidth: 1, shape: 'circle', color: {background:'blue', border:'black'}, physics: false}, 3: {x: 100, y: 300, borderWidth: 1, shape: 'circle', color: {background:'green', border:'black'}, physics: false}, 4: {x: 300, y: 300, borderWidth: 1, shape: 'circle', color: {background:'yellow', border:'black'}, physics: false} } }; ``` 如上所示,节点1的位置已经被固定,而其他节点则没有被固定,并且可以在屏幕上移动。 6. 停用节点移动 要停用节点移动,设置moveable选项为false即可。 ```javascript var options = {moveable: false}; var network = new vis.Network(container, data, options); ``` 现在,节点将无法移动。 以上就是vis.js节点移动相关的教程。通过这些教程,您可以在vis.js中轻松地启用节点移动,监听节点移动事件,限制节点移动范围,并停用节点移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值