Export charts as PDF in angular js /Javascript

Export charts in PDF

View project on GitHub

About Project.

This project aims at adding the feature of downloading chart images in PDF format in current angular charts. All charts example has been used from http://krispo.github.io/angular-nvd3/

Check demo at

Example for Line charts http://readinspeed.com/trial/chart/downloadchartsasPDF/LineChart.html

Example for Cumulative Line Chart http://readinspeed.com/trial/chart/downloadchartsasPDF/CumalitiveLIneChart.html

How to use it?

Initially, download "Download.zip", unzip it following folder structure can be observed.

  1. bower_components
  2. CumalitiveLIneChart.html
  3. LineChart.html

Run "LineChart.html" or "CumalitiveLIneChart.html" you will see the demo

To install in your application

1.Following imports need to be added to your HTML file

<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/d3/d3.min.js"></script>
<script src="bower_components/nvd3/build/nv.d3.js"></script> <!-- or use another assembly -->
<script src="bower_components/nvd3/angular-nvd3.js"></script>
<script src="bower_components/other/FileSaver.js"></script>
<script type="text/javascript" src="bower_components/other/rgbcolor.js"></script>
<script type="text/javascript" src="bower_components/other/StackBlur.js"></script>
<script type="text/javascript" src="bower_components/other/canvg.js"></script>
<script type="text/javascript" src="bower_components/other/StackBlur.js"></script>
<script type="text/javascript" src="bower_components/other/jspdf.min.js"></script>
<link rel="stylesheet" href="bower_components/nvd3/build/nv.d3.css">
  1. Now add the following code in HTML file implementing graphs.

    <button ng-click="exportData('testChart','filename')">Download as Pdf</button> <canvas id="canvas" width="3000" height="100">

Here pass 'id' of the chart and filename for the downloadable file name in exportData function eg: here id is "testChart" and downloadable filename is 'filename' so call would be "exportData('testChart','filename')"

  1. Now add following methods in your controller.

//METHOD-1

`$scope.exportData = function (id,fileName) {
var el = document.createElement( 'html' );
el.innerHTML =document.getElementById(id).innerHTML;
var svgContent=el.getElementsByTagName( 'svg' );
var contentModified=svgContent[0].outerHTML;
contentModified=contentModified.replace('style="height: 450px; width: 100%;"','style="height: 450px; width: 100%;fill:#fff;"');
contentModified=contentModified.replace(new RegExp('style="text-anchor: middle;"', 'g'), 'style="text-anchor:   middle;fill:black;"');
contentModified=contentModified.replace(new RegExp('style="text-anchor: end;"', 'g'), 'style="text-anchor: end;fill:black;"');
$scope.render(contentModified,1500,500);
};`

//METHOD-2

`$scope.render=function(svg, width, height,fileName){
document.createElement('canvas')
var c = document.createElement('canvas');
c.width = width || 1500;
c.height = height || 500;
document.getElementById('canvas').innerHTML = '';
document.getElementById('canvas').appendChild(c);
if (typeof FlashCanvas != "undefined") {
FlashCanvas.initElement(c);
}
canvg(c, svg, { log: true, renderCallback: function (dom) {
if (typeof FlashCanvas != "undefined") {
document.getElementById('svg').innerHTML = 'svg not supported';
} else {
var svg = (new XMLSerializer()).serializeToString(dom);
var canvasdata = c.toDataURL("image/png");
   var doc = new jsPDF();
doc.setFontSize(40);
doc.text(35, 25, "Sample Text added");
doc.addImage(canvasdata, 'png', 15, 40, 230, 100);
doc.save(fileName+'.pdf');
}
}});
};`

Authors and Contributors

In 2015 @gansubhai created this project to provide download charts as PDF in angular charts.

License

(MIT License)

Copyright (c) 2011-2016 Ganesh More,

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Support or Contact

Having trouble with Pages? (http://readinspeed.com/#three) and we’ll help you sort it out.