我是 D3js 的新手,并且(可能是愚蠢地)正在探索它作为简单导航工具的价值。我已经设法拼凑出一个可以工作的基本页面,但它似乎相当冗长,我想知道是否有人有任何指示如何将它重新设计为更优雅并可能集成标签和圆形绘图功能?
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<script type="text/javascript" src="scripts/d3.v3.js"></script>
</head>
<body>
<div id="viz"></div>
<div id="status" style="position:fixed;bottom:0;left:0;
color:white;background-color:grey;font-size:small"></div>
<script type="text/javascript">
// functions for adding a 'status' box with link info
function addStatus(targetURL){document.getElementById('status').innerHTML=targetURL;}
function clearStatus(){document.getElementById('status').innerHTML=null;}
var sampleSVG = d3.select("#viz")
.append("svg")
.attr("width", 500)
.attr("height", 200);
var dataset = [[100, 100, "http://google.com/", "Google"],
[300, 100, "http://yahoo.com/", "Yahoo"]];
// Add labels
sampleSVG.selectAll("text")
.data(dataset)
.enter().append("text")
.attr("text-anchor", "middle")
.attr("x", function(d){return d[0]})
.attr("y", function(d){return d[1]})
.text(function(d) {return d[3];});
// Add circles and functionality
sampleSVG.selectAll("circle")
.data(dataset)
.enter().append("circle")
.style("fill", "transparent")
.style("stroke", "grey")
.style("stroke-width", 3)
.attr("r", 50)
.attr("cx", function(d){return d[0]})
.attr("cy", function(d){return d[1]})
.on("mouseover",
function(d){ d3.select(this).style("stroke", "black");
addStatus(d[2]); }
)
.on("mouseout",
function(){
d3.select(this).style("stroke", "grey");
clearStatus(); } )
.on("mouseup", function(d){window.location = d[2];});
</script>
<!-- ... -->
</body>
</html>