How to Manage Multiple Diagrams and Overviews in Vue?
Switching between multiple diagrams in an application using the Syncfusion® Vue 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 Vue sample that manages multiple diagrams in an application effectively using the above-mentioned approach:
<template>
<div class="app-container">
<div class="button-group">
<button
v-for="diagram in diagrams"
:key="diagram.id"
@click="selectDiagram(diagram.id)"
:class="{ selected: selectedDiagramId === diagram.id }"
>
Diagram {{ diagram.id }}
</button>
</div>
<!-- Loop through diagrams and display each based on the selected diagram ID -->
<div
class="diagram-wrapper"
v-for="diagram in diagrams"
:key="diagram.id"
v-show="selectedDiagramId === diagram.id"
>
<ejs-diagram :id="'diagram' + diagram.id" :nodes="diagram.nodes"></ejs-diagram>
<!-- Overview component providing a mini-map of the diagram -->
<div class="overview">
<ejs-overview :id="'overview' + diagram.id" :sourceID="'diagram' + diagram.id"></ejs-overview>
</div>
</div>
</div>
</template>
<script>
import { DiagramComponent, OverviewComponent } from "@syncfusion/ej2-vue-diagrams";
export default {
name: 'App',
components: {
"ejs-diagram": DiagramComponent, // Register Syncfusion Diagram component
"ejs-overview": OverviewComponent // Register Syncfusion Overview component
},
data() {
return {
selectedDiagramId: 1, // Currently selected diagram ID
diagrams: [ // Array holding diagram data
{ id: 1, nodes: this.createSampleNodes(1) },
{ id: 2, nodes: this.createSampleNodes(2) },
{ id: 3, nodes: this.createSampleNodes(3) }
]
};
},
methods: {
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` }] }
];
},
selectDiagram(id) {
this.selectedDiagramId = 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);
}
},
};
</script>
Sample: Click Here
Conclusion
I hope you enjoyed learning how to manage multiple Diagrams and overviews in a Vue Application.
You can refer to our Vue 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 Vue 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!