How to convert lines into a shape once drawing completed in Angular Diagram?
This article explains how to convert lines into shapes once drawing is completed in Angular Diagram. In Angular Diagram, we can convert the drawn lines into shapes by using the group feature. The drawn lines (polyline or Bezier connectors) can be converted into a shape. Initially, add N number of polylines or Bezier curves to the diagram. The line’s source point and target point play a vital role in converting the lines into shapes.
The following code sample explains how the lines’ source point and target point is converted into a shape’s path.
let node: NodeModel;
let offsetX: number;
let offsetY: number;
let path: string = 'M';
for (let i: number = 0; i < this.diagram.selectedItems.connectors.length; i++) {
if (i === 0) {
if (this.diagram.selectedItems.connectors[i].type === 'Bezier') {
path = path + String(this.diagram.selectedItems.connectors[i].sourcePoint.x) + ',' +
String(this.diagram.selectedItems.connectors[i].sourcePoint.y);
path = path + ' C' + String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.x) + ',' +
String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.y) + ' ';
path = path + String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.x) + ',' +
String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.y) + ' ';
path = path + String(this.diagram.selectedItems.connectors[i].targetPoint.x) + ',' +
String(this.diagram.selectedItems.connectors[i].targetPoint.y);
} else {
let points: PointModel[] = (this.diagram.selectedItems.connectors[i] as Connector).intermediatePoints;
path = path + String(points[0].x) + ',' + String(points[0].y);
for (let j: number = 1; j < points.length; j++) {
path = path + ' L' + String(points[j].x) + ',' + String(points[j].y);
}
}
} else {
if (this.diagram.selectedItems.connectors[i].type === 'Bezier') {
path = path + ' C' + String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.x) + ',' +
String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point1.y) + ' ';
path = path + String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.x) + ',' +
String((this.diagram.selectedItems.connectors[i].segments[0] as BezierSegmentModel).point2.y) + ' ';
path = path + String(this.diagram.selectedItems.connectors[i].targetPoint.x) + ',' +
String(this.diagram.selectedItems.connectors[i].targetPoint.y);
} else {
let points: PointModel[] = (this.diagram.selectedItems.connectors[i] as Connector).intermediatePoints;
path = path + ' L' + String(points[0].x) + ',' + String(points[0].y);
for (let j: number = 1; j < points.length; j++) {
path = path + ' L' + String(points[j].x) + ',' + String(points[j].y);
}
}
}
}
Then a variable path is declared, which is used to store the path data of the lines drawn. This path data is converted into a shape. The path data draws an SVG shape. The commands used to draw the shape in the above code sample is explained as follows,
M = moveto - Set a new current point.
C = curveto - Draw a curve.
L = lineto - Draw a straight line.
this.diagram.group();
for (let j: number = 0; j < this.diagram.nodes.length; j++) {
if (this.diagram.nodes[j].children) {
offsetX = this.diagram.nodes[j].offsetX;
offsetY = this.diagram.nodes[j].offsetY;
}
}
After getting path the diagram is grouped. The offsetX and offsetY of the node with the children are used to place the converted shape, then remove the drawn lines Finally, the lines drawn are added as a shape in the diagram.
this.diagram.remove();
node = { id: 'shape', shape: { type: 'Path', data: path }, offsetY: offsetY, offsetX: offsetX };
this.diagram.add(node);
this.diagram.dataBind();
Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning about how to convert lines into a shape once drawing is completed 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, BoldDesk Support, or feedback portal. We are always happy to assist you!