Aniket17 / gchartphp

Automatically exported from code.google.com/p/gchartphp
0 stars 0 forks source link

renderImage() function added #23

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently gchartphp supports writing an image tag with src attribute and 
returning the chart API url. 

I added the following function to the gChart class. This will download the 
image serverside and send it to the browser.

----
public function renderImage(){
        $url = str_replace('&','&',$this->getUrl());
        header('Content-Type: image/png');
        readfile($url);
}
----

Maybe getUrl() should have a parm to return with or without urlencoded &amp?

To get rid of the error: 'Headers already sent', we need to remove the trailing 
\n after ?> in gChart.php. 

Also I think this it's better to use implode in the getUrl function. The 
previous code resulted in chart?&key=value, while the first parm shouldn't 
start with the ampersand.

----
public function getUrl(){
        $fullUrl = "http://";
        if(isset($this->serverNum))
            $fullUrl .= $this->getServerNumber().".";
        $fullUrl .= $this->baseUrl;
        $this -> setDataSetString();
        $parms = array();
        foreach ($this -> chart as $key => $value) {
            $parms[] = $key.'='.$value;
        }
        return $fullUrl.implode('&', $parms);
}
----

Small example of above code:

---- img.php:

$lineChart = new gLineChart($_GET['width'],$_GET['height']);
$lineChart->addDataSet(array(120,131,135,160,129,22,190));
$lineChart->setLegend(array('Nice figures'));
$lineChart->setColors(array('ED237A'));
$lineChart->setVisibleAxes(array('x','y'));
$lineChart->setDataRange(0,200);
$lineChart->setLegendPosition('t');
// axisnr, from, to, step
$lineChart->addAxisRange(0,0,28,7);
$lineChart->addAxisRange(1,0,200);

$lineChart->setGridLines(25,10);
$lineChart->renderImage();

---- view.html:

<img src="img.php?width=300&amp;height=200" />

As you can see, this also keeps the data to Google hidden from the users. I 
hope these ideas can me merged into svn.

Thanks,
Tijmen

Original issue reported on code.google.com by tijmen.r...@gmail.com on 18 Jun 2010 at 2:04

GoogleCodeExporter commented 9 years ago
Thank you for the code. I will have some time next week and I will try to add 
the code to the project.

Original comment by bardellie on 2 Jul 2010 at 2:19

GoogleCodeExporter commented 9 years ago
All the changes that you proposed were merged in r44, r45, and r46.

Thank you for your contribution!

Original comment by bardellie on 4 Jul 2010 at 5:21