Search results

Print And Export in ASP.NET Core TreeMap control

Print

To use the print functionality, we should set the allowPrint property to true. The rendered treemap can be printed directly from the browser by calling the method print.

tagHelper
print.cs
@using Syncfusion.EJ2;
<div id="container">
<button id="print" style="width: 100px">Print</button>
     <ejs-treemap id="container" allowPrint="true" load="load" height="350px" weightValuePath="GDP">
        <e-treemap-leafitemsettings labelPath="State" labelFormat='${State}<br>${GDP} Trillion<br>(${percentage} %)'>
            <e-leafitemsettings-labelstyle color='#000000'></e-leafitemsettings-labelstyle>
        </e-treemap-leafitemsettings>
    </ejs-treemap>
</div>         
<script>
    function load(args) {
        window.treemap = args.treemap;
        var data = [
            { State: "United States", GDP: 17946, percentage: 11.08, Rank: 1 },
            { State: "China", GDP: 10866, percentage: 28.42, Rank: 2 },
            { State: "Japan", GDP: 4123, percentage: -30.78, Rank: 3 },
            { State: "Germany", GDP: 3355, percentage: -5.19, Rank: 4 },
            { State: "United Kingdom", GDP: 2848, percentage: 8.28, Rank: 5 },
            { State: "France", GDP: 2421, percentage: -9.69, Rank: 6 },
            { State: "India", GDP: 2073, percentage: 13.65, Rank: 7 },
            { State: "Italy", GDP: 1814, percentage: -12.45, Rank: 8 },
            { State: "Brazil", GDP: 1774, percentage: -27.88, Rank: 9 },
            { State: "Canada", GDP: 1550, percentage: -15.02, Rank: 10 }
        ];
        args.treemap.dataSource = data;
    };
    window.onload = function () {
        document.getElementById('print').onclick = () => {
            window.treemap.print();
        };
    }

</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Export

Image Export

To use the image export functionality, we should set the allowImageExport property to true. The rendered treemap can be exported as an image using the export method. The method requires two parameters: image type and file name. The treemap can be exported as an image in the following formats.

  • JPEG
  • PNG
  • SVG
tagHelper
export.cs
@using Syncfusion.EJ2;
<div id="container">
    <button id="export" style="width: 100px">Export</button>
    <ejs-treemap id="container" allowImageExport="true" load="load" height="350px" weightValuePath="GDP" >
	<e-treemap-leafitemsettings labelPath="State"  labelFormat= '${State}<br>${GDP} Trillion<br>(${percentage} %)' >
	<e-leafitemsettings-labelstyle color='#000000'></e-leafitemsettings-labelstyle>
	</e-treemap-leafitemsettings>
    </ejs-treemap>
</div>         
<script>
    function load(args)
    { 
        window.treemap = args.treemap;
        var data = [
    {State:"United States", GDP:17946, percentage:11.08, Rank:1},
    {State:"China", GDP:10866, percentage: 28.42, Rank:2},
    {State:"Japan", GDP:4123, percentage:-30.78, Rank:3},
    {State:"Germany", GDP:3355, percentage:-5.19, Rank:4},
    {State:"United Kingdom", GDP:2848, percentage:8.28, Rank:5},
    {State:"France", GDP:2421, percentage:-9.69, Rank:6},
    {State:"India", GDP:2073, percentage:13.65, Rank:7},
    {State:"Italy", GDP:1814, percentage:-12.45, Rank:8},
    {State:"Brazil", GDP:1774, percentage:-27.88, Rank:9},
    {State:"Canada", GDP:1550, percentage:-15.02, Rank:10}
        ];
        args.treemap.dataSource = data;
    };
	window.onload = function () {
        document.getElementById('export').onclick = () => {
            window.treemap.export('PNG', 'TreeMap');
        };
    }
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

