发送到使用 mandrill 时如何在电子邮件中嵌入图表

How to embed a chart in email when sending out to using mandrill

我想根据我想要包含图表的某些数据,将每周报告作为电子邮件发送给商家。通常在网站上我使用 chartkick 来绘制图表,但我如何在电子邮件中嵌入图表。请帮帮我。

我的型号代码

def weekly_emailer
  # Some code here
  visits = Visit.group(:created_at).count
end

我的查看码

<%= line_chart @visits %>

你还不能用 chartkick 做到这一点,但我们上周刚刚发布了这个:https://ChartURL.com :)

您可以使用 "Encrypted URL" 方案即时生成图表,或者您可以向我们发送大量数据和 return 将解析为图像的 URL 短片.

它建立在 http://C3js.org 之上,因此您可以非常灵活地生成内容。

这些 URL 可用于网络应用程序和移动应用程序,但最初的目的是电子邮件图表。

我们将处理引入 ChartURL 的 chartkick 拉取请求,但与此同时,我希望这会有所帮助!

如果您使用 Highcharts,他们可以将图表转换为 PNG 文件(我认为还有其他几个选项)。

然而,为了完成这项工作,您有两个选择:

  1. 将数据和图表配置发送到他们的服务器进行转换
  2. 下载它的服务器实现,但仅适用于几种语言

过去,我已经为他们发送了一些用于非敏感数据转换的图表,并且效果很好 :) 尽管值得注意的是所有这些都是几年前的事了..

免责声明:我是 Image-Charts 创始人

作为独立黑客,每次我开始一个新的 SaaS 时,我都会遇到和你一样的问题,我不得不从头开始重写图像生成后端,然后通过电子邮件发送这些图表...:(

这就是为什么我在它之上构建了 Image-charts and added gif animation(电子邮件中的图表动画很棒!!),不再有服务器端图表渲染的痛苦,没有缩放问题,它是快如闪电,1 URL = 1 个图像图表。

https://image-charts.com/chart
?cht=bvg
&chd=t:10,15,25,30,40,80
&chs=700x300
&chxt=x,y
&chxl=0:|March '18|April '18|May '18|June '18|July '18|August '18|
&chdl=Visitors (in thousands)
&chf=b0,lg,90,05B142,1,0CE858,0.2
&chxs=1N**K
&chtt=Visitors report
&chma=0,0,10,10
&chl=||||+33% !|x2 !

enter image description here

enter image description here

enter image description here

enter image description here

因为 Chartkick 使用 Chart.js, You can take the Chart.js config object generated by Chartkick and render it using QuickChart, an open-source image renderer for Chart.js charts: https://github.com/typpo/quickchart 生成图表。

与此 post 上的其他答案不同,这实际上适用于 Chartkick 生成的图表。

采用这样的 Chart.js 配置。这类似于 Chartkick 生成的配置:

{
  type: 'bar',
  data: {
    labels: [2012, 2013, 2014, 2015, 2016],
    datasets: [{
      label: 'Data',
      data: [12, 6, 5, 18, 12]
    }]
  }
}

像这样将它打包到 URL 中(你也可以自行托管它,因为它是开源的):

https://quickchart.io/chart?bkg=white&c={ type: 'bar', data: { labels: [2012, 2013, 2014, 2015, 2016], datasets: [{ label: 'Data', data: [12, 6, 5, 18, 12] }] }}

然后在您的电子邮件中包含图片(对于更复杂的图表,通常最好 url 对配置进行编码):

<img src="https://quickchart.io/chart?bkg=white&c={type:'bar',data:{labels:[2012,2013,2014,2015,2016],datasets:[{label:'Data',data:[12,6,5,18,12]}]}}" />

此图片将出现在您的电子邮件中: