可視化工具D3.js教程 入門 (第十二章)—— 力導向圖


力導向圖的概念這里就不細說了,力導向圖適合繪制關系型的信息;

下面先來說幾個關於力導向圖的知識點:

1、d3.forceSimulation([nodes])

  創建一個新的力導向圖;

2、simulation.force(name[, force])

  如果指定了 force 則表示添加指定 name 的 force(力學模型)並返回仿真,如下:

  var simulation = d3.forceSimulation(nodes).force("charge", d3.forceManyBody());

  如果要移除對應的 name 的仿真,可以為其指定 null,如下:

  simulation.force("charge", null);

 3、simulation.nodes([nodes])

  如果指定了 nodes 則將仿真的節點設置為指定的對象數組,並根據需要創建它們的位置和速度,然后 重新初始化並且綁定 force 返回當前仿真。如果沒有指定 nodes 則返回當前仿真的節點數組。

4、d3.forceLink([links]) 

  根據指定的 links 以及默認參數創建一個彈簧力模型。如果沒有指定 links 則默認為空數組。

5、link.links([links])

  如果指定了 links 則將其設置為該彈簧力模型的關聯邊數組,並重新計算每個邊的 distance 和 strength 參數,返回當前力模型。如果沒有指定 links 則返回當前力模型的邊數組,默認為空。

6、simulation.tick()

  簡單來講就是讓力導向圖隨時更新並且動起來的。

7、d3.drag() 

  創建一個新的拖拽行為並返回自身。drag 既是一個對象,也是一個函數,通常通過 selection.call 被應用在選中的元素上。

  d3.selectAll(".node").call(d3.drag().on("start", started));

 

 

有了這些新知識 我么開始繪圖

代碼:

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>force</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>

<svg height="400" width="500"></svg>

</body>

<script>

//數據
var nodes = [//節點集
{name:"湖南邵陽"},
{name:"山東萊州"},
{name:"廣東陽江"},
{name:"山東棗庄"},
{name:"趙麗澤"},
{name:"王恆"},
{name:"張欣鑫"},
{name:"趙明山"},
{name:"班長"}
];
var edges = [//邊集
{source:0,target:4,relation:"籍貫",value:1.3},
{source:4,target:5,relation:"舍友",value:1},
{source:4,target:6,relation:"舍友",value:1},
{source:4,target:7,relation:"舍友",value:1},
{source:1,target:6,relation:"籍貫",value:2},
{source:2,target:5,relation:"籍貫",value:0.9},
{source:3,target:7,relation:"籍貫",value:1},
{source:5,target:6,relation:"同學",value:1.6},
{source:6,target:7,relation:"朋友",value:0.7},
{source:6,target:8,relation:"職責",value:2}
];

var margin = 30;//邊距
var svg = d3.select('svg');
var width = svg.attr('width');
var height = svg.attr('height');

//創建一個分組 並設置偏移
var g = svg.append('g').attr('transform','translate('+ margin +','+ margin +')');


//新建一個顏色比例尺
var scaleColor = d3.scaleOrdinal()
.domain(d3.range(nodes.length))
.range(d3.schemeCategory10);

//新建一個力導向圖
var forceSimulation = d3.forceSimulation()
.force("link",d3.forceLink())
.force("charge",d3.forceManyBody())
.force("center",d3.forceCenter());


//生成節點數據
forceSimulation.nodes(nodes)
.on('tick',linksTick);//這個函數下面會講解


//生成邊數據
forceSimulation.force('link')
.links(edges)
.distance(function (d,i) {
return d.value*100;//設置邊長
});

//設置圖形 中心點
forceSimulation.force('center')
.x(width/2)//設置x坐標
.y(height/2)//設置y坐標

//再來看下頂點數據 和 邊數據
console.log(nodes);
console.log(edges);


//繪制邊 這里注意一下繪制順序 在d3中 各元素是有層級關系的,先繪制的在下面
var links = g.append('g')
.selectAll('line')
.data(edges)
.enter()
.append('line')
.attr('stroke',function (d,i) {
return scaleColor(i);//設置邊線顏色
})
.attr('storke-width','1');//設置邊線寬度


//繪制邊上的文字
var linksText = g.append('g')
.selectAll('text')
.data(edges)
.enter()
.append('text')
.text(function (d,i) {
return d.relation;
});


//創建節點分組
var gs = g.selectAll('.circle')
.data(nodes)
.enter()
.append('g')
.attr('transform',function (d,i) {
return 'translate('+ d.x +','+ d.y +')'
})
.call(
d3.drag()//相當於移動端的拖拽手勢 分以下三個階段
.on('start',start)
.on('drag',drag)
.on('end',end)
);

//繪制節點
gs.append('circle')
.attr('r',10)
.attr('fill',function (d,i) {
return scaleColor(i);
});

//繪制文字
gs.append('text')
.text(function (d,i) {
return d.name;
});





function linksTick(){
links
.attr("x1",function(d){return d.source.x;})
.attr("y1",function(d){return d.source.y;})
.attr("x2",function(d){return d.target.x;})
.attr("y2",function(d){return d.target.y;});

linksText
.attr("x",function(d){
return (d.source.x+d.target.x)/2;
})
.attr("y",function(d){
return (d.source.y+d.target.y)/2;
});

gs && gs.attr('transform',function (d,i) {
return 'translate('+ d.x +','+ d.y +')';
})

}


function start(d){
if(!d3.event.active){//event.active 屬性對判斷並發的拖拽手勢序列中的 start 事件和 end 事件: 在拖拽手勢開始時為0,在拖拽結束最后一個手勢事件時為0
//這里就是drag的過程中
forceSimulation.alphaTarget(0.8).restart();//設置衰減系數,對節點位置移動過程的模擬,數值越高移動越快,數值范圍[0,1]
}
d.fx = d.x;
d.fy = d.y;
}

function drag(d){
d.fx = d3.event.x;
d.fy = d3.event.y;
}

function end(d) {
if (!d3.event.active) {
forceSimulation.alphaTarget(0);
}
d.fx = null;
d.fy = null;
}


</script>

</html>

 

效果:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM