We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

connect nodes using ports and mouse

Is there any way of allowing the user to connect 2 nodes together by just clicking on a source port and then "dragging" the connector to a target port ?

10 Replies

SG Shyam G Syncfusion Team October 9, 2017 07:02 AM UTC

Hi Julian, 
 
Please set port constraints ConnectOnDrag which activates the connection Tool when mouse hover on the port and you can draw the connector and establish the connection between two nodes. Please refer to the code example, playground link and help documentation below. 
 
Code example: 
var nodes = 
            [ 
                { 
                    name: "NewIdea", width: 150, height: 60, offsetX: 300, offsetY: 60, 
                    ports: [ 
                        { 
                            name: "NewIdea_port1", 
                            //define the portconstraints connect and connectondrag  
                            constraints: ej.datavisualization.Diagram.PortConstraints.Connect | ej.datavisualization.Diagram.PortConstraints.ConnectOnDrag, 
                        }] 
                }, 
            ]; 
 
 
 
Regards, 
Shyam G 



JL Julian Lyndon-Smith October 9, 2017 08:26 AM UTC

thanks for the reply. I asked the question, because I had followed the documentation and was surprised that it wasn't working ;)

I am using the angular toolkit. My code looks like this

private portOut:Object = {
name: 'out',
offset: {
x: 1,
y: 0.5
},
shape: 'circle',
size:16,
connectorPadding:10,
visibility: ej.datavisualization.Diagram.PortVisibility,
constraints: ej.datavisualization.Diagram.PortConstraints.Connect | ej.datavisualization.Diagram.PortConstraints.ConnectOnDrag,
fillColor: '#ffb74d'
};

and then I add the port like this

this.nodes = [
{
name:'start',
data: {foo: 'bar'},
type:'basic',
shape:'ellipse',
width:50,
height:50,
offsetX:45,
offsetY:45,
fillColor:'#428bca',
borderColor:'black',
constraints:this.nodeConstraints,

ports: [Object.assign({},this.portOut)],

labels: [{ text: 'start', fontColor:'white', textAlign:'center' }]
},

however, when the diagram is drawn, nothing changes when I hover over the port. If I click on the port, the resize handles appear instead







JL Julian Lyndon-Smith October 10, 2017 08:28 AM UTC

gentle *bump* - sorry but I really need to figure out what I'm doing wrong here. thanks ;)



SG Shyam G Syncfusion Team October 10, 2017 11:04 AM UTC

Hi Julian, 
 
We have created an angular sample in which we have rendered nodes with ports. When you hover on the port, the connection Tool gets activated(cursor changes to crosshair) and you can draw the connector. Please refer to the sample and video below. 
 
 
 
Regards, 
Shyam G 



JL Julian Lyndon-Smith October 11, 2017 09:18 AM UTC

thanks for the example - it showed where my problem was

I had

visibility:  ej.datavisualization.Diagram.PortVisibility,

instead of

visibility:ej.datavisualization.Diagram.PortVisibility.Visible,

:)



SG Shyam G Syncfusion Team October 12, 2017 04:51 AM UTC

Hi Julian, 
  
Please let us know if you need further assistance on this. 
  
Regards, 
Shyam G 



CH Chaitanya August 31, 2019 09:46 AM UTC

Hi, 

Is it possible to get source port and target port when first time (initially) dragged connector from port..??

connectionChange() triggering only when already initiated connector connects to another port. 

When we initially mouseover on a port and drag connector to another port  then we should get the source and target of that connector.

is it possible..??

Thanks in advance.



SG Shyam G Syncfusion Team September 2, 2019 04:25 AM UTC

Hi Chaitanya, 

Please use connectorSourceChange and connectorTargetChange to achieve your requirement. Please refer to an code example, sample and help documentation below. 

Code example: 
 
$("#diagram").ejDiagram({ 
                 // connectorSourceChange and connectorTargetChange events 
                connectorSourceChange: function connectorSourceChange(args) { 
                     
                }, 
                connectorTargetChange: function connectorSourceChange(args) { 
                     
                }, 
            }); 



Regards, 
Shyam G 



JA Jayaprakash October 15, 2021 12:31 PM UTC

Hi Team,

When I Hover the port need to connect another port like user friendly I have tired below this... how to enable port visibility?? I have shared the file

I need this source code in javascript  https://www.syncfusion.com/blogs/wp-content/uploads/2020/02/Create-diagram-interactively-in-Blazor-application.gif


ej.base.enableRipple(true);
/**
* Serialization sample
*/
ej.diagrams.Diagram.Inject(ej.diagrams.UndoRedo);


