Articles in this section

How to Dynamically Set a Node’s Fill Color in Angular Diagram?

In this article, you can learn about how to dynamically set a node’s fill color in Angular Diagram. With the Syncfusion® Angular Diagram, you can easily change the background color of a node by modifying its fill property. One effective way to achieve this is by integrating a color picker component within a dialog that is triggered when a node is selected.

Steps to Implement:

  1. Listen for Node Selection: Use the selectionChange event to detect when a node is selected. This event fires whenever the user clicks on a node, allowing you to display the dialog with the color picker.
  2. Show the Color Picker Dialog: Once a node is selected, a dialog containing the color picker will be shown. The dialog is positioned dynamically near the selected node for ease of access.
  3. Update the Node’s Fill Color: When the user selects a color from the color picker, the fill property of the selected node is updated, changing the node’s background color.

Code-Sample:

// Handle the selectionChange event
 public selectionChange(args: any): void {
   if (args.state === 'Changed' && args.newValue.length > 0) {
     // Show the dialog and position it when selection changes
     this.dialogComponent.visible = true;
     this.dialogPosition();
   } else {
     this.dialogComponent.visible = false;
   }
 }

 // Position the dialog based on the selected node
 dialogPosition(): void {
   if ((this.diagram as any).selectedItems.nodes.length > 0) {
     const selectedNode = (this.diagram as any).selectedItems.nodes[0];
     const dialogObject = this.dialogComponent.element.style;

     dialogObject.left = (
       selectedNode.wrapper.bounds.x + selectedNode.width + 30
     ).toString() + 'px';
     dialogObject.top = (
       selectedNode.wrapper.bounds.y - 30
     ).toString() + 'px';
   }
 } 
template: `
   <div id="container">
     <!-- Diagram component -->
     <ejs-diagram #diagramComponent [width]="'100%'" [height]="'600px'" [nodes]="nodes" (selectionChange)="selectionChange($event)">
     </ejs-diagram>

     <!-- Dialog component -->
     <ejs-dialog #dialogComponent [width]="'250px'" [visible]="false" [showCloseIcon]="true">
     <div>
     <ejs-input ejs-colorpicker   (change)="onColorChange($event)" id="element" mode="Palette" /></div>
       
     </ejs-dialog>
   </div>
 `, 

You can find a working example of this implementation on StackBlitz

Conclusion

I hope you enjoyed learning how to dynamically set a node’s fill color in Angular Diagram.

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)
Access denied
Access denied