Giter VIP home page Giter VIP logo

Comments (12)

TheDahv avatar TheDahv commented on June 4, 2024 6

I encountered something similar. I found a workaround but I don't know if it's a bandaid because I'm doing it wrong or not.

Effectively, I used the graph event hooks to call the layout function as nodes were added. Try this and see what works for you. Maybe it will uncover or yield insight into how it's meant to be done:

<CytoscapeComponent
  cy={cy =>
    cy.on('add', 'node', _evt => {
      cy.layout(layoutOptions).run()
      cy.fit()
    })
  } 
/>

from react-cytoscapejs.

EdwinSmulders avatar EdwinSmulders commented on June 4, 2024 1

What worked really well for me is this:

                    cy.on('resize', _evt => {
                        cy.layout(layoutOptions).run()
                        cy.fit()
                    })

This will run the layout the first time it configures the size, which is apparently after the nodes have been added. This is way more performance friendly than running it on every node add.

from react-cytoscapejs.

TheDahv avatar TheDahv commented on June 4, 2024

Seems like this idea was repeated here: #43

Admittedly not obvious though

from react-cytoscapejs.

SaiMun92 avatar SaiMun92 commented on June 4, 2024

Seems like this code is needed when i tried to re-render a different graph on the same canvas. All my nodes are on the same position previously.

from react-cytoscapejs.

willowv avatar willowv commented on June 4, 2024

Adding the cy.on calls didn't solve the problem for me. I've tried putting a run on ready as well, and no good. Not really sure what else there is to try...

from react-cytoscapejs.

sgratzl avatar sgratzl commented on June 4, 2024
const layout = {
name: "dagre",
rankDir: "LR"
}
pageData = < CytoscapeComponent
elements = {
CytoscapeComponent.normalizeElements({
nodes: nodess,
edges: edgess,
layout: layout,
})
}

I'm confused why the layout is part of the CytoscapeComponent.normalizeElements call, it should be set next the elements as another property

from react-cytoscapejs.

EdwinSmulders avatar EdwinSmulders commented on June 4, 2024

I'm having the same problem, and just like eps-tvaughan, cy.on did not solve the problem for either.

Edit: it did work, I misspelled the cy prop

from react-cytoscapejs.

willowv avatar willowv commented on June 4, 2024

I ended up using a different lightweight graph vis library and writing my own basic layout calculation. My graphs are pretty light (strictly less than 20 nodes) and I think ultimately cytoscape is probably overkill for what I'm doing.

from react-cytoscapejs.

dcsan avatar dcsan commented on June 4, 2024

from a plotly component. works for me but its really not obvious to get here... thanks those pathfinders!

                <CytoscapeComponent
                    cy={cy =>
                        cy.on('add', 'node', _evt => {
                            cy.layout(layout).run()
                            cy.fit()
                        })
                    }
                    className='cyto-box'
                    elements={graphData}
                    layout={layout}
                    stylesheet={stylesheet}
                />

from react-cytoscapejs.

fiatexodus avatar fiatexodus commented on June 4, 2024

What worked really well for me is this:

                    cy.on('resize', _evt => {
                        cy.layout(layoutOptions).run()
                        cy.fit()
                    })

This will run the layout the first time it configures the size, which is apparently after the nodes have been added. This is way more performance friendly than running it on every node add.

Unfortunately I didn't have similar luck, however your comment inspired a simple workaround which was to only call layout and fit after all the changed elements had been added (i.e. run a counter in the add event handler and when it matches elements.length, then do the layout and fit).

edit: by similar luck, I mean I tried using resize and for my use case (changing elements from React set state), I didn't get the layout/fit calls expected.

from react-cytoscapejs.

sahansk2 avatar sahansk2 commented on June 4, 2024

Because my elements are fed into the parent component by a variable created with useState, what I'm doing is just running the .layout and .fit methods in a React.useEffect hook. The plus side is that this works. The downside is that the window spazzes a little bit when I change the data.

    React.useEffect(() => {
        if (cyRef.current !== null) {
            cyRef.current.layout(graphLayout).run()
            cyRef.current.fit()
        }
    })

    return <CytoscapeComponent elements={allElements}
                style={graphComponentStyle}
                stylesheet={graphStyleSheet}
                layout={graphLayout}
                cy={cy => {
                    cyRef.current = cy
                }}/>

To be clear, allElements is not the useState variable; the actual state variable is higher up, which is comprised of random JSON that from a server. I run some processing on the stateful variable to get allElements.

from react-cytoscapejs.

aa-software2112 avatar aa-software2112 commented on June 4, 2024

This worked for me... using the "dagre" layout

        cy={cy => {
            // The layout is set here. Otherwise, it doesn't
            // seem to respond well!
            cy.layout(settings[renderLayout]).run();
        }}

from react-cytoscapejs.

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.