Comments (12)
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.
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.
Seems like this idea was repeated here: #43
Admittedly not obvious though
from react-cytoscapejs.
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.
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.
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.
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.
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.
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.
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.
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.
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)
- ReferenceError: window is not defined when refresshing the page.
- Maximum call stack size exceeded HOT 1
- How to use ctxmenu in this component?
- Issues with React 18 StrictMode HOT 4
- Next.js uses cjs module loader resulting in SyntaxError: Cannot use import statement outside a module HOT 2
- TS: cannot find module error
- nodes selction is not working
- Canvas elements moved by 50% to the right of the CytoscapeComponent HOT 2
- v2.0 has demo target missing HOT 7
- resize CytoscapeComponent
- updates to component HOT 2
- Createportal / react-cytoscape HOT 1
- Microbundle Compile Error Next.js HOT 1
- Support automatically check/uncheck link between two nodes
- Question: Could the nodes be interactive? HOT 2
- Question: Update to React 18? HOT 2
- Question: Styling & Layout HOT 1
- Infinite render graph force-directed on parameter "randomize": false
- Module not found: Can't resolve 'react-cytoscapejs' in Nextjs
- TypeError: renderer is null HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-cytoscapejs.