Print and Export in ASP.NET Core Chart component

8 May 202424 minutes to read

Print

The rendered chart can be printed directly from the browser by calling the public method print. You can pass array of ID of elements or element to this method. By default it take element of the chart.

@Html.EJS().Chart("container").ChartArea(area => area.Border(ViewBag.ChartBorder)).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).Width(2).XName("xValue").Marker(ViewBag.marker).YName("yValue").DataSource(ViewBag.dataSource).Name("Germany").Add();
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).Width(2).XName("xValue").YName("yValue1").Marker(ViewBag.marker).DataSource(ViewBag.dataSource).Name("England").Add();


       }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).MajorGridLines(ViewBag.majorGridLines).MajorGridLines(ViewBag.majorGridLines).IntervalType(Syncfusion.EJ2.Charts.IntervalType.Years).EdgeLabelPlacement(Syncfusion.EJ2.Charts.EdgeLabelPlacement.Shift).LabelFormat("y")
       ).PrimaryYAxis(py => py.LabelFormat("{value}%").RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None).MajorTickLines(ViewBag.majorTickLines).MinorTickLines(ViewBag.minorTickLines).LineStyle(ViewBag.lineStyle).Interval(20).Minimum(0).Maximum(100)
       ).Title("Inflation - Consumer Price").Tooltip(tt => tt.Enable(true)).Load("load").Render()
public IActionResult Index()
        {
              List<ColumnChartData> chartData = new List<ColumnChartData>
            {
                new ColumnChartData { x= "USA", yValue= 46 },
                new ColumnChartData { x= "GBR", yValue= 27 },
                new ColumnChartData { x= "CHN", yValue= 26 },
                new ColumnChartData { x= "UK", yValue= 26 },
                new ColumnChartData { x= "AUS", yValue= 26 },
                new ColumnChartData { x= "IND", yValue= 26 },
                new ColumnChartData { x= "DEN", yValue= 26 },
                new ColumnChartData { x= "MEX", yValue= 26 },
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class ColumnChartData
        {
            public string x;
            public double yValue;
        }

Export

The rendered chart can be exported to JPEG, PNG, SVG, PDF, XLSX, or CSV format using the export method in chart. The input parameters for this method are Type for format and FileName for result.

The optional parameters for this method are,

  • Orientation - either portrait or landscape mode during PDF export,
  • Controls - pass collections of controls for multiple export,
  • Width - width of chart export,
  • Height - height of chart export,
  • IsVertical - decides the chart export in vertical or horizontal direction,
  • Header - header for the exported chart,
  • Footer - footer for the exported chart, and
  • ExportToMultiplePages - decides to export multiple charts on separate pages for PDF export.
@Html.EJS().Chart("container").ChartArea(area => area.Border(ViewBag.ChartBorder)).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).Width(2).XName("xValue").Marker(ViewBag.marker).YName("yValue").DataSource(ViewBag.dataSource).Name("Germany").Add();
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).Width(2).XName("xValue").YName("yValue1").Marker(ViewBag.marker).DataSource(ViewBag.dataSource).Name("England").Add();


       }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).MajorGridLines(ViewBag.majorGridLines).MajorGridLines(ViewBag.majorGridLines).IntervalType(Syncfusion.EJ2.Charts.IntervalType.Years).EdgeLabelPlacement(Syncfusion.EJ2.Charts.EdgeLabelPlacement.Shift).LabelFormat("y")
       ).PrimaryYAxis(py => py.LabelFormat("{value}%").RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None).MajorTickLines(ViewBag.majorTickLines).MinorTickLines(ViewBag.minorTickLines).LineStyle(ViewBag.lineStyle).Interval(20).Minimum(0).Maximum(100)
       ).Title("Inflation - Consumer Price").Tooltip(tt => tt.Enable(true)).Load("load").Render()
