It is obvious that a “half decent” application to generate reports has to include a tool for the creation of charts and graphs on the fly, so PHPDOCX could not do less than that.
In fact PHPDOCX is a pretty sophisticated tool to that regard and as we show below is very simple to generate a nice chart from some data that could be extracted from a database, a spreadsheet or elsewhere.
In this example we will concentrate in creating a pie chart that plots the following sample data:
Market Share Distribution by Country
Spain: 50
China: 10
USA: 40
Germany: 20
require_once('../classes/cCreateDocx.inc');
$objDocx = new cCreateDocx();
$arrDatos = array( 'Spain' => array(50), 'China' => array(10), 'USA' => array(40), 'Germany' => array(20));
$arrArgs = array('data' => $arrDatos, 'type' => 'pieChart', 'title' => 'Market Share Distribution by Country', 'showPercent' => 1 );
$objDocx->fAddGraphic($arrArgs);
$objDocx->fCreateDocx('simple_piechart');
That generates this pie chart.
If you prefer a 3D version of this pie chart you need the Pro version but the required code is not much different:
require_once('../classes/cCreateDocx.inc');
$objDocx = new cCreateDocx();
$arrDatos = array( 'Spain' => array(50), 'China' => array(10), 'USA' => array(40), 'Germany' => array(20));
$arrArgs = array('data' => $arrDatos, 'type' => 'pie3DChart', 'title' => 'Market Share Distribution by Country', 'showPercent' => 1 );
$objDocx->fAddGraphic($arrArgs);
$objDocx->fCreateDocx('simple_piechart_3d');
If you enjoyed this post you might want to subscribe to our RSS Feed!


4 Responses
I have tried the above example code. But it didn’t work. It is b’coz of AddGraphics function from the code. If I comment this function it will create docx without any contents.
Please advice on the same.
Thanks for writing. I worked a lot of work
I am having exactly the same issue as Vandy. Nothing is being created, and there are no errors displaying on the screen.
For reference I am using php5.3 on Linux