9.4. Code and Code Explanation

Now you should have an understanding of what information will be collected and how to retrieve it in a meaningful way so it's time to turn your attention to writing the project's code. You have the ability to create charts and graphs by using PHP's GD functions. Here you'll build two common types: a bar chart and a pie chart. Both will be coded as classes so they can easily be extended and reused in future projects. First I will take a look at the pie chart and then the bar chart. They can be incorporated into the report to augment the presentation of information in a visual way. Then I will present the code for the report itself.

9.4.1. Pie Chart

A pie chart is a circular graph that is divided into section. Each section represents how much a data member consumes of the aggregate total. In other words, a pie chart might be a good choice when you want to visually present percentage data. The PieChart class will offer three public methods, as shown in Table 9-1.

Table 9-1. PieChart Methods
MethodDescription
_construct(width)Initializes a new PieChart object with the specified width
flushImage([filename])Sends the chart image to the browser or saves to a file if filename is provided
graphData(data, colors)Graphs an array of data and uses colors as the corresponding colors for the data segments

The class has three private properties: One to store the image handle of the graph, one to store the width of the graph, and then the center of the chart. The constructor ...

Get PHP and MySQL®: Create-Modify-Reuse now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.