How to export the data label template with a Angular Pie Charts in the PNG format?
Description
This article explains how to export the data label template with Angular Pie Charts in the PNG format.
Solution
The data label template in the Angular Pie Charts cannot be exported to an image because it is intended to render any items in the chart component, such as text, images, or custom HTML elements. Since the chart is an SVG-based component, the data label template can be rendered as a foreignObject element at the application level.
You can create a foreignObject element for each data label template and add it to the chart component’s SVG element. Then, to allow image and PDF export, you can draw this SVG element in an HTML canvas element. Using the HTML image element, this canvas element can be transformed into an image. This image element can then be converted to a PNG or JPEG image for export, or it can be inserted into a PDF document using Syncfusion’s ej2-pdf-export library.
Code Snippet
In the following code, the above process is carried out in the exportPie method
public exportPie() {
       var label = [];
       var labels;
       var padding;
       var width;
       var height;
       var svg = document.getElementById("container_svg");
       var clone = svg.cloneNode(true);
       var series = document.getElementById("container_SeriesCollection");
       for (var k = 0; k < series.childElementCount; k++) {
           label = document.getElementById("container").querySelectorAll('[id= ' + 'container_Series_' + k + '_DataLabelCollections');
           if (label.length >= 1) {
               for (var j = 0; j < label[0].children.length; j++) {
                   labels = label[0].children[j];
                   var foreign = document.createElementNS("http://www.w3.org/2000/svg", "foreignObject");
                   foreign.setAttribute("width", labels.getBoundingClientRect().width);
                   foreign.setAttribute("height", (labels.getBoundingClientRect().height));
                   padding = labels.getBoundingClientRect().height;
                   if (series.childElementCount > 1) {
                       foreign.setAttribute("x", parseFloat(labels.style.left).toString());
                   } else {
                       foreign.setAttribute("x", (parseFloat(labels.style.left) - padding).toString());
                   }
                   foreign.setAttribute("y", labels.style.top);
                   var style = labels.children[0].getAttribute("style");
                   foreign.setAttribute('style', style);
                   foreign.innerHTML = labels.innerHTML;
                   width = parseFloat(svg.getAttribute("width"));
                   height = parseFloat(svg.getAttribute("height"));
                   clone.setAttribute("width", width.toString());
                   clone.setAttribute("width", width.toString());
                   clone.setAttribute("height", height.toString());
                   clone.appendChild(foreign);
               }
           }            
       }
       var svgData = new XMLSerializer().serializeToString(clone);
       var canvas = document.createElement("canvas");
       document.body.appendChild(canvas);
       var svgSize = clone.getBoundingClientRect();
       canvas.width = parseFloat(clone.getAttribute("width"));
       canvas.height = parseFloat(clone.getAttribute("height"));
       canvas.style.backgroundColor = "white";
       var ctx = canvas.getContext("2d");
       var image = new Image();
       image.setAttribute("src", "data:image/svg+xml;base64," + btoa(svgData));
       image.onload = function () {
           ctx.drawImage(image, 0, 0);
           var imagedata  = canvas.toDataURL("image/png");
           var anchor = document.createElement("a");
           anchor.download = "Chart.png";
           anchor.href = imagedata;
           document.body.appendChild(anchor);
           anchor.click();
           canvas.remove();
       };
   };
The following image illustrates the output of the above code:
Refer to the working sample for additional details and implementation: View Sample in Stackblitz
Conclusion
We hope you enjoyed learning how to export the data label template with an Angular Pie Chart in PNG format.
You can refer to our Angular Charts feature tour page to learn about its other features and documentation, and how to quickly get started for configuration specifications. You can also explore our Angular Charts example to understand how to create and visualize 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 comment section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!