Coder Social home page Coder Social logo

Comments (5)

duhaime avatar duhaime commented on April 29, 2024 11

In case it might help others, I hit this same snag ("Cannot create property 'vx' on string...") because I had updated my simulation's nodes but hadn't updated the links when transitioning between layouts, so needed to add a simulation.force('link').links(links); line before the .force('link', d3.forceLink(links).id(function(d) { return d.id; }) line.

from d3-force.

mbostock avatar mbostock commented on April 29, 2024

The documentation is out-of-date and I need to update it. You need to use link.id. Specifically: link.id(function(d, i) { return i; }).

EDIT: I was wrong. See subsequent comment.

from d3-force.

curran avatar curran commented on April 29, 2024

Ok, thank you.

I do think it would be nice, though, to have the index as the default id function. Just my 2¢.

All the best.

from d3-force.

mbostock avatar mbostock commented on April 29, 2024

Oops, I was wrong, and I overlooked something in your example.

The default node id accessor does use the numeric index as desired:

function id(d, i) {
  return i;
}

The problem isn’t in link.id. The problem is that your nodes are not objects: they are strings. The simulation is trying to assign the various node properties (in particular node.vx, representing the x-component of the node’s velocity) to those strings, and that’s what’s failing with a TypeError: “Cannot create property 'vx' on string 'a'.”

You need to use objects to represent your data, say like this:

var graph = {
  "nodes": [
    {"id": "a"},
    {"id": "b"},
    {"id": "c"}
  ],
  "links": [
    {"source": 0, "target": 1},
    {"source": 1, "target": 2}
  ]
};

The simulation doesn’t wrap the input nodes and links; it assigns properties to them directly.

from d3-force.

curran avatar curran commented on April 29, 2024

Ahhhh got it. Makes perfect sense. Thank you.

from d3-force.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.