//save the diagram object in json data.
function download(data) {
if (window.navigator.msSaveBlob) {
var blob = new Blob([data], { type: 'data:text/json;charset=utf-8,' });
window.navigator.msSaveOrOpenBlob(blob, 'Diagram.json');
}
else {
var dataStr = 'data:text/json;charset=utf-8,' + encodeURIComponent(data);
var a = document.createElement('a');
a.rel='nofollow' href = dataStr;
a.download = 'Diagram.json';
document.body.appendChild(a);
a.click();
a.remove();
}
}
//create and add ports for Node.
function getPorts(obj) {
var ports = [
{ id: 'port1', shape: 'Circle', offset: { x: 0, y: 0.5 },fillColor:"red" },
{ id: 'port2', shape: 'Circle', offset: { x: 0.5, y: 1 },fillColor:"red" },
{ id: 'port3', shape: 'Circle', offset: { x: 1, y: 0.5 },fillColor:"red"},
{ id: 'port4', shape: 'Circle', offset: { x: 0.5, y: 0 },fillColor:"red" }
];
var additionalports = [{ id: 'port2', shape: 'Circle', offset: { x: 0.5, y: 1 } },
{ id: 'port4', shape: 'Circle', offset: { x: 0.5, y: 0 } }];
if (obj.id === 'Data') {
return additionalports;
}
else {
return ports;
}
}

function openPalette() {
var paletteSpace = document.getElementById('palette-space');
var isMobile = window.matchMedia('(max-width:550px)').matches;
if (isMobile) {
if (!paletteSpace.classList.contains('sb-mobile-palette-open')) {
paletteSpace.classList.add('sb-mobile-palette-open');
}
else {
paletteSpace.classList.remove('sb-mobile-palette-open');
}
}
}

