如何在React组件的内部渲染D3.js映射?

发布于 2025-01-30 16:34:04 字数 1663 浏览 2 评论 0原文

我正在尝试在React组件的内部渲染D3地图,但结果是地图的微小表示。当我在React之外尝试此代码时,它可以很好地工作。

我正在使用基于类的组件。 在这里,任何帮助都将不胜感激的

是代码:

componentDidMount() {
    

    var height = 800,
      width = 800,
      projection = d3.geoMercator(), 
      nyc = void 0,
      map;

    var path = d3.geoPath().projection(projection);

    var svg = d3
      .select(this.myRef.current)
      .append("svg")
      .attr("id", "MapSVG_")
      .attr("width", width)
      .attr("height", height)
      .style("position", "relative")
      .style("opacity", "0.8")
      .style("overflow", "visible");

    const districts = feature(boroughsData, boroughsData.objects.districts);


    projection.scale(1).translate([0, 0]);
    const b = path.bounds(districts);
    const s =
      0.85 /
      Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height);
    const t = [
      (width - s * (b[1][0] + b[0][0])) / 2,
      (height - s * (b[1][1] + b[0][1])) / 2
    ];
    projection.scale(s).translate(t);
    map = svg
      .append("g")
      .attr("class", "boundary")
      .attr("x", 300);
      
    nyc = map.selectAll("path").data(districts.features);
    nyc
      .enter()
      .append("path")
      .attr("d", path)
      .attr("class", "nycDistrict")
      .style("stroke", "rgb(226, 252, 255)")
      .style("cursor", "pointer")
      .attr("stroke-width", 0.7)
      .attr("fill", "#2D4859");
    nyc.attr("fill", "#eee");
    nyc.exit().remove();
    
}

小点是地图,它的大小应与容器相同。

I am trying to render a d3 map inside of a React component but the result is a tiny representation of the map. When I try this code outside of React, it works well.

I am using a class-based component. Any help will be greatly appreciated

Here is the code:

componentDidMount() {
    

    var height = 800,
      width = 800,
      projection = d3.geoMercator(), 
      nyc = void 0,
      map;

    var path = d3.geoPath().projection(projection);

    var svg = d3
      .select(this.myRef.current)
      .append("svg")
      .attr("id", "MapSVG_")
      .attr("width", width)
      .attr("height", height)
      .style("position", "relative")
      .style("opacity", "0.8")
      .style("overflow", "visible");

    const districts = feature(boroughsData, boroughsData.objects.districts);


    projection.scale(1).translate([0, 0]);
    const b = path.bounds(districts);
    const s =
      0.85 /
      Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height);
    const t = [
      (width - s * (b[1][0] + b[0][0])) / 2,
      (height - s * (b[1][1] + b[0][1])) / 2
    ];
    projection.scale(s).translate(t);
    map = svg
      .append("g")
      .attr("class", "boundary")
      .attr("x", 300);
      
    nyc = map.selectAll("path").data(districts.features);
    nyc
      .enter()
      .append("path")
      .attr("d", path)
      .attr("class", "nycDistrict")
      .style("stroke", "rgb(226, 252, 255)")
      .style("cursor", "pointer")
      .attr("stroke-width", 0.7)
      .attr("fill", "#2D4859");
    nyc.attr("fill", "#eee");
    nyc.exit().remove();
    
}

The tiny dot is the map, and it should be of the same size as its container.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2025-02-06 16:34:04

一个简单的

   .fitSize([width, height], geoData)

D3.GeomerCator()修复了所有内容

A simple

   .fitSize([width, height], geoData)

Under d3.geoMercator() fixed everything

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文