How to dynamically embed custom charts in emails
October 21, 2023
It’s tricky to embed visual elements like charts in emails. Emails aren’t like web pages; they come with their own set of quirks. But since you’ll still obviously want to convey your data visually, we put together this guide to help you get around the limitations of email.
The challenge with email environments
Instead, most email clients support a subset of HTML and CSS, which means we can create an image representation of our chart and embed it within the email.
How to embed custom charts
Generate a chart image on the server-side
The first step in the process is to generate an image of your chart. There are a couple of libraries, depending on the server-side language you're using, that allow for this:
- Node.js: Use a combination of Chart.js with the 'canvas' package.
- Python: You can use libraries like Matplotlib or Seaborn.
- Ruby: The Gruff gem is a solid option.
Let’s try Node.js:
Upload the image to a CDN
Once the chart is converted into an image, upload it to a Content Delivery Network (CDN). Using a CDN ensures that the image loads quickly regardless of the recipient's location. Amazon S3, Cloudinary, and Firebase are good options.
Emded the image in the email
After uploading the image, embed it into the email content using a simple
<img> tag. Ensure you use the URL provided by the CDN.
Other things to consider
Ensure the chart image looks good on all devices. Consider generating multiple sizes or using vector formats like SVG.
Always include alternative text (
alt attribute) for the image. If the image fails to load, the recipient will at least have a description of the content.
Compress the image to ensure that it loads quickly and doesn't eat up a lot of the recipient's data.
Ensure your CDN caches the images for optimal performance.
Dynamically embedding custom charts in emails is a blend of art and technology. While we're limited by email client capabilities, with a server-side approach, we can still convey crucial data visually.