Add names of the states to a map in d3.js

后端 未结 2 1040

I am using albersUSA projection to display a map. I want to add the name of the states to each state.

This is what I tried, and i can see the names of the states in

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 09:18

    OK for anyone wondering, this is how I managed to do it:

    function draw(){
    
      d3.json("readme.json", function(json) {
        g.selectAll("path")
        .data(json.features)
        .enter()
        .append("path")
        .attr("d", path)
        .on("click", click);
    
        g.selectAll("text")
        .data(json.features)
        .enter()
        .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];
        })
        .attr("text-anchor","middle")
        .attr('font-size','6pt');
    
    
      });
    }
    

提交回复
热议问题