Example of Export in ASP.NET MVC Charts Control
This sample demonstrates client-side exporting of the chart, enabling you to export its data to Excel, PDF, and CSV formats. Additionally, it allows you to save the chart in image formats such as JPEG, PNG, and SVG.
Properties
@Html.UiChart(new UiChartOptions
{
Id = "exportChart",
Title = "Top 10 Countries Using Solar Power",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ShowGridLines = false, ValueType = UiChartValueType.Category, Interval = 1, LabelRotation = -45 },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 40, Interval = 10, LabelFormat = "{value}GW" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "GigaWatts", Name = "Measurements (in Gigawatt)", ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "exportChart",
Title = "Top 10 Countries Using Solar Power",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ShowGridLines = false, ValueType = UiChartValueType.Category, Interval = 1, LabelRotation = -45 },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 40, Interval = 10, LabelFormat = "{value}GW" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Country", YName = "GigaWatts", Name = "Measurements (in Gigawatt)", ShowDataLabel = true }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="exportType">Export Type</label><select id="exportType">@foreach (var type in (string[])ViewData["data"]) { <option>@type</option> }</select></div>
<div class="demo-property-row"><label for="fileName">File Name</label><input type="text" id="fileName" value="Chart" /></div>
<div class="demo-property-row"><button type="button" id="exportButton">Export</button></div>
</div>
<script>
document.getElementById('exportButton').addEventListener('click', function () {
var chart = window.Lumen.get('exportChart');
var name = document.getElementById('fileName').value || 'Chart';
var type = document.getElementById('exportType').value;
if (type === 'CSV') {
// The rows the chart draws, as a file
var rows = chart.getData();
var keys = rows.length ? Object.keys(rows[0]) : [];
var csv = [keys.join(',')].concat(rows.map(function (r) { return keys.map(function (k) { return JSON.stringify(r[k] == null ? '' : r[k]); }).join(','); })).join('\r\n');
var link = document.createElement('a');
link.href = URL.createObjectURL(new Blob([csv], { type: 'text/csv' }));
link.download = name + '.csv';
link.click();
URL.revokeObjectURL(link.href);
} else {
chart.exportImage(name, type === 'JPEG' ? 'jpg' : type.toLowerCase());
}
});
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ChartController : Controller
{
// The page is Views/Chart/ChartExport.cshtml; it draws what ViewData holds.
public ActionResult ChartExport()
{
List<ExportChartData> ChartPoints = new List<ExportChartData>
{
new ExportChartData {Country="India", GigaWatts = 35.5, DataLabelMappingName="35.5GW"},
new ExportChartData {Country="China", GigaWatts = 18.3, DataLabelMappingName="18.3GW"},
new ExportChartData {Country="Italy", GigaWatts = 17.6, DataLabelMappingName="17.6GW"},
new ExportChartData {Country="Japan", GigaWatts = 13.6, DataLabelMappingName="13.6GW"},
new ExportChartData {Country="United state", GigaWatts = 12, DataLabelMappingName="12GW"},
new ExportChartData {Country="Spain", GigaWatts = 5.6, DataLabelMappingName="5.6GW"},
new ExportChartData {Country="France", GigaWatts = 4.6, DataLabelMappingName="4.6GW"},
new ExportChartData {Country="Australia", GigaWatts = 3.3, DataLabelMappingName="3.3GW"},
new ExportChartData {Country="Belgium", GigaWatts = 3, DataLabelMappingName="3GW"},
new ExportChartData {Country="United Kingdom", GigaWatts = 2.9, DataLabelMappingName="2.9GW"},
};;
ViewData["ChartPoints"] = ChartPoints;
ViewData["data"] = new string[] { "JPEG", "PNG", "SVG", "PDF", "CSV" };
return View();
}
public class ExportChartData
{
public string Country { get; set; }
public double GigaWatts { get; set; }
public string DataLabelMappingName { get; set; }
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
In this example, you can see how the export functionality is configured. The rendered chart can be exported in JPEG, PNG, SVG, and PDF file types. Data from the chart can also be exported to Excel and CSV files.