Articles in this section
Category / Section

How to Export Annotations in React Column Chart?

3 mins read

Description

This article shows how to export annotation in React Column Charts.

Solution

To export annotations in a chart, start by converting the annotations into foreignObject elements and appending them to the chart’s SVG. Use getBoundingClientRect() to obtain the size and position of each annotation, then transfer the HTML content of the annotation into the foreignObject. Hide the original annotation and append the foreignObject to the SVG. Next, serialize the SVG to a string, render this serialized SVG string onto a canvas, and finally, convert the canvas content to a PNG image. This process ensures that annotations are accurately exported as part of the chart.

Code-snippet:

The following example shows how to export annotation into image.

function clickHandlerJPEG() {
   let svg: SVGSVGElement | null = document.querySelector('#charts_svg');
   if (svg) {
     let annotationEle = document.getElementById('charts_Annotation_Collections')?.querySelectorAll('[id*="charts_Annotation_"]'); 
     if (annotationEle) {
       for (let i = 0; i < annotationEle.length; i++) {
         let annotation: any = annotationEle[i];
         let foreign = document.createElementNS(
           'http://www.w3.org/2000/svg',
           'foreignObject'
         );
         foreign.setAttribute(
           'width',
           (annotation.getBoundingClientRect().width + 10).toString()
         );
         foreign.setAttribute(
           'height', (annotation.getBoundingClientRect().height + 10).toString()
         );
         foreign.setAttribute('x', annotation['style'].left);
         foreign.setAttribute('y', annotation['style'].top);
         foreign.innerHTML = annotation.innerHTML;
         annotation['style'].display = 'none';
         svg.appendChild(foreign);
       }
     }
     let svgData = new XMLSerializer().serializeToString(svg);
     let canvas = document.createElement('canvas');
     document.body.appendChild(canvas);
     let svgSize = svg.getBoundingClientRect();
     canvas.width = svgSize.width;
     canvas.height = svgSize.height;
     let ctx = canvas.getContext('2d');
     if (ctx) {
       ctx.fillStyle = 'white';
       ctx.fillRect(0, 0, canvas.width, canvas.height);
       let img = document.createElement('img');
       img.setAttribute('src', 'data:image/svg+xml;base64,' + btoa(svgData));
       img.onload = function () {
         ctx.drawImage(img, 0, 0);
         let imagedata = canvas.toDataURL('image/png');
         let anchor = document.createElement('a');
         anchor.download = 'Chart.png';
         anchor.href = imagedata;
         document.body.appendChild(anchor);
         anchor.click();
         canvas.remove();
       };
     }C
   }
 }

The following screenshot illustrates the output of the above code snippet

image.png

View Sample in Stackblitz

Conclusion

I hope you enjoyed learning on how to export annotations in React Column Chart.
You can refer to our React Charts 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 React Charts 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