如何在React中渲染svg.node()对象?
我正在制作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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也遇到了这个问题。该解决方案是使用REF对HTML元素,并使用Ref.Current.AppendChild(SVGOBject)。
在没有危险的js reect JS中svgsvggelement,而无需
危险
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