public IActionResult Index()
        {
              List<ColumnChartData> chartData = new List<ColumnChartData>
            {
                new ColumnChartData { x= "USA", yValue= 46 },
                new ColumnChartData { x= "GBR", yValue= 27 },
                new ColumnChartData { x= "CHN", yValue= 26 },
                new ColumnChartData { x= "UK", yValue= 26 },
                new ColumnChartData { x= "AUS", yValue= 26 },
                new ColumnChartData { x= "IND", yValue= 26 },
                new ColumnChartData { x= "DEN", yValue= 26 },
                new ColumnChartData { x= "MEX", yValue= 26 },
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class ColumnChartData
        {
            public string x;
            public double yValue;
        }

In the export method, specify the following parameters to add a header and footer text to the exported PDF document:

  • Header - Specify the text that should appear at the top of the exported PDF document.
  • Footer - Specify the text that should appear at the bottom of the exported PDF document.
@Html.EJS().Chart("container").Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).Width(2).XName("x").YName("y").DataSource(ViewBag.dataSource).Add();
       }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.Category).Title("Manager").MajorGridLines(mg=>mg.Width(0))
       ).PrimaryYAxis(py => py.Title("Sales").MajorTickLines(mt=>mt.Width(0)).Minimum(0).Maximum(20000)
       ).Title("Sales Comparision").Render()

<div>
    @Html.EJS().Button("togglebtn").IsPrimary(true).Content("Export").Render()
</div>
<script>
    document.getElementById('togglebtn').onclick = () => {
        var chart = document.getElementById('container').ej2_instances[0];
        const header = {
            content: 'Chart Header',
            fontSize: 15
        };

        const footer = {
            content: 'Chart Footer',
            fontSize: 15,
        };
        chart.exportModule.export('PDF', 'Chart', 1, [chart], null, null, true, header, footer);
    };
</script>
public IActionResult Index()
{
    List<ExcelExportChartData> chartData = new List<ExcelExportChartData>
    {
        new ExcelExportChartData { x= "John",  y= 10000 },
        new ExcelExportChartData { x= "Jake",  y= 12000 },
        new ExcelExportChartData { x= "Peter", y= 18000 },
        new ExcelExportChartData { x= "James", y= 11000 },
        new ExcelExportChartData { x= "Mary",  y= 9700  }
    };
    ViewBag.dataSource = chartData;
    return View();
}
public class ExcelExportChartData
{
    public string x;
    public double y;
}

Exporting charts into separate page during the PDF export

During PDF export, set the ExportToMultiplePage parameter to true to export each chart as a separate page.

@Html.EJS().Chart("container1").Series(series =>
{
  series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).Width(2).XName("x").YName("y").DataSource(ViewBag.dataSource).Add();
  }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).MajorGridLines(mg=>mg.Width(0))
  ).PrimaryYAxis(py => py.Title("Million Metric Tons").MajorTickLines(mt=>mt.Width(0)).Minimum(0).Maximum(20).Interval(4)
  ).Title("Crude Steel Production Annual Growth").Render()
@Html.EJS().Chart("container2").Series(series =>
{
  series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).Width(2).XName("x").YName("y1").DataSource(ViewBag.dataSource).Add();
  }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).MajorGridLines(mg=>mg.Width(0))
  ).PrimaryYAxis(py => py.Title("Million Metric Tons").MajorTickLines(mt=>mt.Width(0)).Minimum(0).Maximum(20).Interval(4)
  ).Title("Steel Production").Render()
@Html.EJS().Chart("container3").Series(series =>
{
  series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Spline).Width(2).XName("x").YName("y2").DataSource(ViewBag.dataSource).Add();
  }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).MajorGridLines(mg=>mg.Width(0))
  ).PrimaryYAxis(py => py.Title("Million Metric Tons").MajorTickLines(mt=>mt.Width(0)).Minimum(0).Maximum(20).Interval(4)
  ).Title("Annual Growth of Crude Steel Production").Render() 
<div>      
  @Html.EJS().Button("togglebtn").IsPrimary(true).Content("Export").Render()       
