英文:
How to add click handler to D3 element in React
问题
下面是你提供的代码,其中我只翻译了注释部分:
import { useEffect } from "react";
import * as d3 from "d3";
export default function App() {
useEffect(() => {
const svg = d3.select("#svg");
svg
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 300)
.attr("height", 300)
.attr("fill", "red")
.attr("id", "red-rect");
const redRect = d3.select("#red-rect");
redRect.on("mouseenter", () => {
console.log("working");
});
}, []);
return <svg id="svg" />;
}
上述的代码无法正常工作,可以为整个svg
注册事件,但无法为rect
注册事件。以下代码是有效的:
svg.on("mouseenter", () => {
console.log("working");
});
这是一个简化的示例。在实际项目中,你可能会在同一个svg
内创建多个rect
。
奇怪的是,如果我在return
语句内创建rect
,我可以注册鼠标监听器。但是我不想使用React的return
来创建元素,我想专门使用D3来创建元素。
export default function App() {
useEffect(() => {
const greenRect = d3.select("#green-rect");
greenRect.on("mouseenter", () => {
console.log("working");
});
}, []);
return (
<svg id="svg">
<rect x={0} y={0} width={300} height={300} fill="green" id="green-rect" />
</svg>
);
}
如何在我的svg
上附加rect
(或其他元素)并在它们上使用鼠标事件呢?
英文:
I have the below code that appends a rect
into an svg
. I need that rect
to handle click events such as click
and mouseenter
.
import { useEffect } from "react";
import * as d3 from "d3";
export default function App() {
useEffect(() => {
const svg = d3.select("#svg");
svg
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 300)
.attr("height", 300)
.attr("fill", "red")
.attr("id", "red-rect");
const redRect = d3.select("#red-rect");
redRect.on("mouseenter", () => {
console.log("working");
});
}, []);
return <svg id="svg" />;
}
The above does not work. I can register the entire svg
but not the rect
. The below works:
svg.on("mouseenter", () => {
console.log("working");
});
This is a minimalistic example. In the real project I'll be creating several rects inside the same svg
.
The strange thing is that if I create a rect
inside the return
statement, I can register the mouse listener. But I don't want to use React return to create elements. I want to create elements exclusively with D3.
export default function App() {
useEffect(() => {
const greenRect = d3.select("#green-rect");
greenRect.on("mouseenter", () => {
console.log("working");
});
}, []);
return (
<svg id="svg">
<rect x={0} y={0} width={300} height={300} fill="green" id="green-rect" />
</svg>
);
}
How can I append rects
(or other elements) on my svg
and use mouse events on them?
Here's a fiddle
Thanks
答案1
得分: 1
在附加元素时,请为选择指定一个名称:
useEffect(() => {
const svg = d3.select("#svg");
const redRect = svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 300)
.attr("height", 300)
.attr("fill", "red")
.attr("id", "red-rect");
redRect.on("mouseenter", () => {
console.log("working");
});
}, []);
这是分叉的代码沙箱。
英文:
When appending your elements, give the selection a name:
useEffect(() => {
const svg = d3.select("#svg");
const redRect = svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 300)
.attr("height", 300)
.attr("fill", "red")
.attr("id", "red-rect");
redRect.on("mouseenter", () => {
console.log("working");
});
}, []);
Here's the forked sandbox.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论