Articles in this section
Category / Section

How to Manage Multiple Diagrams and Overviews in Angular?

3 mins read

In this article, you can learn about how to manage multiple diagrams and overviews in Angular. Switching between multiple diagrams in an application using the Syncfusion® Angular 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 Angular sample that manages multiple diagrams in an application effectively using the above-mentioned approach:

import { CommonModule } from '@angular/common';
import { Component, ViewChild, OnInit } from '@angular/core';
import { DiagramModule, OverviewModule, NodeModel, DiagramComponent, OverviewComponent } from '@syncfusion/ej2-angular-diagrams';

interface DiagramData {
 id: number;
 nodes: NodeModel[];
}

@Component({
 selector: 'app-root',
 imports: [CommonModule, DiagramModule, OverviewModule],
 template: `<div class="app-container">
 <div class="button-group">
   <button
     *ngFor="let diagram of diagrams"
     [class.selected]="selectedDiagramId === diagram.id"
     (click)="selectDiagram(diagram.id)">
     Diagram {{ diagram.id }}
   </button>
 </div>
 <ng-container *ngFor="let diagram of diagrams">
   <div class="diagram-wrapper" [style.display]="selectedDiagramId === diagram.id ? 'block' : 'none'">
     <ejs-diagram [id]="'diagram' + diagram.id" [nodes]="diagram.nodes"></ejs-diagram>
     <div class="overview">
       <ejs-overview #overviewRef [id]="'overview' + diagram.id" [sourceID]="'diagram' + diagram.id"></ejs-overview>
     </div>
   </div> 
 </ng-container>
 </div>`,
 styleUrl: './app.component.css',
 standalone: true,
})
export class AppComponent implements OnInit {
 selectedDiagramId: number;
 diagrams: DiagramData[] = [];

 ngOnInit(): void {
   this.diagrams = [1, 2, 3].map(id => ({
     id,
     nodes: this.createSampleNodes(id),
   }));
   this.selectDiagram(1);
 }

 createSampleNodes(diagramId: number): NodeModel[] {
   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: number): void {
   this.selectedDiagramId = id;
   // Use setTimeout to allow UI changes to resolve before updating components
   setTimeout(() => {
     const element = document.getElementById('diagram' + id);
     if (element) {
       // Access Syncfusion diagram instance and update the viewport
       const diagramInstance = (element as any).ej2_instances[0];
       diagramInstance.updateViewPort();
     }
   }, 100);
 }
} 

Sample: Click Here

Conclusion

I hope you enjoyed learning how to manage multiple Diagrams and overviews in Angular.

You can refer to our Angular 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 Angular 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, BoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied