How to Convert the Polyline Connector to Shape in Angular Diagram?
In this article, you can learn about how to convert the polyline connector to a shape in an Angular diagram. In the Syncfusion® Angular Diagram component, we have support for drawing a polyline connector interactively by clicking and dragging the mouse on the diagram canvas at runtime. It allows the creation of 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.
//bind to button click
public drawPolyLine() {
let polyline = {
id: 'connector1',
type: 'Polyline'
};
this.diagram.drawingObject = polyline;
this.diagram.tool = DiagramTools.DrawOnce;
this.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 code snippet and sample below.
Code - snippet:
public click(args: IClickEventArgs) {
if (args.actualObject != undefined && (args.actualObject as any).segments) {
if (args.button == 'Left') {
let node = {
id: 'polylineShape' + randomId(),
style: {
fill: 'yellow',
strokeColor: 'black',
opacity: 4,
},
shape: {
type: 'Basic',
shape: 'Polygon',
points: [],
},
} as NodeModel;
for (let i = 0; i < (args.actualObject as any).segments.length - 1; i++) {
(node.shape as DiagramShapeModel).points.push((args.actualObject as any).segments[i].point);
}
(node.shape as DiagramShapeModel).points.push((args.actualObject as any).targetPoint);
node.offsetX = (args.actualObject as any).wrapper.offsetX;
node.offsetY = (args.actualObject as any).wrapper.offsetY;
this.diagram.remove((args.actualObject as any));
this.diagram.add(node);
}
}
}
Sample: Draw shape using Polyline connector
Conclusion
We hope you enjoyed learning about how to convert the polyline connector to a shape 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, Direct-Trac, or feedback portal. We are always happy to assist you!