|
Charts show in URL, but will not save to file |
Posted by rpetras on Mar-23-2012 21:24 |
|
Originally posted in "general" but this question probably makes more sense here.
------------------------------------------------------
Hi, I'm experiencing an odd problem.
I've been using ChartDirector (Perl Version) for years to display to web pages using the :
binmode(STDOUT);
print "Content-type: image/png\\n\\n";
print $c->makeChart2($perlchartdir::PNG);
style of output code. This works fine.
We recently got a requirement to save off a few charts to be emailed at a later time.
I adjusted the output code as follows:
#Output the chart
if ($saveName) {
$c->makeChart("/tmp/$saveName.png");
} else {
binmode(STDOUT);
print "Content-type: image/png\\n\\n";
print $c->makeChart2($perlchartdir::PNG);
}
So, if the chart comes in with a name, it should save it to /tmp/<name>.png, which seems simple enough.
However, it is not saving.
If I simplify to something like :
#Output the chart
if ($saveName) {
$c->makeChart("/tmp/test1.png");
} else {
binmode(STDOUT);
print "Content-type: image/png\\n\\n";
print $c->makeChart2($perlchartdir::PNG);
}
I can, at least, get that to work ... but not consistently.
If I run the above without the saveName variable, view the chart, THEN run with the saveName variable, I can get it to output a file.
This behavior seems inconsistent to me.
I've checked simple things like file permissions etc.
So, ultimately, I need to be able to output to the web sometimes, and save to files sometimes.
Any help would be appreciated. |
Re: Charts show in URL, but will not save to file |
Posted by rpetras on Mar-24-2012 04:47 |
|
Thanks Peter,
I tried your alternate save method, and it did not change the behavior.
I feel my OP may have confused the issue a bit though.
I know that I can use either a hard coded names OR a variables for the names of files.
I have tested this and it works just fine on test scripts.
My main issue, is that when I try to select between setting a chart for web display, or saving it to a file, in general, the files are not being saved.
With ChartDirector, it should be a simple matter of directing the output to makechart or makechart2.
Now, on the few occasions where I have been able to save the files, my actions were as follows.
1: Run script with switch set to generate a web chart
2: View the chart
3: Re run the script with the flag set to generate a save file.
If I just try to generate the save file, I get nothing.
If I just run the web output, it works fine, as it has for years.
Any additional ideas would be greatly appreciated. |
Re: Charts show in URL, but will not save to file |
Posted by Peter Kwan on Mar-27-2012 00:18 |
|
Hi rpetras,
ChartDirector can both save the chart image to a file, and stream the chart image to the browser. For example:
$c->makeChart("/tmp/test1.png");
binmode(STDOUT);
print "Content-type: image/png\\n\\n";
print $c->makeChart2($perlchartdir::PNG);
If the request is sent from an <IMG> tag URL, then the browser should be able to display the chart.
I am not too sure the exact meaning of "to select between setting a chart for web display, or saving it to a file". Do you mean you are using an "if" statement to select between setting a chart for web display or or saving it to a file? If the code does not work if there is a "if" statement, one possibly explanation is that the condition in the "if" statement is false.
For example, consider the following code:
if ($saveName) {
$c->makeChart("/tmp/test1.png");
} else {
binmode(STDOUT);
print "Content-type: image/png\\n\\n";
print $c->makeChart2($perlchartdir::PNG);
}
the file is only saved as an image if ($saveName) evaluates to true. If the condition is false, the file is not saved, but instead the chart is streamed to the browser (I assume your code is running as a CGI in a server). The browser may not display the chart the request is not issued through an <IMG> tag, so you may not be aware the chart is streamed to the browser.
If the filename is not hard coded as "/tmp/test1.png", but is in a variable, the chart image can be saved only if the file name is valid. Is there any verification in your code to confirm that the filename is valid (like including the variable filename in the chart title, and save it in a hard coded filename as well as the variable filename)?
Another thing you may check if to verify if your script is being run at all. If you save the image file, I am not sure if your code outputs any other thing, and if the browser will perform error checking to make sure it receives the output from your code. If your code outputs nothing other than saving the file, or the browser does not check if it receives the expected output from your CGI script, it is hard to verify if your script has actually been executed.
Regards
Peter Kwan |
Re: Charts show in URL, but will not save to file |
Posted by rpetras on Mar-28-2012 02:46 |
|
Thanks Peter, that's what it was.
Since it was normally embedded in an IMG tag, it only ran when displayed.
I rewrote the calling code and CD worked just perfectly.
Thanks for your support! |
|