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
- <script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
- <script src="https://www.amcharts.com/lib/3/pie.js"></script>
- <script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
- <link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
- <script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
Copy below code into view where you want to display the chart.
- @{
- ViewBag.Title = "Home Page";
- }
- <style>
- #MyChart{
- width: 100%;
- height: 500px;
- }
- #exportChart {
- font-size: 18px;
- padding: 5px 10px;
- position: absolute;
- top: 30px;
- right: 30px;
- z-index: 10;
- }
- </style>
- <div id="MyChart" style="width:50%"></div>
- <br />
- <input type="button" id="exportChart" value="Export Chart" onclick="exportChart();" />
- <script>
- var chart = AmCharts.makeChart("MyChart", {
- "type": "pie",
- "theme": "light",
- "dataProvider": [{
- "City": "Surat",
- "litres": 501.9
- }, {
- "City": "Karnavati",
- "litres": 301.9
- }, {
- "City": "Navsari",
- "litres": 201.1
- }, {
- "City": "Junagadh",
- "litres": 165.8
- }, {
- "City": "Amreli",
- "litres": 139.9
- }, {
- "City": "Bhavnagar",
- "litres": 128.3
- }, {
- "City": "Gondal",
- "litres": 99
- }, {
- "City": "Rajkot",
- "litres": 60
- }, {
- "City": "Pune",
- "litres": 50
- }],
- "valueField": "litres",
- "titleField": "City",
- "balloon": {
- "fixedPosition": true
- },
- "export": {
- "enabled": true,
- "menu": []
- }
- });
- function exportChart() {
- chart["export"].capture({}, function () {
- this.toPNG({}, function (base64) {
- $.ajax({
- url: '/Home/SaveImage',
- type: 'POST',
- data: { base64: base64},
- success: function (data) {
- if (data != "") {
- alert("file saved.")
- }
- else {
- alert("file not saved.")
- }
- },
- error: function (r, s, e) {
- alert("Unexpected error:" + e);
- console.log(r);
- console.log(s);
- console.log(e);
- }
- });
- });
- });
- }
- </script>


Amit MohantyPosted Jul 23, 2019, 11:18 PM
Very useful..