Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node id 0 not set properly #160

Closed
sauln opened this issue Jan 11, 2019 · 3 comments
Labels
bug

Comments

@sauln
Copy link
Contributor

@sauln sauln commented Jan 11, 2019

It seems that if a node id is 0, then any links that should point to 0 instead point to an [Object object].

Here's a minimal example

import React from 'react';
import ReactDOM from 'react-dom';

import { Graph } from 'react-d3-graph';

var badgraph = {
    "id": 1,
    "nodes": [{
        "id": 0
    }, {
        "id": 1
    }, {
        "id": 2
    }, {
        "id": 3
    }, {
        "id": 4
    }, {
        "id": 5
    }, {
        "id": 6
    }],
    "links": [{
        "source": 0,
        "target": 1
    }, {
        "source": 0,
        "target": 3
    }, {
        "source": 2,
        "target": 4
    }, {
        "source": 2,
        "target": 5
    }, {
        "source": 2,
        "target": 6
    }]
}


const config = {
  height: 400,
  width: 400,
  staticGraph: false,
  node: {
    color: 'brown',
    size: 20
  },
  d3: {
    alphaTarget: 0.05,
    gravity: -30,
    linkDistance: 0.05,
    linkLength: 1,
  }
};

ReactDOM.render(
  <Graph id={"graph"}
         data={badgraph}
         config={config}
  />,
  document.getElementById('root')
)

and the resulting graph is shown below. It looks like one end of the link is never updated, even though the location of the 0 node is updated.

screen shot 2019-01-11 at 2 25 06 pm

By changing the first node to have id 999, the graph renders correctly.

I'm new to react and javascript, so this might be a user error.

@danielcaldas danielcaldas added the bug label Jan 11, 2019
@danielcaldas

This comment has been minimized.

Copy link
Owner

@danielcaldas danielcaldas commented Jan 11, 2019

Hey thanks for reporting, this is actually a bug, the problem is that react-d3-graph is currently supporting both data types as ids (not the best decision IMHO) you can have the type number or the type string and it should just work... Still, as you may know, 0 evaluates to false in javascript that is why some checks for node.id fallback to something else rather than the actual id 0. This is an internal inconsistency, still I would strongly recommend that you use strings as ids only. In the future, we might be dropping the support for number id as data type as it unnecessarily increases complexity and causes this kind of naughty bugs.

@sauln

This comment has been minimized.

Copy link
Contributor Author

@sauln sauln commented Jan 14, 2019

Thanks for getting back to me. I'll definitely switch to strings in the meantime.

@danielcaldas

This comment has been minimized.

Copy link
Owner

@danielcaldas danielcaldas commented Jan 20, 2019

This was fixed with #161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.