Tôi đang sử dụng phép chiếu albersUSA để hiển thị bản đồ. Tôi muốn thêm tên của các tiểu bang cho mỗi tiểu bang.Thêm tên của các tiểu bang vào bản đồ trong d3.js
Đây là những gì tôi đã thử và tôi có thể thấy tên của các trạng thái trong nguồn, nhưng tôi không thấy chúng được hiển thị. Tôi đang làm gì sai?
var width = 1060,
height = 600,
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", click)
.on("mousemove", mousemove);
var g = svg.append("g")
.attr("transform", "translate(" + width/2 + "," + height/2 + ")")
.append("g")
.attr("id", "states");
var projection = d3.geo.albersUsa()
.scale(width)
.translate([0, 100]);
var path = d3.geo.path()
.projection(projection);
draw();
function draw(){
d3.json("readme.json", function(json) {
g.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.append("svg:text")
.text(function(d){
return d.properties.name;
})
.attr("x", function(d){
return -path.centroid(d)[0];
})
.attr("y", function(d){
return -path.centroid(d)[1];
});
});
}
Tại sao bạn trả về '-path.centroid (d) [0];', nó phải là 'path.centroid (d) [0];'? – Duopixel