In the example below, using graphic functions we will build a bar chart based on the values stored in MySQL database. In our case, values represent poll results.
In this example we will use poll database. To create it run the following SQL script in your MySQL server:
Script building a bar chart is quite long, so let's divide it into two parts and discuss them separately. The first part of the script connects to MySQL database and requests data that will be represented in the bar chart.
In the second part of the script first of all we will calculate variables needed to build the chart. Then, we prepare primary image and actually output data. Results for each genre are picked from the database one by one, percentage of votes is calculated, and then bars and explanatory notes are outputted.
In this script we are using TrueType fonts to get anti-aliased text. Functions that work with the TrueType fonts have its own peculiarities. First, you need to point where the fonts are located. For this, system variable GDFONTPATH is set. Then, we need to set the font name. TrueType fonts are rendered using ImageTTFText().
We have already discussed above all functions used for drawing, the code has been commented and is quite clear, so you shouldn’t have any trouble.