Let’s start this blog.

First, we create an MVC project.
Go to File-> New-> Project and select “ASP.NET Web Application”.
Enter the Project name and give a path in Location, after it clicks on ok button.
Add below URLs in the view page
  1. <script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
  2. <script src="https://www.amcharts.com/lib/3/pie.js"></script>
  3. <script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
  4. <link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
  5. <script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
Copy below code into view where you want to display the chart.
  1. @{
  2. ViewBag.Title = "Home Page";
  3. }

  4. <style>
  5. #MyChart{
  6. width: 100%;
  7. height: 500px;
  8. }
  9. #exportChart {
  10. font-size: 18px;
  11. padding: 5px 10px;
  12. position: absolute;
  13. top: 30px;
  14. right: 30px;
  15. z-index: 10;
  16. }
  17. </style>
  18. <div id="MyChart" style="width:50%"></div>
  19. <br />
  20. <input type="button" id="exportChart" value="Export Chart" onclick="exportChart();" />
  21. <script>
  22. var chart = AmCharts.makeChart("MyChart", {
  23. "type": "pie",
  24. "theme": "light",
  25. "dataProvider": [{
  26. "City": "Surat",
  27. "litres": 501.9
  28. }, {
  29. "City": "Karnavati",
  30. "litres": 301.9
  31. }, {
  32. "City": "Navsari",
  33. "litres": 201.1
  34. }, {
  35. "City": "Junagadh",
  36. "litres": 165.8
  37. }, {
  38. "City": "Amreli",
  39. "litres": 139.9
  40. }, {
  41. "City": "Bhavnagar",
  42. "litres": 128.3
  43. }, {
  44. "City": "Gondal",
  45. "litres": 99
  46. }, {
  47. "City": "Rajkot",
  48. "litres": 60
  49. }, {
  50. "City": "Pune",
  51. "litres": 50
  52. }],
  53. "valueField": "litres",
  54. "titleField": "City",
  55. "balloon": {
  56. "fixedPosition": true
  57. },
  58. "export": {
  59. "enabled": true,
  60. "menu": []
  61. }
  62. });
  63. function exportChart() {
  64. chart["export"].capture({}, function () {
  65. this.toPNG({}, function (base64) {
  66. $.ajax({
  67. url: '/Home/SaveImage',
  68. type: 'POST',
  69. data: { base64: base64},
  70. success: function (data) {
  71. if (data != "") {
  72. alert("file saved.")
  73. }
  74. else {
  75. alert("file not saved.")
  76. }
  77. },
  78. error: function (r, s, e) {
  79. alert("Unexpected error:" + e);
  80. console.log(r);
  81. console.log(s);
  82. console.log(e);
  83. }
  84. });
  85. });
  86. });
  87. }
  88. </script>
Now copy and paste below function into your Home controller.
  1. [HttpPost]
  2. public JsonResult SaveImage(string base64)
  3. {
  4. string filePath = "";
  5. string FileName = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
  6. string DomainName = ConfigurationSettings.AppSettings["DomainName"];
  7. try
  8. {
  9. var imageParts = base64.Split(',').ToList<string>();
  10. byte[] Image = Convert.FromBase64String(imageParts[1]);
  11. filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Image/" + FileName + ".png");
  12. System.IO.File.WriteAllBytes(filePath, Image);
  13. filePath = DomainName + "Image/" + FileName + ".png";
  14. }
  15. catch (Exception ex)
  16. {
  17. filePath = "";
  18. }
  19. return Json(filePath, JsonRequestBehavior.AllowGet);
  20. }

In this SaveImage() function, we get the base64 as a string from parameter and convert it to png using convert the base64 to bytes.

For getting the domain name in this article we use the "ConfigurationSettings.AppSettings["DomainName"]"

So, you need to add the "DomainName" key into the web.config like below code.
  1. <add key="DomainName" value="http://localhost:42213//" />
Right now I'm running my local project so I give the value of localhost but you can add your live domain name and don't forget to give permission to the folder for read and write for save image.
Output