</div>
<script>
    document.getElementById('togglebtn').onclick = () => {
      var chart1 = document.getElementById('container1').ej2_instances[0];
      var chart2 = document.getElementById('container2').ej2_instances[0];
      var chart3 = document.getElementById('container3').ej2_instances[0];
      chart1.exportModule.export('PDF', 'Chart', null, [chart1, chart2, chart3], null, null, true, undefined, undefined, true);
    };
</script>
public IActionResult Index()
{
    List<MultiExportChartData> chartData = new List<MultiExportChartData>
    {
        new MultiExportChartData { x = new DateTime(2012, 01, 01), y = 11.0, y1 = 19.5, y2 = 7.1 },
        new MultiExportChartData { x = new DateTime(2013, 01, 01), y = 12.9, y1 = 17.5, y2 = 6.8 },
        new MultiExportChartData { x = new DateTime(2014, 01, 01), y = 13.4, y1 = 15.5, y2 = 4.1 },
        new MultiExportChartData { x = new DateTime(2015, 01, 01), y = 13.7, y1 = 10.3, y2 = 2.8 },
        new MultiExportChartData { x = new DateTime(2016, 01, 01), y = 12.7, y1 = 7.8,  y2 = 2.8 },
        new MultiExportChartData { x = new DateTime(2017, 01, 01), y = 12.5, y1 = 5.7,  y2 = 3.8 },
        new MultiExportChartData { x = new DateTime(2018, 01, 01), y = 12.7, y1 = 5.9,  y2 = 4.3 },
        new MultiExportChartData { x = new DateTime(2019, 01, 01), y = 12.4, y1 = 5.6,  y2 = 4.7 },
        new MultiExportChartData { x = new DateTime(2020, 01, 01), y = 13.5, y1 = 5.3,  y2 = 5.6 }
    };
    ViewBag.dataSource = chartData;
    return View();
}
public class MultiExportChartData
{
    public DateTime x;
    public double y;
    public double y1;
    public double y2;
}

Export with optional parameters

The rendered chart can be exported to specific orientation, width and height by passing it as optional parameters in the export method of chart

@Html.EJS().Chart("container").ChartArea(area => area.Border(ViewBag.ChartBorder)).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).Width(2).XName("xValue").Marker(ViewBag.marker).YName("yValue").DataSource(ViewBag.dataSource).Name("Germany").Add();
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Line).Width(2).XName("xValue").YName("yValue1").Marker(ViewBag.marker).DataSource(ViewBag.dataSource).Name("England").Add();


       }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).MajorGridLines(ViewBag.majorGridLines).MajorGridLines(ViewBag.majorGridLines).IntervalType(Syncfusion.EJ2.Charts.IntervalType.Years).EdgeLabelPlacement(Syncfusion.EJ2.Charts.EdgeLabelPlacement.Shift).LabelFormat("y")
       ).PrimaryYAxis(py => py.LabelFormat("{value}%").RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None).MajorTickLines(ViewBag.majorTickLines).MinorTickLines(ViewBag.minorTickLines).LineStyle(ViewBag.lineStyle).Interval(20).Minimum(0).Maximum(100)
       ).Title("Inflation - Consumer Price").Tooltip(tt => tt.Enable(true)).Load("load").Render()
public IActionResult Index()
        {
              List<ColumnChartData> chartData = new List<ColumnChartData>
            {
                new ColumnChartData { x= "USA", yValue= 46 },
                new ColumnChartData { x= "GBR", yValue= 27 },
                new ColumnChartData { x= "CHN", yValue= 26 },
                new ColumnChartData { x= "UK", yValue= 26 },
                new ColumnChartData { x= "AUS", yValue= 26 },
                new ColumnChartData { x= "IND", yValue= 26 },
                new ColumnChartData { x= "DEN", yValue= 26 },
                new ColumnChartData { x= "MEX", yValue= 26 },
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class ColumnChartData
        {
            public string x;
            public double yValue;
        }

Multiple chart export

You can export the multiple charts in single page by passing the multiple chart objects in the export method of chart. To export multiple charts in a single page, follow the given steps:

Initially, render more than one chart to export, and then add button to export the multiple charts. In button click, call the export method in charts, and then pass the multiple chart objects in the export method.

@Html.EJS().Chart("container").Series(series =>
            {
                series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).Width(2)
                .XName("x").YName("yValue").DataSource("ViewBag.dataSource").Name("Germany").Add();

            }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.Category)
             
            ).PrimaryYAxis(py => py.RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None)
           ).Title("Inflation - Consumer Price").Tooltip(tt => tt.Enable(true).Header("Temperature")).Render()
    @Html.EJS().Chart("container1").Series(series =>
          {
              series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).Width(2)
              .XName("x").YName("yValue").DataSource("ViewBag.dataSource").Name("Germany").Add();

          }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.Category)
           
          ).PrimaryYAxis(py => py.RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None)
         ).Title("Inflation - Consumer Price").Tooltip(tt => tt.Enable(true).Header("Temperature")).Render()
