Hello and welcome back to Red Stapler
This video is another tutorial of PHPWord
and today we'll talk about adding
chart to the document
Here's a sample document generated by
PHPWord. You can see that the PHP word
supports many types of charts. So let's
get to the code!
Here is the page that I used in previous tutorial to generate the
word document for now it creates an
empty and we're going to add the chart
First of all you're going to need the chart
input (the chart data) The first array
represent the x-axis we call it "category"
The second array represent the y-axis
value. I'm calling it "series"
Now to add a chart, you just added to the
section you already have and call
addChart method then specify the chart type
you want to add. I'm going to use "column" chart
Next provide the category and series
Next I'm going to set the width
and height of the chart. So from the chart object
you have from the addChart method
you call getStyle and you can setWidth
and setHeight
Unfortunately you cannot set it to pixel
you need to use the converter class and
supplied inch value so our chart will be
2.5 inch-width and 2 inch -height
Don't forget to include a converter
in your PHP file.
So that's it the chart should be ready
Now you can see that there's only one
set of data display on the chart.
So if you want to add more sets of data in the
same chart like we have on the example
you can you call the addSeries
method from the chart object and then
provide category and the sets of data
you want to add to the chart. I'm going
to change the data bit so you can see
the difference
Now you might wonder why I'm not using
the bar chart. Actually the bar chart for
PHP word is the horizontal chart
To switch to other types of chart is very easy
You can change the chart type like this
and regenerate the chart
So that's all for this tutorial
As always don't forget to check out other
interesting video from our Channel
New tutorial upload weekly so if you want to
stay tuned please subscribe. Bye!