Articles in this section
Category / Section

How to export the annotation in JavaScript Charts?

3 mins read

Description

This article explains how to export an annotation of the JavaScript charts in PNG format.

Solution

The annotations in the JavaScript Charts component cannot be exported to an image because they are intended to render any items in the charts component, such as text, images, or custom HTML elements. Since the chart is an SVG-based component, the annotation can be rendered as a foreignObject element at the application level.

You can create a foreignObject element for each annotation 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 is now converted to a PNG or JPEG image for export.

Code Snippet

The following code shows how to export the annotations in charts.

document.getElementById('togglebtn').onclick = function () {

       var dataLabelEle = document.getElementById("container").querySelectorAll('[id="container_Annotation_0"]');

       var dataLabel = dataLabelEle[0];

       var foreign = document.createElementNS('http://www.w3.org/2000/svg',"foreignObject");

       foreign.setAttribute('width', (dataLabel.getBoundingClientRect().width + 100).toString());

       foreign.setAttribute('height', (dataLabel.getBoundingClientRect().height).toString());

       foreign.setAttribute('x', dataLabel.style.left);

       foreign.setAttribute('y', dataLabel.style.top);

       foreign.innerHTML = dataLabel.innerHTML;

       dataLabel.style.display='none';

       var svg = document.querySelector("#container_svg");

       svg.appendChild(foreign);

       var svgData = new XMLSerializer().serializeToString(svg);

       var canvas = document.createElement("canvas");

        document.body.appendChild(canvas);

        var svgSize = svg.getBoundingClientRect();

        canvas.width = svgSize.width;

        canvas.height = svgSize.height;

        var ctx = canvas.getContext("2d");

       var img = document.createElement("img");

       img.setAttribute("src", "data:image/svg+xml;base64," + btoa(svgData));

       img.onload = function () {

            ctx.drawImage(img, 0, 0);

            var imagedata = canvas.toDataURL( "image/png");

            var anchor = document.createElement("a");

            anchor.download = "Charts.png";

            anchor.href = imagedata;

            document.body.appendChild(anchor);

            anchor.click();

            (canvas).remove();

       };

   };


The following image illustrates the output of the above code:

image.png

Refer to the working sample for additional details and implementation: View Sample in Stackblitz

Conclusion

We hope you enjoyed learning how to export the annotation in JavaScript Chart.

You can refer to our JavaScript Charts feature tour page to learn about its other features and documentation, as well as how to quickly get started with configuration specifications. You can also explore our JavaScript 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, support portal, BoldDesk Support, 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