-1

i get the image URL from chart.getImageURI() function. but how can i automatically move to my local server folder when i run the php file without browser. i run the php file with command line.....in SSH.

can any one explain how can i do that. thanks in advance.

<script type="text/javascript">

                 google.charts.load("current", {packages:['corechart']});

              google.charts.setOnLoadCallback(drawChart);

            function drawChart() {

                var data = new google.visualization.DataTable(//jsonarray hear);


                    var options = {
                  title: 'Title of Graph',
                  curveType: 'function',
                  width: 800,
                  height: 400

                };



               var chart_div = document.getElementById('chart_div');
                var chart = new google.visualization.LineChart(chart_div);

                  google.visualization.events.addListener(chart, 'ready', function () {

                    var imgURL = chart.getImageURI();
                    chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
                });


               chart.draw(data, options);

            }

        </script>


        <div id=\"chart_div\"></div>";  

i added the script at top of my php file.

the graph are create and its work good. but i want this graph in png image file at my local server on specific folder. this PHP file not run in browser. its run in SSH as command line.so i want automatically save the chart in image.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • you can't without using a headless browser to execute the javascript and render the chart and then convert there – charlietfl Sep 27 '16 at 14:46
  • You can't execute JavaScript code with PHP. Use a command line JavaScript engine (possibly fed by the `php script.php` output). If the Google visualization renders the chart online, you probably want to get the URI and download it with a command line, too (e.g. `curl`). – Martin Nyolt Sep 27 '16 at 14:59

1 Answers1

1

To achieve this you have to use a headless browser to execute js and render google charts in server side.

You can use PhantomJs with this library PhantomJS-Google-Charts

Here is an example : create a js file (e.g generate.js ) with this

var system = require('system');
var args = system.args;

// require PhantomJS-Google-Charts library
var GC = require('/path/to/googleCharts.js');

// google chart data as first argument
var jsonData = JSON.parse(args[1]);

GC.generateChart(jsonData, function(svgHtml){

    //filename where to save the chart as second arg
    var fileName = args[2];

    var page = require('webpage').create();

    page.content = svgHtml;

    page.render(fileName);

    phantom.exit();
});

Then in php script use exec function to execute phantomJs command which will render the graph as image :

exec("/path/to/phantomJs /path/to/generate.js '{chartOptions}' path/to/image.png");
Yacine al
  • 163
  • 2
  • 8
  • You have to install phantomjs, check this question http://stackoverflow.com/questions/8778513/how-can-i-setup-run-phantomjs-on-ubuntu – Yacine al Sep 28 '16 at 12:18