Custom Web Applications to Grow Your Business

(256) 429-8145

Using Jpgraph with Cakephp

If you are looking for a graph, math or geometry plugin for Cakephp, then Jpgraphs is a great option.

First, download Jpgraphs. Create a folder in your app/vendor directory called jpgraph and upload the contents of src folder into it.

In the controller you intend to use for your graph create a method / action and call it graphy. Include $this->autoRender = false in your action to prevent direct viewing

public function graphy()
    {
        $this->autoRender = false;
        App::import('Vendor', 'jpgraph/jpgraph');
        App::import('Vendor', 'jpgraph/jpgraph_line');
        // Some data
        $ydata = array(11,3,8,12,5,1,9,13,5,7);
        
        // Create the graph. These two calls are always required
        $graph = new Graph(350,250);
        $graph->SetScale('textlin');
        
        // Create the linear plot
        $lineplot=new LinePlot($ydata);
        $lineplot->SetColor('blue');
        
        // Add the plot to the graph
        $graph->Add($lineplot);
        
        // Display the graph
        $graph->Stroke();
        
    }

In the view add the following:

<?php echo $this->Html->image(array('controller' => 'contacts', 'action' => 'graphy'),array('id' => 'captcha_image'));?>

You should see a graph. If you do not, then check your php.ini file to see if GD is enabled.