How to Manage Multiple Diagram and Overviews in React?
Switching between multiple diagrams in an application using the Syncfusion® React Diagram Component requires careful handling to ensure a smooth user experience. One common approach developers use is the style.display
attribute to toggle diagram visibility, alternating the display between block
and none
. This method preserves any changes made through interactions with each diagram, ensuring the state is maintained without losing modifications when switching between diagrams.
However, this approach can sometimes lead to rendering issues, as the initial rendering of a diagram depends on the visible dimensions of the diagram’s container. If the container is hidden, the dimensions of the diagram may not be computed properly, which can result in misalignment and incomplete rendering of elements when the diagram becomes visible again.
To address this rendering issue, it is essential to update the viewport of the diagram when switching its visibility. The diagram.updateViewPort()
method must be called after changing the visibility of the container from none
to block
. This ensures the diagram’s bounds are recalculated and all elements are aligned correctly within the viewport.
Here is the React sample that manages multiple diagrams in an application effectively using the above-mentioned approach:
import React, { useState } from 'react';
import { createRoot } from 'react-dom/client';
import { DiagramComponent, OverviewComponent } from '@syncfusion/ej2-react-diagrams';
const App = () => {
const [selectedDiagramId, setSelectedDiagramId] = useState(1);
const diagrams = [1, 2, 3].map(id => ({
id,
nodes: createSampleNodes(id)
}));
function createSampleNodes(diagramId) {
return [
{ id: `node${diagramId}1`, offsetX: 100, offsetY: 100, annotations: [{ content: `Node ${diagramId}1` }] },
{ id: `node${diagramId}2`, offsetX: 200, offsetY: 200, annotations: [{ content: `Node ${diagramId}2` }] }
];
}
function selectDiagram(id) {
setSelectedDiagramId(id);
// Use setTimeout to allow UI changes to resolve before updating components
setTimeout(() => {
// Access Syncfusion diagram instance and update the viewport
const diagram = document.getElementById('diagram' + id).ej2_instances[0];
diagram.updateViewPort();
}, 100);
}
return (
<div className="app-container">
<div className="button-group">
{diagrams.map(diagram => (
<button
key={diagram.id}
onClick={() => selectDiagram(diagram.id)}
className={selectedDiagramId === diagram.id ? 'selected' : ''}
>
Diagram {diagram.id}
</button>
))}
</div>
{diagrams.map(diagram => (
<div
key={diagram.id}
className="diagram-wrapper"
style={{ display: selectedDiagramId === diagram.id ? 'block' : 'none' }}
>
<DiagramComponent id={'diagram' + diagram.id} nodes={diagram.nodes} />
<div class="overview">
<OverviewComponent id={'overview' + diagram.id} sourceID={'diagram' + diagram.id} />
</div>
</div>
))}
</div>
);
};
const root = createRoot(document.getElementById('container'));
root.render(<App />);
Sample: Click Here
Conclusion
I hope you enjoyed learning how to manage multiple Diagram and overviews in React.
You can refer to our React Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Diagram example to understand how to create and manipulate data. For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!