plotly / plotly/react-plotly.js

OnClick callback receives incomplete data when using React.SetStateAction

未关闭
#358 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
1.1k
派生
138
平均合并
3 天 2 小时
30 天内合并 PR
4

描述

I'm writing an onClick event for my Pie chart and then using data provided in event.points[index] for my needs to create a custom popover. I noticed that when I click on the pie chart slice for the first time, I receive data including the properties percent and text, and both are numbers. percent represents a decimal percentage value, and the text shows the real value

{ 
...
  percent : 0.5773337926283155,
  pointNumber: 0,
  pointNumbers: [0],
  text: 1676,
  v: 1676,
  value: 1676,
... 
}

When I click just right after that on the other pie sector, I recieve the different data structure

{ 
...
  percent : undefined,
  pointNumber: 0,
  pointNumbers: [0],
  text: '57%',
  v: 1676,
  value: 1676,
... 
}

As you can see now, percent is undefined, and text is a formatted string with a percent unit. It also missing bbox props. I do not use it by I just noticed that.

In my onClick handlers, I'm changing the state of the React component to store the clicked point. Also, I have a popover outside click event (provided by Mantine), which is basically a document click listener, and I'm resetting my stored click point.

When I don't mutate the React component state and just console.log the event points in my onClick handler, the data is consistent and always contains the property percent, and the text is a number.

It seems like the React state update in my component triggers some Plotly rerender, which breaks the logic of computing the data.

From the code of plotly.js, I noticed that you are reusing hoverData, computing it, and sending it as a callback parameter. I decided to compare onClick and onHover events parameters. Comes up that the onHover handler always receives correct data and properties percent, bbox and text is a number, but just right after click this parameters are missing in onClick event.

Here is an example in Codepen where I managed to reproduce similar issues

https://codepen.io/graylog-max/pen/myPjYZN

I this example I have:

  • onCLickHandler it stores data from points to react.useStates and logs in console percent, bbox and text
  • onHoverHandler it stores data from points to react.useStates and logs in console percent, bbox and text
  • React.useEffect, which adds document.addEventListener to somehow mock Mantine onOutsideClick which also change the state

Here are scenarios of inconsistent behaviors depending on whether in some of these cases the state update is disabled. You can do that by just commented lines setClickPoint(ev.points[0]); , setHoverPoint(ev.points[0]); or whole useEffect

  1. All three handlers do not mutate state. No matter how many times we click or hover the data is consistent
Image Image
  1. only onCLickHandler mutating the state. The first click on the slice has all data, but when we click, just right after that, on the same slice again, the data doesn't contain all fields. If before clicking on the slice again we click somewhere outside of the chart, then the click data is fine. Hover data is always fine.
Image Image Image
  1. onCLickHandler enabled mutation and useEffect or onHoverHandler as well. Then onClick data is missing bbox and percent but hover is fine
Image Image
  1. If you put the brakepoint node_modules/plotly.js/src/traces/pie/plot.js line 471 inside the function sliceTop.on('click', function(pt) then everything works fine in any scenarion. I think that proves that computed data needs more time to be completed before getting to onClick callbacks.

I can fix my initial issue just by calculating the percent by myself, but this bug might be a sign of other potential issues, so it would be nice to find a solid fix. The bug relates to React state updates, which are happening very often.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 CodePen 复现开始,在启用 React 状态更新和 document click listener 的情况下比较 onClick 和 onHover 事件数据。检查 node_modules/plotly.js/src/traces/pie/plot.js 第 471 行附近的代码,并跟踪 click callback 接收其点数据的时机。完成标准是:React 重新渲染后重复点击时,始终包含 percent、bbox 和预期的数字文本。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, react
领域
data-visualization, frontend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。