How to Dynamically Set a Node’s Fill Color in JavaScript Diagram?
In Syncfusion® JavaScript 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:
- 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. - 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
fill
property of the selected node is updated, changing the node’s background color.
Code-Sample:
selectionChange: function (args) {
if (args.state === 'Changed' && args.newValue.length > 0) {
var dialogObject = document.getElementById('defaultdialog').style;
console.log('selection');
dialogPosition(dialogObject);
} else {
document.getElementById('defaultdialog').style.visibility = 'hidden';
}
},
function dialogPosition(dialogObject) {
document.getElementById('defaultdialog').style.visibility = 'visible';
if (diagram.selectedItems.nodes.length > 0) {
dialogObject.left =
(
diagram.selectedItems.nodes[0].wrapper.bounds.x +
diagram.selectedItems.nodes[0].width +
10
).toString() + 'px';
dialogObject.top =
(
diagram.selectedItems.nodes[0].wrapper.bounds.y - 10
).toString() + 'px';
}
}
var iconTemp ='<div id="colorPicker" >'
var colorPicker = new ej.inputs.ColorPicker({
id: 'color-picker',
change: function (args) {
// Change the fill color of the selected node dynamically
if (diagram.selectedItems.nodes.length > 0) {
diagram.selectedItems.nodes[0].style.fill = args.value;
}
}
});
var dialog = new ej.popups.Dialog({
id: 'defaultdialog',
showCloseIcon: true,
width: '250px',
visible: true,
header: iconTemp,
});
dialog.appendTo('#defaultdialog');
colorPicker.appendTo('#colorPicker')
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 JavaScript Diagram.
You can refer to our JavaScript 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 JavaScript 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!