reactjs - 为什么 D3 Brush 不清除 React App 中的多个图形
问题描述
我在同一个 React 组件中的不同元素的同一页面上显示了多个图表。每个图表使用相同的代码位,但只有第一个图表在选择后清除画笔。js
所有图表在没有 React的常规文件中都能正常工作。
const plotArea = async (props: any) => {
...
// Handler for the end of a brush event from D3.
const brushEnded = (event: any) => {
const s = event.selection;
// Consume the brush action
if (s) {
d3.select('.brush').call(brush.move, null);
}
}
// Create a brush for selecting regions to zoom on.
const brush: any = d3
.brushX()
.extent([
[0, 0],
[width, height - 1],
])
.on('end', brushEnded);
// Zoom brush
svg.append('g').attr('class', 'brush').call(brush);
}
useEffect(() => {
// plotArea() occurs for each graph, there are multiple graphs
plotArea(...);
...
}, []);
解决方案
当 d3 在 中运行选择时d3.select('.brush').call(brush.move, null);
,它不会将搜索限制在组件中。它将在整个文档中搜索一个.brush
元素,并在找到第一个元素后立即停止。
作为快速修复,您可以保存组件的特定画笔,这样您就已经有了参考,并且不需要运行 ad3.select
来取回它:
const plotArea = async (props: any) => {
...
// Create brushElement first
const brushElement = svg.append('g').attr('class', 'brush');
// Handler for the end of a brush event from D3.
const brushEnded = (event: any) => {
const s = event.selection;
// Consume the brush action
if (s) {
brushElement.call(brush.move, null); // Now uses the variable
}
}
// Create a brush for selecting regions to zoom on.
const brush: any = d3
.brushX()
.extent([
[0, 0],
[width, height - 1],
])
.on('end', brushEnded);
brushElement.call(brush);
}
推荐阅读
- angular - Ionic5 angular9 vendor-es2015.js ERROR [object Object]
- django - Inherit from abstract class Django
- android - 改造界面如何添加cookie来存储数据
- architecture - Right architecture for Authentication and Authorization with IdentityServer4
- android - Xamarin forms Android SurfaceView - still video image left behind
- vba - Powerpoint Macros: Column(unknown member) error at Columns.Width Formatting code
- angular - 只调用一次切换地图后点击
- python - 如何重新启动我的 Python Arcade 游戏以使其在游戏结束后正常运行?
- php - Prestashop 1.7.6 覆盖模块 src 文件夹中的类的正确方法
- javascript - Firebase Messaging Web getToken 在正常工作 1-2 天后返回过期令牌(无法删除)