Articles in this section
Category / Section

How to import SVG files in Angular Diagram

3 mins read

This article provides an explanation of how to import SVG files in Syncfusion® Angular Diagram. The example demonstrates how to add a button to facilitate importing an image from the local system. When the button is clicked, it triggers a file selection dialog that allows users to choose an image from their local storage.

Include the file input element in your HTML, configured to accept only SVG file types:

 template: `
 <input type="file" id="fileUpload" (change)="onFileChange($event)" accept="image/svg+xml" />
 <ejs-diagram #diagram id="diagram" width="100%" height="800px" > </ejs-diagram>`

Once an image is selected, the application processes the file upload. Then onFileChange method is invoked to handle the upload and add the SVG file as a node to the diagram. The following code, demonstrates how to import SVG files into diagram:

 // function to import svg file to diagram
 onFileChange(args: any) {
   const file = args.target.files[0];
   if (file) {
     // Create a new FileReader to read the content
     const reader = new FileReader();
     // Define a callback function to execute when the file reading is complete
     reader.onload = (event: any) => {
       const svgContent = event.target.result;
       // Define the SVG node
       const svgNode: NodeModel = {
         id: 'svgNode',
         offsetX: 250, offsetY: 250,
         width: 200, height: 200,
         shape: {
           type: 'Native',
           content: svgContent  // Set the SVG content here
         }
       };
       // Add the SVG node to the diagram
       this.diagram.add(svgNode);
     };
     reader.readAsText(file);
   }
 }

You can find a working example of this implementation in the provided sample on StackBlitz.

Sample - Click here for sample

Conclusion

I hope you enjoyed learning how to import SVG files 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!

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