Save a google chart to file

Alemus
Alemus used Ask the Experts™
on
I need a way to save a google chart directly as a file instead of just referencing it in an image source.

So instead of <img src="charts.api.google.com?whatever>,
I need the chart api to be attached to a variable that saves directly to an image file.

ASP solution

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please show us the complete code you are using now - it may not be possible to do this but with the code in clear view, we have a chance of getting you a solution.  Best, ~Ray

Author

Commented:
<img src="http://chart.apis.google.com/chart?cht=p&chs=400x280&chd=t:<%=TotalWagesPct(1)%>,<%=TotalWagesPct(2)%>,<%=TotalWagesPct(3)%>,<%=TotalWagesPct(4)%>,<%=TotalWagesPct(5)%>,<%=TotalWagesPct(6)%>,<%=TotalWagesPct(7)%>,<%=TotalWagesPct(8)%>,<%=TotalWagesPct(9)%>,<%=TotalWagesPct(10)%>,<%=TotalWagesPct(11)%>,<%=TotalWagesPct(12)%>,<%=TotalWagesPct(13)%>,<%=TotalWagesPct(14)%>,<%=TotalWagesPct(15)%>,<%=TotalWagesPct(16)%>&chco=003366&chl=<%=PieChartLabel(1)%>|<%=PieChartLabel(2)%>|<%=PieChartLabel(3)%>|<%=PieChartLabel(4)%>|<%=PieChartLabel(5)%>|<%=PieChartLabel(6)%>|<%=PieChartLabel(7)%>|<%=PieChartLabel(8)%>|<%=PieChartLabel(9)%>|<%=PieChartLabel(10)%>|<%=PieChartLabel(11)%>|<%=PieChartLabel(12)%>|<%=PieChartLabel(13)%>|<%=PieChartLabel(14)%>|<%=PieChartLabel(15)%>|<%=PieChartLabel(16)%>&chtt=Distribution+of+Wages+Paid+by+County" alt="Wage chart" border="1" style="border-color:#CCCCCC"/>


Here is the code, such as it is. A lot of variables in there.
It is a call to a google API made through an img tag. I need to make the call so it saves directly to a file instead. This is done in Classic ASP with VB or Javascript.
Most Valuable Expert 2011
Top Expert 2016

Commented:
On this page, I tried the example.
http://code.google.com/apis/charttools/docs/choosing.html

The code snippet enabled me to save the chart on my server.  It is do-able, for sure.  You can install this and run it.  Assuming your file permissions are correct, it will work just fine.  My example is installed here:
http://www.laprbass.com/RAY_google_chart_api.php

You might want to check to be sure this is permitted by the terms of service.  

Best regards, ~Ray
<?php // RAY_google_chart_api.php
error_reporting(E_ALL);

// DEMONSTRATE HOW TO CAPTURE AN IMAGE FROM THE GOOGLE CHARTS API

// THE URL FROM THE GOOGLE CHARTS PAGE http://code.google.com/apis/charttools/docs/choosing.html
$url = 'http://chart.apis.google.com/chart?cht=p3&chd=s:Uf9a&chs=250x100&%20chl=January|February|March|April';

// READ THE IMAGE
$png = file_get_contents($url);

// WRITE THE IMAGE TO MY SERVER
file_put_contents('google.png', $png);

// PRESENT A LINK TO THE IMAGE
echo "<a href=\"google.png\" alt=\"google.png\">google.png</a>";

Open in new window

Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Most Valuable Expert 2011
Top Expert 2016

Commented:
Since this is not written in ASP (instead in PHP) please look over the code and post back if you have any questions about the functions.  The design pattern is fairly simple - you just read the data from the URL into a variable in string format, and write the string into a file on the server.  The data type in their example is "png" and IIRC there are other data types available, but png has a lossless compression and is supported by all browsers.

HTH, ~Ray

Author

Commented:
Maybe I'm overlooking something, but how do I get my ASP variables into the PHP URL string?
The example is basically passing hard-coded parameters in and I don't have those.
Most Valuable Expert 2011
Top Expert 2016
Commented:
The URL string is not PHP - it is an HTML URL - you can type it into the browser address bar and visit the web site.

I would guess that you could use come kind of concatenation operator to assemble the URL.  The PHP example for this would not be very helpful, I'm afraid, but it would go something like this...
// MAKE THIS:
$url = 'http://chart.apis.google.com/chart?cht=p3&chd=s:Uf9a&chs=250x100&%20chl=January|February|March|April';

// PIECES HERE
$months = 'January|February|March|April';
$basurl = 'http://chart.apis.google.com/chart?cht=p3&chd=s:Uf9a&chs=250x100&%20chl=';

// ASSEMBLED INTO THE URL BY STRING CONCATENATION
$url = $basurl . $months;

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for the points - it's a great question, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial