How to Dynamically Set a Node’s Fill Color in Vue Diagram?
This article explains how to dynamically set a node’s fill color in Vue Diagram. The Syncfusion® Vue Diagram allows you to 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:
- Listen for Node Selection: Use the
selectionChangeevent 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. - 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.
- Update the Node’s Fill Color: When the user selects a color from the color picker, the
fillproperty of the selected node is updated, changing the node’s background color.
Code-Sample:
onSelectionChange(args) {
// Check if node is selected
if (args.state === 'Changed' && args.newValue.length > 0) {
this.dialogVisible = true; // Show dialog
this.setDialogPosition(); // Set dialog position
} else {
this.dialogVisible = false; // Hide dialog if no node is selected
}
},
setDialogPosition() {
// Adjust dialog position based on selected node
const diagramInstance = this.$refs.diagram.ej2Instances;
if (diagramInstance.selectedItems.nodes.length > 0) {
const selectedNode = diagramInstance.selectedItems.nodes[0];
this.dialogPosition = {
X: selectedNode.wrapper.bounds.x + selectedNode.width + 10,
Y: selectedNode.wrapper.bounds.y - 10,
};
}
},
<template>
<div class="control-pane">
<div class="control-section">
<!-- Syncfusion Diagram Component -->
<ejs-diagram
id="diagram"
ref="diagram"
:width="'100%'"
:height="'645px'"
:nodes="nodes"
@selectionChange="onSelectionChange"
></ejs-diagram>
<!-- Dialog for Color Picker -->
<ejs-dialog
id="defaultdialog"
ref="dialog"
:visible="dialogVisible"
showCloseIcon="true"
width="250px"
:position="dialogPosition" >
<div>
<ejs-colorpicker
mode="Palette"
:change="onColorChange"
></ejs-colorpicker>
</div>
</ejs-dialog>
</div>
</div>
</template>
Refer to the working sample for additional details and implementation: StackBlitz
Conclusion
We hope you enjoyed learning how to dynamically set a node’s fill color in Vue Diagram.
You can refer to our Vue Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with 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, BoldDesk Support, or feedback portal. We are always happy to assist you!