Symfony ezComponents Integration
As the documentation on how to integrate and use ezComponents in Symfony is quite meager here’s a small example on that. Exemplarily I picked the Graph component for the demonstration. Further documentaion on that can be found here.
Asuming there is an already configured symfony project with one or more application and modules the first thing to do after downloading ezComponents is to prepare symfony for the usage and copy the ezComponent classes into the project folder (they can also be referenced as explained in symfony book chapter 17 but copying the files has some advantages. E.g. you don’t have to change the application’s settings.yml when swapping from Win to Linux).
Configuration is quite easy. Just copy the base folder of the ezComponent you want to use into your symfony lib folder. In our case the folders “Base”, “autoload” and “Graph” are needed. Afterwards you just have to activate the built-in bridge class in symfony. So search for the line autoloading_functions: in your settings.yml, uncomment it and add the following line:
- [sfEzComponentsBridge, autoload]
The whole block should look like that afterwards:
.settings:
.
.
.
autoloading_functions:
- [sfEzComponentsBridge, autoload]
After doing this we can instance the ez classes like every other custom class from the lib folder. Let’s assume you have a shop and want to illustrate how many items of each product are sold per day. The following code snippets demonstrate how to generate a simple line chart showing the amount of sold products broken down by product category for each day of the year. Please ignore the absurdity or the performance issues of this approach as it shall only demonstrate how the data passed to the graph class has to be formatted ![]()
This is the actions.class.php of the module:
public function executeIndex()
{
.
.
.
(the days array could be generated with PEAR::Date)
foreach ($days as $day) {
$product_1[$day]++;
$product_2[$day]++;
$c = new Criteria();
$c->add(SalesPeer::DAY,$day);
$sales = SalesPeer::doSelect($c);
foreach ($sales as $sale) {
if($sales->getProductId() == '1') {
$product_1[$day]++;
} elseif($sales->getProductId() == '2') {
$product_2[$day]++;
}
}
This results in an array with the days of the year as indexes and the sale count as value. Now we just have to pass those arrays to the Graph component:
$data = array('Product 1' => $product_1,'Product 2' => $product_2);
//init graph
$graph = new ezcGraphLineChart();
//set title of the chart
$graph->title = 'Sales';
//tell class to generate a jpg instead of svg
$graph->driver = new ezcGraphGdDriver();
$graph->options->font = 'fonts/tutorial_font.ttf';
$graph->driver->options->supersampling = 1;
$graph->driver->options->jpegQuality = 100;
$graph->driver->options->imageFormat = IMG_JPEG;
//assign previously generated arrays to class
foreach ( $data as $item => $count ) {
$graph->data[$item] = new ezcGraphArrayDataSet( $count );
}
//render image
$graph->render( 500, 300, 'images/charts/sales_chart.jpg' );
}
The ezComponents automatically know about the application paths so if you don’t pass any dir names (like “images/charts/”) the output would be saved to the “web” folder (same for the ttf file for the jpg generation). So all you have to do to display the generated chart on a page is the call of the “image_tag” helper:
< ?php echo image_tag('charts/sales_chart.jpg'); ?>
As you can see, quite simple and very comfortable. Have fun with it!
August 4th, 2008 at 15:26
thank you, it works..