In the old Google Chart API it was possible to use PHP to render a chart, there was even a wrapper to do it:
http://code.google.com/p/gchartphp/
But with the new Chart API
http://code.google.com/apis/chart/
which produces much fancier chart, but loads only with javascript in the browser.
The effect I am trying to achieve is submitting a multiple choice form to the server via AJAX, have the PHP update the DB serverside, then return the updated chart.
In the old API way I could have done this. But in the new way I would be returning javascript to the browser and appending it to the document in order to render the chart. It wouldn’t execute because of this. I believe I could eval() this javascript but that is bad form isn’t it because someone could do some nasty stuff with that couldn’t they – eval()ing a serverside response?
How can I overcome this? Is there a PHP wrapper to help this? Or is there another why which I have overlooked?
Many thanks
The way I solved this issue was pretty obvious in the end. I just needed to step outside my thoughts and approach it from a different angle.
I was trying to all the leg work of processing the database information AND creating the Google Chart (a Pie Chart in this case) on the server side, using PHP. Then return that as the AJAX response.
Actually all I needed to do was return the data needed to create the Pie Chart (along with some extra meta data such as target element id). I did this as JSON. Then by picking through the Google Charts documentation I was able to find out how to trigger the API using client side javascript to load the returned JSON data. Then let the Google code render the chart client side – so I basically shifted all the rendering responsibility to the client browser. This is where the old and new chart APIs differ.
It took a lot of trial and error and plenty of help from Firebug. One big tripping point which is worth mentioning is that you need to load all the Google JSAPI before you receive the JSON response – so as the initial page renders – and one thing you must do with:
is make sure it loads as the page is rendering. If you call this after the page has completely rendered then the page will reload.
Hope this helps someone.