【问题标题】:Why is the 'fill' attribute colouring the entire svg area instead of only the path? [duplicate]为什么“填充”属性会为整个 svg 区域着色而不仅仅是路径? [复制]
【发布时间】:2021-09-05 12:19:43
【问题描述】:

我已经使用 d3.js 创建了一个地图投影,在“填充”设置为“无”的情况下,它一开始可以正常工作。

d3.js 无填充投影:

但是,当我尝试将路径“填充”属性设置为颜色时,它会创建填充整个 svg 并与其他路径重叠的区域。

带填充的ds.js投影:

该项目的代码如下。我认为也许路径没有关闭,但我似乎无法解决问题所在。任何帮助将不胜感激。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Map of Wales</title>
    <style></style>
  </head>
  <body>
    <script src="https://d3js.org/d3.v7.min.js"></script>
    <script>
      let width = 600,
        height = 600;

      d3.select("body")
        .append("svg")
        .style("width", width)
        .style("height", height);

      let projection = d3
        .geoMercator()
        .translate([width / 2, height / 2])
        .center([-3.7, 52.4])
        .scale([width / 0.06]);

      let geoGenerator = d3.geoPath().projection(projection);

      d3.json("https://opendata.arcgis.com/datasets/50d3f1363f144a94bde9353520697207_0.geojson").then((wales) => {
        d3.select("svg")
          .append("g")
          .selectAll("path")
          .data(wales.features)
          .enter()
          .append("path")
          .attr("d", geoGenerator)
          .attr("stroke", "#000000")
          .attr("fill", "blue")
          .attr("fill-opacity", 0.2)
          .attr("id", (d) => d.properties.nawc18cd)
          .attr("class", "constituency");
      });
    </script>
  </body>
</html>

【问题讨论】:

标签: javascript d3.js


【解决方案1】:

这个问题可以通过在this answer中描述的回退geojson数据来解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2016-07-16
    • 2014-08-24
    • 2014-05-02
    相关资源
    最近更新 更多