D3 添加经纬网和地理



当先添加D3经纬网然后调用d3.json时,json中的第一个要素不会显示在地图上。如果我在附加路径后在 d3.json 中添加 d3.graticule,则 graticule 将绘制在功能之上,这不是我想要的。这是我的JavaScript文件:

$(document).ready(function (){
$('.aToolTip').tooltip();
$('.aPopOver').popover();
// D3 Mapping Starts Here
var h = $('#myMap').height();
var w = $('#myMap').width();
console.log("h = " + h , ",  w = " + w);
$('svg').attr('width', w)
        .attr('height', h);
var aSVG = d3.select("#aSVGElement");
var aProjection = d3.geo.mercator()
                    .rotate([-135, 24])
                    .scale(600)
                    .translate([w/2, h/2]);
var path = d3.geo.path()
                .projection(aProjection);
var graticule = d3.geo.graticule()
                    .step([10, 10]);
aSVG.append("path")
        .datum(graticule)
        .attr("class", "graticule")
        .attr("d", path);
d3.json("js/Australia.json", function(error, json){
    aSVG.selectAll("path")
        .data(json.features)
        .enter()
        .append("path")
        .attr("d", path);    
}); // end d3.json
});// end document ready 

如果您先绘制网格,则不会显示第一个功能,因为您正在选择path,网格是其中之一。这意味着第一个数据元素(即第一个特征)与现有路径匹配,而不是.enter()选择的一部分。

要解决此问题,只需将类分配给您的要素path并相应地选择:

aSVG.selectAll("path.feature")
    .data(json.features)
    .enter()
    .append("path")
    .attr("class", "feature")
    .attr("d", path); 

最新更新