Print and Export in ASP.NET MVC Sankey Chart component
The Sankey Chart provides comprehensive print and export functionality, enabling users to generate static files in multiple formats (PNG, JPEG, SVG, PDF) or print the diagram directly. This is useful for reports, documentation, sharing, and offline access.
This guide covers printing the chart and exporting to various formats with customization options.
Print the Sankey Chart directly to paper or PDF using the print() method. This opens the browser’s print dialog, allowing users to select printer settings and output format:
<button type="button" style="margin-bottom:10px;" onclick="printSankey()">Print</button>
@Html.EJS().Sankey("sankey-container")
.Width("90%")
.Height("450px")
.Tooltip(t => t.Enable(true))
.LegendSettings(l => l.Visible(true))
.Nodes(ViewBag.SankeyNodes)
.Links(ViewBag.SankeyLinks)
.Render()
<script>
function printSankey() {
var el = document.getElementById('sankey-container');
var inst = el && el.ej2_instances ? el.ej2_instances[0] : null;
if (inst && typeof inst.print === 'function') {
inst.print();
}
}
</script>public ActionResult Index()
{
List<SankeyNode> nodes = new List<SankeyNode>
{
new SankeyNode { Id = "Agricultural Waste" },
new SankeyNode { Id = "Biomass Residues" },
new SankeyNode { Id = "Bio-conversion" },
new SankeyNode { Id = "Liquid Biofuel" },
new SankeyNode { Id = "Electricity" },
new SankeyNode { Id = "Heat" }
};
List<SankeyLink> links = new List<SankeyLink>
{
new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
};
ViewBag.SankeyNodes = nodes;
ViewBag.SankeyLinks = links;
return View();
}Export
Export the Sankey Chart to various file formats using the export() method. This allows you to generate standalone files suitable for sharing, archiving, or embedding in documents.
Export Formats
The Sankey Chart supports exporting to the following formats:
- PNG - Portable Network Graphics (raster format, good for web)
- JPEG - Joint Photographic Experts Group (compressed raster, smaller file size)
- SVG - Scalable Vector Graphics (vector format, scalable without quality loss)
- PDF - Portable Document Format (ideal for printing and archiving)
Export the chart using default settings with a default filename:
Export with Default Settings
<div style="margin-bottom:10px;">
<button type="button" style="margin-right:5px;" onclick="exportSankeyPNG()">Export PNG</button>
<button type="button" style="margin-right:5px;" onclick="exportSankeyPDF()">Export PDF</button>
<button type="button" onclick="exportSankeySVG()">Export SVG</button>
</div>
@Html.EJS().Sankey("sankey-container")
.Width("90%")
.Height("450px")
.Tooltip(t => t.Enable(true))
.LegendSettings(l => l.Visible(true))
.Nodes(ViewBag.SankeyNodes)
.Links(ViewBag.SankeyLinks)
.Render()
<script>
function getSankeyInstance() {
var el = document.getElementById('sankey-container');
return el && el.ej2_instances ? el.ej2_instances[0] : null;
}
function exportSankeyPNG() {
var inst = getSankeyInstance();
if (inst && typeof inst.export === 'function') {
inst.export('PNG', 'Sankey');
}
}
function exportSankeyPDF() {
var inst = getSankeyInstance();
if (inst && typeof inst.export === 'function') {
inst.export('PDF', 'Sankey');
}
}
function exportSankeySVG() {
var inst = getSankeyInstance();
if (inst && typeof inst.export === 'function') {
inst.export('SVG', 'Sankey');
}
}
</script>public ActionResult Index()
{
List<SankeyNode> nodes = new List<SankeyNode>
{
new SankeyNode { Id = "Agricultural Waste" },
new SankeyNode { Id = "Biomass Residues" },
new SankeyNode { Id = "Bio-conversion" },
new SankeyNode { Id = "Liquid Biofuel" },
new SankeyNode { Id = "Electricity" },
new SankeyNode { Id = "Heat" }
};
List<SankeyLink> links = new List<SankeyLink>
{
new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
};
ViewBag.SankeyNodes = nodes;
ViewBag.SankeyLinks = links;
return View();
}Export with Custom Options
Export the chart with a custom filename and format selection to control output file names and type:
<button type="button" style="margin-bottom:10px;" onclick="exportCustomSankey()">Export with Custom Options</button>
@Html.EJS().Sankey("sankey-container")
.Width("90%")
.Height("450px")
.Tooltip(t => t.Enable(true))
.LegendSettings(l => l.Visible(true))
.Nodes(ViewBag.SankeyNodes)
.Links(ViewBag.SankeyLinks)
.Render()
<script>
function exportCustomSankey() {
var el = document.getElementById('sankey-container');
var inst = el && el.ej2_instances ? el.ej2_instances[0] : null;
if (inst && typeof inst.export === 'function') {
inst.export('PNG', 'CustomSankey');
}
}
</script>public ActionResult Index()
{
List<SankeyNode> nodes = new List<SankeyNode>
{
new SankeyNode { Id = "Agricultural Waste" },
new SankeyNode { Id = "Biomass Residues" },
new SankeyNode { Id = "Bio-conversion" },
new SankeyNode { Id = "Liquid Biofuel" },
new SankeyNode { Id = "Electricity" },
new SankeyNode { Id = "Heat" }
};
List<SankeyLink> links = new List<SankeyLink>
{
new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
};
ViewBag.SankeyNodes = nodes;
ViewBag.SankeyLinks = links;
return View();
}Export Events
Before Export Event
Use the BeforeExport event to customize the export process before the file is generated. This allows you to modify chart properties, add watermarks, or perform pre-export calculation
Use the BeforeExport event to customize the export process:
<button type="button" id="btnExport" style="margin-bottom:10px;" onclick="exportSankey()">Export PNG</button>
@Html.EJS().Sankey("sankey-container")
.Width("90%")
.Height("450px")
.Tooltip(t => t.Enable(true))
.LegendSettings(l => l.Visible(true))
.BeforeExport("onBeforeExport")
.Nodes(ViewBag.SankeyNodes)
.Links(ViewBag.SankeyLinks)
.Render()
<script>
function onBeforeExport(args) {
args.cancel = false;
}
function exportSankey() {
var inst = document.getElementById('sankey-container').ej2_instances && document.getElementById('sankey-container').ej2_instances[0];
if (inst && typeof inst.export === 'function') {
inst.export('PNG', 'Sankey');
}
}
</script>public ActionResult Index()
{
List<SankeyNode> nodes = new List<SankeyNode>
{
new SankeyNode { Id = "Agricultural Waste" },
new SankeyNode { Id = "Biomass Residues" },
new SankeyNode { Id = "Bio-conversion" },
new SankeyNode { Id = "Liquid Biofuel" },
new SankeyNode { Id = "Electricity" },
new SankeyNode { Id = "Heat" }
};
List<SankeyLink> links = new List<SankeyLink>
{
new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
};
ViewBag.SankeyNodes = nodes;
ViewBag.SankeyLinks = links;
return View();
}Export Completed Event
Handle the completion of export using the ExportCompleted event:
<button type="button" id="btnExport" style="margin-bottom:10px;" onclick="exportSankey()">Export PNG</button>
@Html.EJS().Sankey("sankey-container")
.Width("90%")
.Height("450px")
.Tooltip(t => t.Enable(true))
.LegendSettings(l => l.Visible(true))
.ExportCompleted("onExportComplete")
.Nodes(ViewBag.SankeyNodes)
.Links(ViewBag.SankeyLinks)
.Render()
<script>
function onExportComplete(args) {
console.log('Export completed successfully');
}
function exportSankey() {
var el = document.getElementById('sankey-container');
var inst = el && el.ej2_instances ? el.ej2_instances[0] : null;
if (inst && typeof inst.export === 'function') {
inst.export('PNG', 'Sankey');
}
}
</script>public ActionResult Index()
{
List<SankeyNode> nodes = new List<SankeyNode>
{
new SankeyNode { Id = "Agricultural Waste" },
new SankeyNode { Id = "Biomass Residues" },
new SankeyNode { Id = "Bio-conversion" },
new SankeyNode { Id = "Liquid Biofuel" },
new SankeyNode { Id = "Electricity" },
new SankeyNode { Id = "Heat" }
};
List<SankeyLink> links = new List<SankeyLink>
{
new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
};
ViewBag.SankeyNodes = nodes;
ViewBag.SankeyLinks = links;
return View();
}Export Format Comparison
| Format | Use Case | Quality | File Size |
|---|---|---|---|
| PNG | Web sharing, presentations | Raster (good quality) | Medium |
| JPEG | Web images, email | Raster (good quality) | Small |
| SVG | Scalable graphics, printing | Vector (scalable) | Medium |
| Documents, archival | Vector (scalable) | Medium |
Best Practices
- PNG/JPEG: Best for quick sharing and web usage
- SVG: Best for scalable, print-ready exports
- PDF: Best for formal documents and archival purposes
- Choose format based on your distribution and usage requirements
- Test exports in different formats to ensure quality meets your needs