react-vis icon indicating copy to clipboard operation
react-vis copied to clipboard

Sankey diagram - When source index is larger than target index the whole app freezes

Open comatory opened this issue 5 years ago • 0 comments

So this is kind of unfortunate but I have quite a complex way of contructing data for Sankey diagram where I need to recalculate and shift some data around. This also means reconstructing links prop so the indices match. However sometimes what happens is that I'm actually creating links array where some item, that is situated further in nodes array points to another node in nodes array that is in the beginning.

Consider this data:

{
  "nodes": [
    {
      "id": "A-1",
      "name": "A"
    },
    {
      "id": "B-1",
      "name": "B"
    },
    {
      "id": "C-1",
      "name": "C"
    }
  ],
  "links": [
    {
      "source": 0,
      "target": 1,
      "value": 10
    },
    {
      "source": 1,
      "target": 2,
      "value": 8
    },
    {
      "source": 2,
      "target": 1,
      "value": 2
    }
  ]
}

The last link is pointing from node with ID C-1 to node with ID B-1. While logically you should be constructing the data sequentially, I don't see a reason why this should cause any errors. Even worse, no error gets printed to console and application eats up 100% of CPU because it's trying to do something recursively (at least from what I can tell).

When I change last link object to:

{
    "source": 1,
    "target": 2,
    "value": 2
}

it works with no problems. Reproducible example can be found here: https://codesandbox.io/s/react-vis-sankey-error-with-reverse-link-twhf3?file=/src/AppSankey.js

comatory avatar Oct 27 '20 15:10 comatory