D3泰森多邊形示例代碼
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.triangles {
fill: none;
}
.links {
stroke: #000;
}
.sites {
fill: #000; //黑色
stroke: #fff; //白色
}
.triangles .primary {
fill: #f00;
}
.links .primary {
stroke: #fff;
}
.sites :first-child {
fill: #fff;
}
</style>
<svg width="960" height="500"></svg>
<!--
<script src="https://d3js.org/d3.v4.min.js"></script>
-->
<script src="./d3_5.14.2.min.js"></script>
<script>
var svg = d3.select("svg")
width = +svg.attr("width"),
height = +svg.attr("height");
debugger
//生成隨機的座標點
var sites = d3.range(10)
.map(function(d) { return [Math.random() * width, Math.random() * height]; });
/**
var sites =[
[267.9209975005688, 379.3751254981226],
[943.7398333408541, 418.65866999340903],
[479.52144481621383, 380.00693430254785],
[187.47809023912828, 38.27165834711255],
[405.9934965423606, 26.394513050200462],
[510.66798177187104, 98.63893482407582],
[577.1853032467536, 126.1476827753022],
[116.91624923957548, 456.1918205970447],
[62.400157804006184, 258.10840657921386],
[241.32040003222505, 447.48491825904756]
];
**/
var voronoi = d3.voronoi();
//這裏在這個示例裏面沒用可以刪除
var triangle = svg.append("g")
.attr("class", "triangles")
.selectAll("path")
.data(voronoi.triangles(sites))
.enter().append("path")
debugger
//畫線
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(voronoi.links(sites))
.enter().append("line")
.call(redrawLink);
//.attr("r", 5.5) 設置頂點大小
function redrawLink(link) {
link
// .classed("primary", function(d) { return d.source === sites[0] || d.target === sites[0]; })
.attr("x1", function(d) { return d.source[0]; })
.attr("y1", function(d) { return d.source[1]; })
.attr("x2", function(d) { return d.target[0]; })
.attr("y2", function(d) { return d.target[1]; });
}
</script>
界面效果