We can get the image file as base64 string for the JPEG and PNG formats. The treemap can be exported to image as a base64 string using the export method. There are four parameters required: image type, file name, orientation of the exported PDF document which must be set as null for image export and finally allowDownload which should be set as false to return base64 string.

tagHelper
export.cs
@using Syncfusion.EJ2;
<div id="container">
    <button id="export" style="width: 100px">Export</button>
    <ejs-treemap id="container" load="load" allowImageExport="true" height="350px" weightValuePath="GDP" >
	<e-treemap-leafitemsettings labelPath="State"  labelFormat= '${State}<br>${GDP} Trillion<br>(${percentage} %)' >
	<e-leafitemsettings-labelstyle color='#000000'></e-leafitemsettings-labelstyle>
	</e-treemap-leafitemsettings>
    </ejs-treemap>
</div>         
<script>
    function load(args) {
        window.treemap = args.treemap; 
        var data = [
    {State:"United States", GDP:17946, percentage:11.08, Rank:1},
    {State:"China", GDP:10866, percentage: 28.42, Rank:2},
    {State:"Japan", GDP:4123, percentage:-30.78, Rank:3},
    {State:"Germany", GDP:3355, percentage:-5.19, Rank:4},
    {State:"United Kingdom", GDP:2848, percentage:8.28, Rank:5},
    {State:"France", GDP:2421, percentage:-9.69, Rank:6},
    {State:"India", GDP:2073, percentage:13.65, Rank:7},
    {State:"Italy", GDP:1814, percentage:-12.45, Rank:8},
    {State:"Brazil", GDP:1774, percentage:-27.88, Rank:9},
    {State:"Canada", GDP:1550, percentage:-15.02, Rank:10}
        ];
        args.treemap.dataSource = data;
    };
	window.onload = function () {
        document.getElementById('export').onclick = () => {
            window.treemap.export('JPEG', 'TreeMap', null, false).then((data) => {
            let base64 = data;
            document.writeln(base64);
        });
        };
    }
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

PDF Export

To use the PDF export functionality, we should set the allowPdfExport property to true. The rendered treemap can be exported as PDF using the export method. The export method requires three parameters: file type, file name and orientation of the PDF document. The orientation setting is optional and “0” indicates portrait and “1” indicates landscape.

tagHelper
export.cs
@using Syncfusion.EJ2;
<div id="container">
    <button id="export" style="width: 100px">Export</button>
    <ejs-treemap id="container" allowPdfExport="true" load="load" height="350px" weightValuePath="GDP" >
	<e-treemap-leafitemsettings labelPath="State"  labelFormat= '${State}<br>${GDP} Trillion<br>(${percentage} %)' >
	<e-leafitemsettings-labelstyle color='#000000'></e-leafitemsettings-labelstyle>
	</e-treemap-leafitemsettings>
    </ejs-treemap>
</div>         
<script>
    function load(args)
    { 
        window.treemap= args.treemap;
        var data = [
    {State:"United States", GDP:17946, percentage:11.08, Rank:1},
    {State:"China", GDP:10866, percentage: 28.42, Rank:2},
    {State:"Japan", GDP:4123, percentage:-30.78, Rank:3},
    {State:"Germany", GDP:3355, percentage:-5.19, Rank:4},
    {State:"United Kingdom", GDP:2848, percentage:8.28, Rank:5},
    {State:"France", GDP:2421, percentage:-9.69, Rank:6},
    {State:"India", GDP:2073, percentage:13.65, Rank:7},
    {State:"Italy", GDP:1814, percentage:-12.45, Rank:8},
    {State:"Brazil", GDP:1774, percentage:-27.88, Rank:9},
    {State:"Canada", GDP:1550, percentage:-15.02, Rank:10}
        ];
        args.treemap.dataSource = data;
    };
	window.onload = function () {
        document.getElementById('export').onclick = () => {
            window.treemap.export('PDF', 'TreeMap', 0);
        };
    }
</script>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Note: The exporting of the treemap as base64 string is not supported in the PDF export.