Available in the Full Version

Data Chart - Export image

This sample demonstrates how to export the chart as a JPG image by using exportImage method.
Population data from:
U.S. Census Bureau

This sample is designed for a larger screen size.

On mobile, try rotating your screen, view full size, or email to another device.

Code View

Copy to Clipboard
<!DOCTYPE html>
<html>
<head>
    <title>JSON Binding</title>

    <!-- Ignite UI for jQuery Required Combined CSS Files -->
    <link href="http://cdn-na.infragistics.com/igniteui/2024.2/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet" />
    <link href="http://cdn-na.infragistics.com/igniteui/2024.2/latest/css/structure/infragistics.css" rel="stylesheet" />

    <!--CSS file specific for chart styling -->
    <link href="http://cdn-na.infragistics.com/igniteui/2024.2/latest/css/structure/modules/infragistics.ui.chart.css" rel="stylesheet" />

    <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>

    <!-- Ignite UI for jQuery Required Combined JavaScript Files -->
    <script src="http://cdn-na.infragistics.com/igniteui/2024.2/latest/js/infragistics.core.js"></script>
    <script src="http://cdn-na.infragistics.com/igniteui/2024.2/latest/js/infragistics.dv.js"></script>    
    <!-- External files for exporting -->
    <script src="/js/external/FileSaver.js"></script>
    <style>

    </style>
</head>
<body>
    <div class="USCensus-attribution">
        Population data from:<br />
        <a href="http://www.census.gov/" target="_blank">U.S. Census Bureau</a>
    </div>
    <div style="width: 80%; min-width: 210px;">
        <div id="chart"></div>
        <br />
        <input id="exportToPdfBtn" onclick="ExportImage()" value="Export as image" type="button" />
    </div>
    <script>

        $(function () {

            var data = [
                { "CountryName": "China", "Pop1995": 1216, "Pop2005": 1297, "Pop2015": 1361, "Pop2025": 1394 },
                { "CountryName": "India", "Pop1995": 920, "Pop2005": 1090, "Pop2015": 1251, "Pop2025": 1396 },
                { "CountryName": "United States", "Pop1995": 266, "Pop2005": 295, "Pop2015": 322, "Pop2025": 351 },
                { "CountryName": "Indonesia", "Pop1995": 197, "Pop2005": 229, "Pop2015": 256, "Pop2025": 277 },
                { "CountryName": "Brazil", "Pop1995": 161, "Pop2005": 186, "Pop2015": 204, "Pop2025": 218 }
            ];

            $("#chart").igDataChart({
                width: "100%",
                height: "400px",
                title: "Population per Country",
                subtitle: "Five largest projected populations for 2015",
                plotAreaBackground: "white",
                dataSource: data,
                axes: [
                    {
                        name: "NameAxis",
                        type: "categoryX",
                        title: "Country",
                        label: "CountryName"
                    },
                    {
                        name: "PopulationAxis",
                        type: "numericY",
                        minimumValue: 0,
                        title: "Millions of People",
                    }
                ],
                series: [
                    {
                        name: "2015Population",
                        type: "column",
                        isHighlightingEnabled: true,
                        isTransitionInEnabled: true,
                        xAxis: "NameAxis",
                        yAxis: "PopulationAxis",
                        valueMemberPath: "Pop2015",
                        isDropShadowEnabled: true,
                        shadowBlur: 40,
                        shadowColor: "lightgray",
                        shadowOffsetX: 7,
                        shadowOffsetY: -5,
                        brush: "#ff8e3e"
                    }
                ]
            });
        });

        function ExportImage() {
            var img = $('#chart').igDataChart("exportImage", $('#chart').width(), $('#chart').height());

			// atob to base64_decode the data-URI
			var image_data = atob(img.src.split(',')[1]);
			
			// Use typed arrays to convert the binary data to a Blob
			var arraybuffer = new ArrayBuffer(image_data.length);
			var view = new Uint8Array(arraybuffer);
			for (var i=0; i<image_data.length; i++) {
				view[i] = image_data.charCodeAt(i) & 0xff;
			}
	
			var blob = new Blob([arraybuffer], {type: 'image/jpeg'});
			saveAs(blob, "img.jpg");
        };
    </script>

</body>
</html>