【问题标题】:Add names of the states to a map in d3.js在 d3.js 中将州名添加到地图中
【发布时间】:2012-12-03 13:37:11
【问题描述】:

我正在使用 abersUSA 投影来显示地图。 我想为每个州添加州名。

这是我尝试过的,我可以在源代码中看到状态的名称,但我没有看到它们呈现。 我做错了什么?

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];
    });

  });
}

【问题讨论】:

  • 为什么返回-path.centroid(d)[0];,应该是path.centroid(d)[0];

标签: javascript jquery svg d3.js


【解决方案1】:

任何想知道的人都可以,这就是我设法做到的:

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');


  });
}

【讨论】:

  • 我将如何返回状态代码并将其存储到变量中?我不一定需要显示任何名称,只需要获取州代码以便我可以将其用于其他事情。
【解决方案2】:

我对您自己提供的答案采取了类似的方法,但我不喜欢“质心”将所有州名放在哪里。例如,夏威夷、路易斯安那州、密歇根州和佛罗里达州。所以我为这些状态的 dx 和 dy 的状态信息添加了 json 数据的属性,并在绘图函数中添加了代码。

以下是一些修改状态的示例(已移除坐标以使其更小):

    {
        "geometry": { "type": "Polygon", "coordinates": [] },
        "type": "Feature",
        "id": "12",
        "properties": { "name": "Florida", "abbr": "FL", "dx": "1em" }
    },
    {
        "geometry": { "type": "MultiPolygon", "coordinates": [] },
        "type": "Feature",
        "id": "15",
        "properties": { "name": "Hawaii", "abbr": "HI", "dx": "1.15em", "dy": "1.25em" }
    },

这里是绘制标签的函数部分:

        g.selectAll("text")
            .data(json.features)
            .enter()
            .append("text")
            .attr("transform", function (d) { return "translate(" + path.centroid(d) + ")"; })
            .attr("dx", function (d) { return d.properties.dx || "0"; })
            .attr("dy", function (d) { return d.properties.dy || "0.35em"; })
            .text(function (d) { return d.properties.abbr; });

【讨论】:

  • 你有没有机会在某个地方的 git repo 中有这个 json 数据以供重用?
  • 这是我当时工作的地方的内部代码。不过,状态 json 数据中没有任何敏感信息,所以我确实有。它不在公共 repo 上,但这里是状态 json 数据,然后是我们 MVC Razor 视图中使用它的 sn-p(如果您不使用 ASP.NET MVC,则需要大量编辑该部分) .糟糕,无法将整个文件粘贴到此处。我得把它放在某个地方。
猜你喜欢
  • 2013-12-02
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多