Execute Google Chart API (Javascript) in php file Execute Google Chart API (Javascript) in php file php php

Execute Google Chart API (Javascript) in php file


google charts have a native method (getImageURI)
it creates a base64 string representation of the chart
which can be included in the src attribute of an img element
or saved to a file as .png

see Printing PNG Charts for more info

in addition, you should wait for the chart's 'ready' event to fire,
before grabbing the image

to send the chart image in an email, recommend having a page that draws the chart
then when the 'ready' event fires, sends the image string via ajax to the controller that sends the email...

see following snippet for example of getting image...

google.charts.load('current', {  callback: function () {    var data = google.visualization.arrayToDataTable([      ['Year', 'Sales', 'Expenses'],      ['2013',  1000,      400],      ['2014',  1170,      460],      ['2015',  660,       1120],      ['2016',  1030,      540]    ]);    var options = {      title: 'Company Performance',      hAxis: {title: 'Year',  titleTextStyle: {color: '#333'}},      vAxis: {minValue: 0},      legend: {        position: 'top'      }    };    var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));    google.visualization.events.addListener(chart, 'ready', function () {      document.getElementById('image_div').innerHTML = '<img src="' + chart.getImageURI() + '" />';    });    chart.draw(data, options);  },  packages: ['corechart']});
<script src="https://www.gstatic.com/charts/loader.js"></script><div>Chart</div><div id="chart_div"></div><div>Image</div><div id="image_div"></div>