Practical phpdocx



Working with charts is as easy as with pictures. You just need to choose the kind of graphic to insert and add the values and legends.

There are 2D and 3D graphic available, with many formats like cylinder, cone or pyramid, column type, pie format, line, area, radar, surface, doughnut...

You can add charts to new documents, replace placeholders with charts in a template or change the values of already existing charts.

Adding charts

For adding charts in a document, call this method:

It counts with multiple options to use in the array of parameters. The most important are:

  • Type: Type of chart
  • Data: Array of values
  • Title: Text used as a title

Beside these three attributes, you can define other properties as rotation with rotX and rotY, perspective, distance between values, sizes, and so on.

For example, for adding a pie type table, first define an array:

In this array, write the legends of the charts as keys and the arrays of data you want to insert as values.

Next, define the options of the chart:

Note that this method sets its size and alignment.

Finally, add the chart:

Adding charts in templates

You can create a chart as a WordFragment and substitute it instead of a specific placehoder. This procedure is similar to the placeholder template replacement for text and images.

This method is the same as the one explained for images:

For example, you can create as a WordFragment the same chart seen before.

Then, replace it in the template:

Modify the values of an existing chart

The class DocxUtilities, available in the Advanced and Premium licenses, has a useful method for replacing the values of a chart found in a document. This allows to create charts using MS Word and to easily replace its values without having to generate them previously.

This is the method to code:

These are the available parameters:

  • $source: DOCX document with the chart(s) to replace
  • $target: DOCX destination
  • $chartData: with an array which key is the chart number you want to change depending on its position in the document. Its value is the data you want to add

For example, do this to replace the two charts of the document:

Define the new values:

Then, create an array to replace the first position (“position 0”) and the second (“position”) chart:

Finally, make the replacement:

Tips and tricks

phpdocx includes the color schemes used in Word for charts. You can define this property through the 'color' parameter of the addChart method.

Next - HTML and CSS