<div>
    @Html.EJS().Button("togglebtn").IsPrimary(true).IconCss("e-icons e-play-icon").Content("Export").CssClass("e-flat").Render()
</div>
<script>
    document.getElementById('togglebtn').onclick = function () {
        var chart = document.getElementById('container').ej2_instances[0];
        var chart1 = document.getElementById('container1').ej2_instances[0];
        chart.export('PNG', 'chart', 'Landscape', [chart, chart1]);
    };
</script>
public IActionResult Index()
        {
              List<ColumnChartData> chartData = new List<ColumnChartData>
            {
                new ColumnChartData { x= "USA", yValue= 46 },
                new ColumnChartData { x= "GBR", yValue= 27 },
                new ColumnChartData { x= "CHN", yValue= 26 },
                new ColumnChartData { x= "UK", yValue= 26 },
                new ColumnChartData { x= "AUS", yValue= 26 },
                new ColumnChartData { x= "IND", yValue= 26 },
                new ColumnChartData { x= "DEN", yValue= 26 },
                new ColumnChartData { x= "MEX", yValue= 26 },
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class ColumnChartData
        {
            public string x;
            public double yValue;
        }

Exporting chart using base64 string

The chart can be exported as an image in the form of a base64 string by utilizing HTML canvas. This process involves rendering the chart onto a canvas element and then converting the canvas content to a base64 string.

@Html.EJS().Chart("container").Series(series =>
    {
        series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column)
        .XName("x").YName("yValue").DataSource(ViewBag.dataSource).Add();

    }).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.Category)
    ).Render()

@Html.EJS().Button("togglebtn").IsPrimary(true).IconCss("e-icons e-play-icon").Content("Export").CssClass("e-flat").Render()

<script>
    document.getElementById('togglebtn').onclick = function () {
        var svg = document.querySelector("#container_svg");
        var svgData = new XMLSerializer().serializeToString(svg);
        var canvas = document.createElement("canvas");
        document.body.appendChild(canvas);
        var svgSize = svg.getBoundingClientRect();
        canvas.width = svgSize.width;
        canvas.height = svgSize.height;
        var ctx = canvas.getContext("2d");
        var img = document.createElement("img");
        img.setAttribute("src", "data:image/svg+xml;base64," + btoa(svgData));
        img.onload = function () {
            ctx.drawImage(img, 0, 0);
            var imagedata = canvas.toDataURL("image/png");
            console.log(imagedata); // printed base64 in console
            canvas.remove();
        };
    };
</script>
public IActionResult Index()
        {
              List<ColumnChartData> chartData = new List<ColumnChartData>
            {
                new ColumnChartData { x= "USA", yValue= 46 },
                new ColumnChartData { x= "GBR", yValue= 27 },
                new ColumnChartData { x= "CHN", yValue= 26 },
                new ColumnChartData { x= "UK", yValue= 26 },
                new ColumnChartData { x= "AUS", yValue= 26 },
                new ColumnChartData { x= "IND", yValue= 26 },
                new ColumnChartData { x= "DEN", yValue= 26 },
                new ColumnChartData { x= "MEX", yValue= 26 },
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class ColumnChartData
        {
            public string x;
            public double yValue;
        }