Articles in this section

How to Convert the Javascript Diagram Polyline Connector to Shape?

In the Syncfusion® Javascript Diagram component, we have support to draw a Polyline connector interactively by clicking and dragging the mouse on the diagram canvas at runtime. It allows creating polyline segments with straight lines and angled vertices at control points directly within the diagram interface. Kindly refer to the documentation regarding the Polyline connector.

Documentation: Polyline Connector

You can convert a polyline connector into a shape by capturing its segments and using those points to create a polygon shape. This process allows you to draw a polyline, capture the coordinates of its segments, and then remove the original polyline connector while adding a new node with the polygon shape.

Steps to Convert a Polyline Connector to Shape:

  • Set up a button to draw the polyline connector.
  • Create the Polyline Connector, by setting the type of the drawingObject, as ‘Polyline’.
  • Assign DiagramTools.DrawOnce to the tool property of the DiagramComponent, enabling a single instance of drawing.
   document.getElementById('polyline').onclick = function () {
       let polyline = { id: 'connector1', type: 'Polyline' };
       diagram.drawingObject = polyline;
       diagram.tool = ej.diagrams.DiagramTools.DrawOnce;
       diagram.dataBind();
   };

When the drawing is completed, the connector is selected. Handle the Click Event to capture connector points. when the polyline connector is clicked, check if it contains segments. If so, initialize a new node object with a Polygon shape, using the points from each segment of the polyline to build the polygon.

Now add the new Polygon node to the Diagram. After extracting the points, remove the original polyline connector from the diagram and add the new node with the polygon shape, maintaining the position of the original connector. Kindly refer to the sample and code snippet below.

Sample: Draw shape using Polyline connector

Code - snippet:

function click(args) {
 if (args.actualObject != undefined && args.actualObject.segments) {
   if (args.button == 'Left') {
     let node = {
       id: 'polylineShape'+ ej.diagrams.randomId(),
       style: {
         fill: 'yellow',
         strokeColor: 'black',
         opacity: 4,
       },
       shape: {
         type: 'Basic',
         shape: 'Polygon',
         points: [],
       },
     };
     for (let i = 0; i < args.actualObject.segments.length - 1; i++) {
       node.shape.points.push(args.actualObject.segments[i].point);
     }
     node.shape.points.push(args.actualObject.targetPoint);
     node.offsetX = args.actualObject.wrapper.offsetX;
     node.offsetY = args.actualObject.wrapper.offsetY;
     diagram.remove(args.actualObject);
     diagram.add(node);
   }
 }
} 

Conclusion

I hope you enjoyed learning how to convert the JavaScript Diagram polyline connector to a shape.
You can refer to our JavaScript 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 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!

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