简体   繁体   中英

Line chart generated image that will be sent through email

I want to create a line chart similar below:

折线图

I just wonder if there are available framework or API available in ASP.NET MVC that generates chart images since my goal is to send this via email. I'm thinking if I can just put something like <img src="http://imageapi.com?date1=20170101&date=20170130" /> then api will be handling the chart image generation.

On searching, I found a lot of chart framework using javascript but I doubt it will properly work on different email clients.

Thanks a lot!

Google Image Charts will do that. Pass data and display settings via the URL, and it will return an image.

eg.

<img src="https://chart.googleapis.com/chart?cht=lc&chd=t:30,10,45,38,25|10,20,10,20,10&chls=2.0,0.0,0.0&chs=200x125&chg=0,20,3,3,10,20&chxt=x,y&chxl=0:|Week1|Week2|Week3|Week4|Week5|1:|0|20|40|60|80|100&chs=800x300&chm=o,ff9900,0,-1,10.0|d,ff0000,1,-1,10.0&chco=FFC6A5,DEBDDE&chdl=Click|GRU" />

produces this chart:

They provide a playground for testing: https://developers.google.com/chart/image/docs/chart_playground

Note however that Google are not maintaining it further, but have no plans to remove this functionality:

While the dynamic and interactive Google Charts are actively maintained, we officially deprecated the static Google Image Charts way back in 2012. This gives us the right to turn it off without notice, although we have no plans to do so.

What is your design ? Your chart must be generated in web page,then it must be having html generated. If no html is generated and only image is generated then this is best. now you can send same content in.

If image is not generated then again you have 2 option here i) Send complete html in email body along with concern js/css ii) you can convert those html into image using(say c#) then send mail.

Please mention your complete scenario.

  1. There are different types of chart API available in market, both open source and licensed, you can use any one to generate your chart/diagram in page and you can send that page as an email attachment using following code.

      [HttpPost] public ActionResult SendWebPageAsAttachment() { var subject = Request.Form["subject"]; // You can provide subject from page or code var mailContent = Request.Form["bodyInnerHTML"]; // get the body inner HTML by form name var Body = "<div style='background-color:white;'>" + Request.Form["mailContent"] + "</div>"; // Email Body var attachmentName = DateTime.Now.ToString("yyyy/MM/dd").Replace("/", "-") + "_" + DateTime.Now.ToLongTimeString().Replace(" ", "_") + ".html"; // Attachment Name var baseUrl = HttpContext.Request.Url.Scheme + "://" + HttpContext.Request.Url.Authority + HttpContext.Request.ApplicationPath.TrimEnd('/') + '/'; // Base URL string src = @"src="""; mailContent = mailContent.Replace(src, src + baseUrl.Remove(baseUrl.Length - 1)); mailContent = "<html><head><link href='" + baseUrl + "Themes/styles.css' rel='stylesheet' type='text/css' /><link href='" + baseUrl + "Themes/style.css' rel='stylesheet' type='text/css' /></head><body>" + WebUtility.HtmlDecode(mailContent) + "</body></html>"; try { SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25); smtpClient.Credentials = new System.Net.NetworkCredential("info@MyWebsiteDomainName.com", "myIDPassword"); smtpClient.UseDefaultCredentials = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.EnableSsl = true; MailMessage mail = new MailMessage(); //Setting From , To and CC mail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site"); mail.To.Add(new MailAddress("info@MyWebsiteDomainName")); mail.CC.Add(new MailAddress("MyEmailID@gmail.com")); mail.IsBodyHtml = true; mail.Subject = subject; mail.Body = Body; var mailDataBytes = ASCIIEncoding.Default.GetBytes(mailContent); var mailStream = new MemoryStream(mailDataBytes); mail.Attachments.Add(new Attachment(mailStream, attachmentName)); smtpClient.Send(mail); } catch (Exception ex) { //catch } ViewBag.IsHttpPost = true; return View("SendWebPageAsAttachment"); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM