Sunday, July 14, 2019

Create a PDF with charts using Google Charts and SuiteScript

Typically the data in a chart is represented graphically, since humans are generally able to infer meaning from pictures quicker than from text. Charts are often used to ease understanding of large quantities of data and the relationships between parts of the data. Charts can usually be read more quickly than the raw data that they are produced from.

Below is a sample Suitelet code on how to include a Google Chart on a PDF generated by SuiteScript.

function generateReport() {
     var chart = nlapiEscapeXML("http://chart.apis.google.com/chart?cht=p3&chd=t:20,40,80&chs=250x100&chl=A|B|C");
    
     var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n<pdf>\n<body font-size=\"18\">\nReport with Google Chart!\n<img src=\""+chart+"\"></img></body>\n</pdf>";
     var file = nlapiXMLToPDF( xml );
    
     response.setContentType('PDF','helloworld.pdf','inline');
     response.write( file.getValue() );  
}

Here's a general explanation on the parameters of the URL.

cht=p3 – Type of chart
chd=t:20,40,80 – Values, separated by commas.
chs=250×100 – Chart size, width x height
chl=A|B|C – Labels of each chart sections.

No comments:

Post a Comment