如何在React中渲染svg.node()对象?

发布于 2025-02-04 08:37:04 字数 1603 浏览 1 评论 0原文

我正在制作D3 Circle Pack布局。

    const BubbleChart = ({ bubblesData, height, width }: BubbleChartProps) => {
    
      const packData = (size: any) => {
        return d3.pack().size(size).padding(3);
      }
    
      const makeHierarchy = () => {
        return d3.hierarchy({ children: bubblesData }).sum((d:any) => d.count);
      }
    
      const makeBubbles = () => {
        const hierachalData = makeHierarchy();
        const packLayout = packData([width - 2, height - 2]);
        const root = packLayout(hierachalData);
    
        const svg = d3.create("svg")
        .attr("width", width)
        .attr("height", height);
    
        const leaf = svg.selectAll("g")
          .data(root.leaves())
          .enter().append("g")
            .attr("transform", d => `translate(${d.x + 1},${d.y + 1})`);
    
        leaf.append("circle")
        .attr("r", d => {
          const minValue = d3.min(bubblesData, (d) => d.count)
          const maxValue = d3.max(bubblesData, (d) => d.count)
          let scale = d3.scaleSqrt()
            .domain([0, maxValue as unknown as number])
            .range([0,d.r]);
          return scale(minValue as unknown as number);
          })
          .attr("fill", "#d9edf7")
  
        return svg.node();
      }
    
      return (
        <div id="chart">{makeBubbles()}</div>
      )
    }
    
 export default BubbleChart

问题是我无法渲染svg.node(),因为pf svg.node()是对象。我要低于错误:

对象无效,因为React子女(发现:[对象SVGSVGELEMENT])。如果您打算渲染一个孩子的集合,请改用数组。

有没有办法渲染的方法?

I am making a D3 circle pack layout.

    const BubbleChart = ({ bubblesData, height, width }: BubbleChartProps) => {
    
      const packData = (size: any) => {
        return d3.pack().size(size).padding(3);
      }
    
      const makeHierarchy = () => {
        return d3.hierarchy({ children: bubblesData }).sum((d:any) => d.count);
      }
    
      const makeBubbles = () => {
        const hierachalData = makeHierarchy();
        const packLayout = packData([width - 2, height - 2]);
        const root = packLayout(hierachalData);
    
        const svg = d3.create("svg")
        .attr("width", width)
        .attr("height", height);
    
        const leaf = svg.selectAll("g")
          .data(root.leaves())
          .enter().append("g")
            .attr("transform", d => `translate(${d.x + 1},${d.y + 1})`);
    
        leaf.append("circle")
        .attr("r", d => {
          const minValue = d3.min(bubblesData, (d) => d.count)
          const maxValue = d3.max(bubblesData, (d) => d.count)
          let scale = d3.scaleSqrt()
            .domain([0, maxValue as unknown as number])
            .range([0,d.r]);
          return scale(minValue as unknown as number);
          })
          .attr("fill", "#d9edf7")
  
        return svg.node();
      }
    
      return (
        <div id="chart">{makeBubbles()}</div>
      )
    }
    
 export default BubbleChart

The issue is I am not able to render svg.node() as the type pf svg.node() is Object. I am getting below error:

Objects are not valid as a React child (found: [object SVGSVGElement]). If you meant to render a collection of children, use an array instead.

Is there a way I can render this?

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

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

发布评论

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

评论(1

不即不离 2025-02-11 08:37:05

也遇到了这个问题。该解决方案是使用REF对HTML元素,并使用Ref.Current.AppendChild(SVGOBject)。

在没有危险的js reect JS中svgsvggelement,而无需

危险

svgRef.current.replaceWith(svgObj);
return (
  <div>
    <svg ref={svgRef} />
  </div>
)

Also ran into this problem. The solution is to use a ref to an html element and use ref.current.appendChild(svgObject).

Render SVGSVGElement in React JS without dangerouslySetInnerHtml

Another alternative

svgRef.current.replaceWith(svgObj);
return (
  <div>
    <svg ref={svgRef} />
  </div>
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文