13

I want to create a line chart similar below:

Line Chart

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!

3
  • 2
    Did you try the html email?
    – VMAtm
    Feb 27, 2017 at 6:18
  • what do you mean html email?
    – Sherwin
    Feb 27, 2017 at 6:50
  • 1
    SQL Server Reporting Services can create this sort of report either on demand or delivered via email by subscription
    – Steve Ford
    Mar 2, 2017 at 15:06

3 Answers 3

9
+50

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.

4
  • Note it's possible to add a right axis for your percentages also Mar 1, 2017 at 1:49
  • This is cool didn't know google has something like this. This will save me a lot of time in development. xD
    – Sherwin
    Mar 1, 2017 at 7:06
  • Unfortunately, Google Image Charts is deprecated now.
    – Cyril N.
    Jul 10, 2018 at 8:18
  • @CyrilN.It was deprecated long before my post - in 2012. It still works however. At least for the time being Jul 10, 2018 at 8:39
1

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.

0
1
  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");
    }
    
1
  • Thanks for this. :)
    – Sherwin
    Mar 1, 2017 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.