//Initializes the nodes for the diagram
var nodes = [
{
id: 'Start', height: 50, width: 100, offsetX: 250, offsetY: 60,
shape: { type: 'Flow', shape: 'Terminator' },
annotations: [{
content: 'Start'
}],
style: { fill: '#d0f0f1', strokeColor: '#797979' }
}, {
id: 'Alarm', height: 50, width: 100, offsetX: 250, offsetY: 160,
shape: { type: 'Flow', shape: 'Process' },
annotations: [{
content: 'Alarm Rings'
}],
style: { fill: '#fbfdc5', strokeColor: '#797979' }
}, {
id: 'Ready', height: 80, width: 100, offsetX: 250, offsetY: 260,
shape: { type: 'Flow', shape: 'Decision' },
annotations: [{
content: 'Ready to Get Up?'
}],
style: { fill: '#c5efaf', strokeColor: '#797979' }
}, {
id: 'Climb', height: 50, width: 100, offsetX: 250, offsetY: 370,
shape: { type: 'Flow', shape: 'Process' },
annotations: [{
content: 'Climb Out of Bed'
}],
style: { fill: '#fbfdc5', strokeColor: '#797979' }
}, {
id: 'End', height: 50, width: 100, offsetX: 250, offsetY: 460,
shape: { type: 'Flow', shape: 'Terminator' },
annotations: [{
content: 'End'
}],
style: { fill: '#d0f0f1', strokeColor: '#797979' }
}, {
id: 'Relay', height: 50, width: 100, offsetX: 450, offsetY: 160,
shape: { type: 'Flow', shape: 'Delay' },
annotations: [{
content: 'Relay'
}],
style: { fill: '#f8eee5', strokeColor: '#797979' }
}, {
id: 'Hit', height: 50, width: 100, offsetX: 450, offsetY: 260,
shape: { type: 'Flow', shape: 'Process' },
annotations: [{
content: 'Hit Snooze Button'
}],
style: { fill: '#fbfdc5', strokeColor: '#797979' }
}];
//Initializes the connector for the diagram
var connectors = [
{
id: 'connector1', sourceID: 'Start', targetID: 'Alarm'
},
{ id: 'connector2', sourceID: 'Alarm', targetID: 'Ready' },
{
id: 'connector3', sourceID: 'Ready', targetID: 'Climb',
annotations: [{ content: 'Yes', style: { fill: 'white' } }]
},
{ id: 'connector4', sourceID: 'Climb', targetID: 'End', },
{
id: 'connector5', sourceID: 'Ready', targetID: 'Hit',
annotations: [{ content: 'No', style: { fill: 'white' } }]
},
{ id: 'connector6', sourceID: 'Hit', targetID: 'Relay' },
{ id: 'connector7', sourceID: 'Relay', targetID: 'Alarm' }];
var lineinterval;
lineinterval = [1, 9, 0.25, 9.75, 0.25, 9.75, 0.25, 9.75, 0.25, 9.75, 0.25, 9.75, 0.25, 9.75, 0.25, 9.75, 0.25, 9.75, 0.25, 9.75];
var gridlines = { lineColor: '#e0e0e0', lineIntervals: lineinterval };
//Initializes diagram control
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '645px',
nodes: nodes,
connectors: connectors,
snapSettings: { horizontalGridlines: gridlines, verticalGridlines: gridlines },
//set default value for Connectors.
getConnectorDefaults: function (args) {
args.targetDecorator.height = 5;
args.targetDecorator.width = 5;
args.style.strokeColor = '#797979';
args.targetDecorator.style = { fill: '#797979', strokeColor: '#797979' };
return args;
},
//Sets the Node style for DragEnter element.
dragEnter: function (args) {
var obj = args.element;
if (obj instanceof ej.diagrams.Node) {
var ratio = 100 / obj.width;
obj.width = 100;
obj.height *= ratio;
}
}
});
diagram.appendTo('#diagram');
//Initialize the flowshapes for the symbol palatte
var flowshapes = [
{ id: 'Terminator', shape: { type: 'Flow', shape: 'Terminator' } },
{ id: 'Process', shape: { type: 'Flow', shape: 'Process' } },
{ id: 'Decision', shape: { type: 'Flow', shape: 'Decision' } },
{ id: 'Document', shape: { type: 'Flow', shape: 'Document' } },
{ id: 'PreDefinedProcess', shape: { type: 'Flow', shape: 'PreDefinedProcess' } },
{ id: 'PaperTap', shape: { type: 'Flow', shape: 'PaperTap' } },
{ id: 'DirectData', shape: { type: 'Flow', shape: 'DirectData' } },
{ id: 'SequentialData', shape: { type: 'Flow', shape: 'SequentialData' } },
{ id: 'Sort', shape: { type: 'Flow', shape: 'Sort' } },
{ id: 'MultiDocument', shape: { type: 'Flow', shape: 'MultiDocument' } },
{ id: 'Collate', shape: { type: 'Flow', shape: 'Collate' } },
{ id: 'SummingJunction', shape: { type: 'Flow', shape: 'SummingJunction' } },
{ id: 'Or', shape: { type: 'Flow', shape: 'Or' } },
{ id: 'InternalStorage', shape: { type: 'Flow', shape: 'InternalStorage' } },
{ id: 'Extract', shape: { type: 'Flow', shape: 'Extract' } },
{ id: 'ManualOperation', shape: { type: 'Flow', shape: 'ManualOperation' } },
{ id: 'Merge', shape: { type: 'Flow', shape: 'Merge' } },
{ id: 'OffPageReference', shape: { type: 'Flow', shape: 'OffPageReference' } },
{ id: 'SequentialAccessStorage', shape: { type: 'Flow', shape: 'SequentialAccessStorage' } },
{ id: 'Annotation', shape: { type: 'Flow', shape: 'Annotation' } },
{ id: 'Annotation2', shape: { type: 'Flow', shape: 'Annotation2' } },
{ id: 'data', shape: { type: 'Flow', shape: 'Data' } },
{ id: 'Card', shape: { type: 'Flow', shape: 'Card' } },
{ id: 'Delay', shape: { type: 'Flow', shape: 'Delay' } },
];
//Initializes connector symbols for the symbol palette
var connectorSymbols = [
{
id: 'Link1', type: 'Orthogonal', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 40, y: 40 }, style: { strokeWidth: 2, strokeColor: '#757575' },
targetDecorator: { shape: 'Arrow', style:{strokeColor: '#757575', fill: '#757575'} }
},
{
id: 'link2', type: 'Orthogonal', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 40, y: 40 },
targetDecorator: { shape: 'None' }, style: { strokeWidth: 2, strokeColor: '#757575' }
},
{
id: 'Link3', type: 'Straight', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 40, y: 40 }, style: { strokeWidth: 2, strokeColor: '#757575' },
targetDecorator: { shape: 'Arrow', style:{strokeColor: '#757575', fill: '#757575'} }
},
{
id: 'link4', type: 'Straight', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 40, y: 40 },
targetDecorator: { shape: 'None' }, style: { strokeWidth: 2, strokeColor: '#757575' }
},
{
id: 'link5', type: 'Bezier', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 40, y: 40 },
style: { strokeWidth: 2, strokeColor: '#757575' }, targetDecorator: { shape: 'None' }
},
];
//Initializes ToolBar control to invoke save and load the diagram
var toolbarObj = new ej.navigations.Toolbar({
clicked: function (args) {
if (args.item.text === 'New') {
diagram.clear();
}
else if (args.item.text === 'Load') {
document.getElementsByClassName('e-file-select-wrap')[0].querySelector('button').click();
} else if (args.item.id === 'palette-icon') {
openPalette();
} else {
download(diagram.saveDiagram());
}
},
items: [
{ id: 'palette-icon', prefixIcon: 'e-ddb-icons2 e-toggle-palette', align: 'Right' },
{ text: 'New', tooltipText: 'New', prefixIcon: 'e-ddb-icons e-new' },
{ type: 'Separator' }, { text: 'Save', tooltipText: 'Save', prefixIcon: 'e-ddb-icons e-save' },
{ type: 'Separator' }, { text: 'Load', tooltipText: 'Load', prefixIcon: 'e-ddb-icons e-open' }
]
});
toolbarObj.appendTo('#toolbar');
//Initializes the symbol palette
var palette = new ej.diagrams.SymbolPalette({
expandMode: 'Multiple',
palettes: [
{ id: 'flow', expanded: true, symbols: flowshapes, iconCss: 'e-ddb-icons1 e-flow', title: 'Flow Shapes' },
{ id: 'connectors', expanded: true, symbols: connectorSymbols, iconCss: 'e-ddb-icons1 e-connector', title: 'Connectors' }
],
//set default value for Node.
getNodeDefaults: function (symbol) {
if (symbol.id === 'Terminator' || symbol.id === 'Process' || symbol.id === 'Delay') {
symbol.width = 80;
symbol.height = 40;
}
else if (symbol.id === 'Decision' || symbol.id === 'Document' || symbol.id === 'PreDefinedProcess' ||
symbol.id === 'PaperTap' || symbol.id === 'DirectData' || symbol.id === 'MultiDocument' || symbol.id === 'Data') {
symbol.width = 50;
symbol.height = 40;
} else {
symbol.width = 50;
symbol.height = 50;
}

symbol.style.strokeWidth = 2;
symbol.style.strokeColor = '#757575';
},
symbolMargin: { left: 15, right: 15, top: 15, bottom: 15 },
getSymbolInfo: function (symbol) {
return { fit: true };
},
width: '100%', height: '645px', symbolHeight: 60, symbolWidth: 60,
});
palette.appendTo('#symbolpalette');
var uploadObj = new ej.inputs.Uploader({
asyncSettings: {
saveUrl: 'https://aspnetmvc.syncfusion.com/services/api/uploadbox/Save',
removeUrl: 'https://aspnetmvc.syncfusion.com/services/api/uploadbox/Remove'
},
success: onUploadSuccess,
showFileList:false
});
uploadObj.appendTo('#fileupload');
function onUploadSuccess(args) {
var file1 = args.file;
var file = file1.rawFile;
var reader = new FileReader();
reader.readAsText(file);
reader.onloadend = loadDiagram;
}
//Load the diagraming object.
function loadDiagram(event) {
diagram.loadDiagram(event.target.result);
}





