我是D3.js的新手。我试图想象一下美国的社交网络 地图。它基本上包括20个塔和它们之间的连接。现在 我可以在每个塔上绘制圆圈,但路径(直线将是 罚款)因某些原因无法看到。
我将代码附加到此 信息。真诚地感谢您的帮助!
路径数据采用csv格式,每行包含id, 原始塔和目的地塔的经度和纬度。
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<style type="text/css">
#states path {
fill: #ccc;
stroke: #fff;
}
path.arc {
pointer-events: none;
fill: none;
stroke: #fff;
}
path.cell {
fill: none;
pointer-events: all;
}
circle {
fill: steelblue;
fill-opacity: .8;
stroke: #fff;
}
</style>
</head>
<body>
<script type="text/javascript" src="../../d3.v2.js"></script>
<script type="text/javascript" src="../../d3.csv.js"></script>
<script type="text/javascript" src="../../d3.geo.js"></script>
<script type="text/javascript" src="../../d3.geom.js"></script>
<script type="text/javascript">
var w = 1280,
h = 800;
var projection = d3.geo.azimuthal()
.mode("equidistant")
.origin([-98, 38])
.scale(1400)
.translate([640, 360]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").insert("svg:svg", "h2")
.attr("width", w)
.attr("height", h);
var states = svg.append("svg:g")
.attr("id", "states");
var circles = svg.append("svg:g")
.attr("id", "circles");
d3.json("../data/us-states.json", function(collection) {
states.selectAll("path")
.data(collection.features)
.enter().append("svg:path")
.attr("d", path);
});
d3.csv("K_cdr.csv", function(callls) {
var countByTower = {},
locationByTower = {},
positions = [];
callls.forEach(function(calll) {
var origin = calll.tower_orig,
destination = calll.tower_dest;
countByTower[origin] = (countByTower[origin] || 0) + 1;
countByTower[destination] = (countByTower[destination] || 0) + 1;
});
d3.csv("K_Towers.csv", function(towers) {
var location = [];
towers.map(function(tower) {
location = [+tower.lat, +tower.lon];
locationByTower[tower.ID] = location;
positions.push(projection(location));
});
circles.selectAll("circle")
.data(towers)
.enter().append("svg:circle")
.attr("cx", function(d, i) { return positions[i][0]; })
.attr("cy", function(d, i) { return positions[i][1]; })
.attr("r", function(d, i) { return
Math.sqrt(countByTower[d.ID])/2; });
svg.selectAll("circle.node")
.data(towers)
.enter().append("svg:text")
.attr("class", "node")
.attr("x", function(d,i) { return positions[i][0]; })
.attr("y", function(d,i) { return positions[i][1]; })
.text(function(d,i) {return d.ID;});
});
svg.selectAll("path")
.data(callls)
.enter().append("path")
.attr("d", function(d,i) {
var orig = projection([d.tower_orig_lat[i],
d.tower_orig_lon[i]]);
var dest = projection([d.tower_dest_lat[i], d.tower_dest_lon[i]]);
return ("M"+ orig[0]+" "+orig[1]+" L "+ dest[0] +" "+ dest[1] + "
z;"); });
});
</script>
</body>
</html>