Plotly.js Sunburst图表状态变化确定

huangapple go评论67阅读模式
英文:

Plotly.js Sunburst chart state change determination

问题

我有一个React应用,其中我正在使用plotly.js来显示一个日晷图。我有其他操作/组件,这些操作/组件根据日晷图的“缩放状态”而变化。例如,如果我完全缩小,日晷图显示4个同心圆。如果我点击距离中心第二个圆,它会放大,然后有3个同心圆。我曾希望能够从单击处理程序中确定状态,但似乎不太直接。

我感兴趣的状态基本上是可见的事物以及深度(有多少个同心圆)。

我希望能找到一种反映状态的回调或类似的东西,但我找到的只是日晷图的点击处理程序(参考 https://github.com/plotly/react-plotly.js/blob/master/README.md#event-handler-props - onSunburstClick, https://community.plotly.com/t/capturing-sunburst-on-click-events/32171

问题在于,很难根据点击来推断状态。点击一个部分以不同的方式更改状态。如果我点击最外部的部分,它根本不会改变状态。如果我点击最内部的圆,它可能会根据当前显示在最内部圆中的部分放大或缩小。

有人基于此代码在codepen上有一个示例:

Plotly.newPlot('graph', [{
  type: 'sunburst',
  parents: ['', 'Root', 'Root', 'A'],
  labels: ['Root', 'A', 'B', 'a']
}]).then(gd => {
  gd.on('plotly_sunburstclick', console.log)
})

如果你前往那里并点击“A”,你将进入“A”是中心圆,“a”是外圆的状态。如果再次点击“A”,则中心圆将变为“root”。对于点击处理程序来说,这两次点击看起来基本相同,但产生的状态(这是我关心的部分)是不同的。我已经尝试了一些启发式方法来推断它是放大还是缩小,但我尝试的所有方法都不能始终正常工作。

我已经寻找了解决方案,但到目前为止,我找到的最接近的东西是点击处理程序,经过一番努力也无法解决我的问题。如果已经有一个答案,那将很好,但请不要太快将问题标记为重复,而不确定它是否已经解决。我看到有很多问题被标记为重复,但所提到的答案实际上并没有解决新问题,同时讨论和尝试回答实际问题的行为被有效地关闭。

我还应该补充一下,我不一定要使用plotly.js,如果有其他适用于React的库可以解决我的问题,我愿意听取建议。

英文:

I have a react app where I'm using plotly.js to display a sunburst chart. I have other actions/components that change depending on the "zoom state" of the sunburst chart. E.g. if I'm all the way zoomed out, the burst chart shows 4 concentric circles. If I click on the second circle from the center, it zooms in and there are 3 concentric circles. I thought that I could determine the state from the click handler, but that doesn't seem to be straightforward.
The state I'm interested in is essentially what things are visible, and what the depth is (how many concentric circles).

I'd hoped to find some sort of call back or something that would reflect the state, but all I've found is a click handler for sunburst chart (cf https://github.com/plotly/react-plotly.js/blob/master/README.md#event-handler-props - onSunburstClick, https://community.plotly.com/t/capturing-sunburst-on-click-events/32171)

The problem is, it's hard to deduce the state based on the click. Clicking on a segment changes the state in different ways. If I click on the outmost segment, it doesn't change the state at all. If I click on the innermost circle, it could zoom out or in, depending on the segments currently shown in the innermost circle.

Someone has an example on codepen based on this code:

Plotly.newPlot('graph', [{
  type: 'sunburst',
  parents: ['', 'Root', 'Root', 'A'],
  labels: ['Root', 'A', 'B', 'a']
}]).then(gd => {
  gd.on('plotly_sunburstclick', console.log)
})

If you go there and click on 'A', you end up in the state where 'A' is the center circle, and 'a' is the outer circle. If you click on 'A' again, then you end up with 'root' as the center circle. To the click handler, both clicks look essentially the same, but the resulting state (which is the part I care about) is different. I've tried some heuristics in my case to deduce whether it's zooming in or out, but nothing I've tried works consistently.

I've searched for solutions, but so far the closest thing I've found is the click handler, which after some effort doesn't solve my issue. If there's already an answer, that'd be great, but please don't be too quick mark a question as duplicate without being sure that it really is already solved. I've seen a lot of questions marked duplicate where the referred answer doesn't actually address the new question, meanwhile discussion and any attempts to answer the actual question are effectively shut down.

I should add that I'm not necessarily wedded to using plotly.js, if there are other react friendly libraries that would solve my problem, I'm open to suggestions.

答案1

得分: 1

在这种情况下,单击事件最终触发类似于"开关"的操作。

默认情况下,我们看到根节点和层次结构的所有子节点。

  • 单击至少有一个子节点的环(如'A')会过滤掉其父级('Root')和兄弟姐妹('B'),并将该环在视觉上设置为新的根节点,我们只看到该环下的层次结构(即,环过滤开关打开,并且事件数据中的属性nextLevel被设置为'A',例如"A 是新的根")。

  • 再次单击该环 - 现在是最内部的圆环 - 将关闭过滤器,父级和兄弟姐妹重新出现,nextLevel 设置为'Root'。

  • 单击没有子节点的环(如'a'或'B')不产生任何效果,因此在这种情况下nextLevel 未定义。

实际上,属性nextLevel 反映了状态的变化。

现在,为了更好地理解单击处理程序中正在发生的事情,您可能想检查currentpath(或出于一致性考虑,稍微调整版本)以及nextLevel 的存在/值:

Plotly.newPlot('graph', [{
  type: 'sunburst',
  parents: ['', 'Root', 'Root', 'A'],
  labels: ['Root', 'A', 'B', 'a']
}]).then(gd => {
  gd.on('plotly_sunburstclick', data => {
    const pt = data.points[0]
    const path = ((pt.currentPath ?? '/') + pt.label).replace('Root', '')
    console.log('path:', path)                // 单击元素的路径
    console.log('nextLevel:', data.nextLevel) // 如有的话,新根的标签
  })
})
英文:

The click event in this context eventually triggers what behaves like a "switch".

By default, we see the root node and all the children of the hierarchy.

  • Clicking on a ring that has at least one child (like 'A') filters out its parent ('Root') and siblings ('B') and put that ring visually as the new root, and we only see the hierarchy under that ring (ie. the ring filter switch is on and the property nextLevel is set accordingly in the event data to 'A', like "A is the new root").

  • Clicking on that ring again - now the innermost circle - switches the filter back off, the parent and the siblings reappear and nextLevel is set to 'Root'.

  • Clicking on a ring that has no children (like 'a' or 'B') has no effect, and thus nextLevel is undefined in this case.

Actually the property nextLevel is what reflects a state change.

Now in order to have a better understanding on what's going on in the click handler, you might want to check currentpath (or for consistency, a slightly tweaked version of it) and the presence/value of nextLevel :

Plotly.newPlot('graph', [{
  type: 'sunburst',
  parents: ['', 'Root', 'Root', 'A'],
  labels: ['Root', 'A', 'B', 'a']
}]).then(gd => {
  gd.on('plotly_sunburstclick', data => {
    const pt = data.points[0]
    const path = ((pt.currentPath ?? '/') + pt.label).replace('Root', '')
    console.log('path:', path)                // path of the clicked element 
    console.log('nextLevel:', data.nextLevel) // label of the new root if any
  })
})

huangapple
  • 本文由 发表于 2023年2月16日 03:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定