Attachment: diagram_(2)_a92df72a.zip



GG Gowtham Gunashekar Syncfusion Team October 18, 2021 01:09 PM UTC

Hi Jayaprakash, 
 
On the further analysis of the shared sample, we understood that you want to enable the port draw feature in the diagram. We suggest you set the port constraints as “ej.diagrams.PortConstraints.Default |ej.diagrams.PortConstraints.Draw”  to enable the connector drawing tool while hover over the port and set the port’s visibility property as “ej.diagrams.PortVisibility.Defalut |ej.diagrams.PortVisibility” to make the port visible while hover the mouse pointer over the nodes . we have added the suggested changes in the shared sample and added below for your references. 
 
Code snippet: 
 
 
        getNodeDefaults: function (node) { 
            node.ports = [ 
                { id: 'port1', shape: 'Circle', offset: { x: 0, y: 0.5 },fillColor:"red" ,visibility: ej.diagrams.PortVisibility.Defalut |ej.diagrams.PortVisibility.Hover, constraints:ej.diagrams.PortConstraints.Default |ej.diagrams.PortConstraints.Draw}, 
                { id: 'port2', shape: 'Circle', offset: { x: 0.5, y: 1 },fillColor:"red",visibility: ej.diagrams.PortVisibility.Defalut |ej.diagrams.PortVisibility.Hover,constraints:ej.diagrams.PortConstraints.Default |ej.diagrams.PortConstraints.Draw }, 
                { id: 'port3', shape: 'Circle', offset: { x: 1, y: 0.5 },fillColor:"red",visibility: ej.diagrams.PortVisibility.Defalut |ej.diagrams.PortVisibility.Hover,constraints:ej.diagrams.PortConstraints.Default |ej.diagrams.PortConstraints.Draw}, 
                { id: 'port4', shape: 'Circle', offset: { x: 0.5, y: 0 },fillColor:"red",visibility: ej.diagrams.PortVisibility.Defalut |ej.diagrams.PortVisibility.Hover,constraints:ej.diagrams.PortConstraints.Default |ej.diagrams.PortConstraints.Draw } 
            ]; 
            return node; 
        }, 
 
 
 
Regards, 
Gowtham  


Loader.
Live Chat Icon For mobile
Up arrow icon