d3 / d3-force

Force-directed graph layout using velocity Verlet integration.
https://d3js.org/d3-force
ISC License
1.77k stars 375 forks source link

TypeError: Cannot create property 'vx' on string "Bob" in React #99

Closed jaydenlin closed 7 years ago

jaydenlin commented 7 years ago

I encountered this issue, does anyone has idea ? I ran all the d3 stuffs under componentDidMount.

* React@15.6.1

React Component

componentDidMount() {

       let dataset = {
            "nodes": [
                {
                    "id": "Alice"
                }, {
                    "id": "Bob"
                }, {
                    "id": "Carol"
                }
            ],
            "links": [
                {
                    "source": "Alice",
                    "target": "Bob"
                }, {
                    "source": "Bob",
                    "target": "Carol"
                }
            ]
        };
        let currentNode = ReactDOM.findDOMNode(this);
        let svg = d3
                .select(currentNode)
                .append("svg")
                .classed(s.svg, true),
            width = +svg.attr("width"),
            height = +svg.attr("height");

        var simulation = d3
            .forceSimulation()
            .force("link", d3.forceLink().distance(60).id(id => {}))
            .force("charge", d3.forceManyBody())
            .force("center", d3.forceCenter(width / 2, height / 2));

        let link = svg
            .append("g")
            .attr("class", "links")
            .selectAll("line")
            .data(dataset.links)
            .enter()
            .append("line")
            .attr("stroke-width", function (d) {
                return 3;
            });

        let node = svg
            .append("g")
            .selectAll("circle")
            .data(dataset.nodes)
            .enter()
            .append("circle")
            .attr("r", 5)
            .attr("fill", function (d) {
                return "#000";
            });
        simulation
            .nodes(dataset.nodes)
            .on("tick", () => {
                link
                    .attr("x1", function (d) {
                        return d.source.x;
                    })
                    .attr("y1", function (d) {
                        return d.source.y;
                    })
                    .attr("x2", function (d) {
                        return d.target.x;
                    })
                    .attr("y2", function (d) {
                        return d.target.y;
                    });

                node.attr("cx", function (d) {
                    return d.x;
                })
                    .attr("cy", function (d) {
                        return d.y;
                    });
            });

        simulation
            .force("link")
            .links(dataset.links);

    }
curran commented 7 years ago

The problem is that your code is not setting .id() properly. This is what you have: .id(id => {}).

In general, issues here are reserved for problems with the actual D3 library. For help with getting your code to work, please post to StackOverflow or the D3 Google Group.

mbostock commented 7 years ago

Please use Stack Overflow tag d3.js to ask for help. Although I make an effort to assist everyone that asks, I am not always available to provide help promptly or directly. Stack Overflow provides a better collaborative forum for self-help: tens of thousands of D3-related questions have already been asked there, and some answered questions may be relevant to you.

When asking for help, please include a link to a live example that demonstrates the issue, preferably on bl.ocks.org. It is often impossible to debug from code snippets alone. Isolate the issue and reduce your code as much as possible before asking for help. The less code you post, the easier it is for someone to debug, and the more likely you are to get a helpful response.

If you have a question about D3’s behavior and want to discuss it with other users, also consider the d3-js Google Group or joining the d3-js Slack.

Thank you! 🤗

Bwing4 commented 1 year ago

Use Integer rather than string as id . The source and target should also use integer

Jimflip commented 5 months ago

So what was the actual solution here?